2015-11-17 26 views
1

我是android新手。我正在開發一個考試應用程序,我想在對話框中顯示所有問題編號,以便用戶可以切換到任何問題,即1,2,3,4等。我嘗試了下面的代碼,但它不正確。我從服務器示例中得到了全部的問題,我有40個問題,所以我想在對話框中顯示1到40之間的數字。請幫忙在對話框中添加數字

AlertDialog.Builder alert = new AlertDialog.Builder(context); 

      LinearLayout layout = new LinearLayout(this); 
      layout.setOrientation(LinearLayout.HORIZONTAL); 
      layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)); 

      for (int i = 0; i < 20; i++) { 
       // Set an EditText view to get user input 
       final TextView input = new TextView(context); 
       input.setText("" + i); 
       input.setPadding(5, 5, 5, 5); 
       input.setTextSize(15); 

       input.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         String srt = "clickd"; 
         Toast.makeText(context, srt, Toast.LENGTH_LONG).show(); 

        } 
       }); 
       layout.addView(input); 
       alert.setView(layout); 
      } 
      alert.setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        //You will get as string input data in this variable. 
        // here we convert the input to a string and show in a toast. 
        String srt = "fdsfdsf"; 
        Toast.makeText(context, srt, Toast.LENGTH_LONG).show(); 
       } // End of onClick(DialogInterface dialog, int whichButton) 
      }); //End of alert.setPositiveButton 
      alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int whichButton) { 
        // Canceled. 
        dialog.cancel(); 
       } 
      }); //End of alert.setNegativeButton 
      AlertDialog alertDialog = alert.create(); 
      alertDialog.show(); 

我已經在這裏提出了20個問題,但它可以是任何沒有。這樣

enter image description here

+1

爲什麼你不使用對話框內的列表視圖 –

+0

@VishalGaur我發佈了一張圖片。請檢查。我想要這樣的東西 – Priyanka

+0

你應該使用對話框片段並創建GridView佈局 –

回答

1

有些事情似乎您試圖顯示固定大小的數據,讓說,從response.Simple路20號是設置GridView控件的對話框裏,像這樣的數據定額:

private void showDialog() { 
    //Here is ur gridview 
    GridView gridView = new GridView(ctx); 

    List<Integer> mList = new ArrayList<Integer>(); 
    for (int i = 1 ; i < 20; i++) { 
     mList.add(i); 
    } 

    gridView.setAdapter(new ArrayAdapter(ctx, android.R.layout.simple_list_item_1, mList)); 
    gridView.setNumColumns(4); 
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      // do what ev you want with ur stuff here 
     } 
    }); 

    // Here you can set grid view to ur dialog 
    AlertDialog.Builder builder = new AlertDialog.Builder(ctxI); 
    builder.setView(gridView); 
    builder.setTitle("Its me !!"); 
    builder.show(); 
} 
相關問題