2

我有一個AlertDialog ListView和我不明白如何獲取檢查項目... 在AlertDialog我創建代碼中的視圖。 請解決我的問題。AlertDialog,具有simpleAdapter和onItemClickListener的ListView

AlertDialog。正常運行的:

 public class MyDialog { 
...... 
AlertDialog.Builder adb; 
private Dialog onCreateDialog(int id, Context context) { 
    workWithDB = new WorkWithDB(context); 
    adb = new AlertDialog.Builder(context); 
    switch (id) { 
    case ActivityMain.DIALOG_ADD_BUTTONS: 
      adb.setTitle(R.string.app_name); 
      adb.setIcon(android.R.drawable.ic_input_add); 
      adb.setPositiveButton(R.string.dialogAddBtn, myClickListener); 
      adb.setNeutralButton(R.string.dialogConfirmChangesCancel, myClickListener); 
      adb.setView(addBtnView()); 
     return adb.create(); 
    } 
     return onCreateDialog(id, context); 
    } 
OnClickListener myClickListener = new OnClickListener() { 
    public void onClick(DialogInterface dialog, int which) { 
    // Main.editor.putBoolean("continue", true); 
    // Main.editor.commit(); 
     switch (id) { 
     case ActivityMain.DIALOG_ADD_BUTTONS: 
      // some code 
      break; 
     } 

    } 
    }; 
    public void showDialog(int id, Context context /* some parametres*/){ 
     onCreateDialog(id, context); 
     adb.show();  
    } 
    protected void setViewForAddBtnView(ListView lstView){ 
     this.lstView = lstView; 
    } 
    protected View addBtnView(){ 
     LinearLayout llMain = new LinearLayout(context); 
     RadioGroup rGroupWhereAddBtns = new RadioGroup(context); 
     RadioButton top = new RadioButton(context), bottom = new RadioButton(context); 
     top.setText(context.getResources().getString(R.string.addBtnTop)); 
     bottom.setText(context.getResources().getString(R.string.addBtnBottom)); 
     rGroupWhereAddBtns.setOrientation(RadioGroup.HORIZONTAL); 
     rGroupWhereAddBtns.addView(top); 
     rGroupWhereAddBtns.addView(bottom); 

     llMain.setOrientation(LinearLayout.VERTICAL); 
     llMain.addView((new MyMsg()).view(context, " " + message, Color.BLACK, ActivityMain.messageTextSize + 2, Gravity.LEFT));  
     llMain.addView(rGroupWhereAddBtns); 
     llMain.addView(lstView); 
     return llMain; 
    } 

    } 

從主類的一些方法(擴展活動):

  private ListView createListViewForAddBtnDialog(){ 
    LinkedHashMap<String, String> mapOfBtn; 
    SimpleAdapter adapterAddBtnDialog; 
    ListView lstViewBtnAdd = new ListView(this); 
    SQLiteDatabase dbBtn = calcDbHelper.getReadableDatabase();   
    Cursor cc = dbBtn.query(CalcDBHelper.TABLE_BUTTONS, null, "profileName = 'unnamed' and orientation = '"+ orient + "' and canBeAdded = 1", null, null, null, null);   
    //try { 
    if (cc.moveToFirst()){ 
     //(new WorkWithDB(context)).showButtonsTable(); 
     Log.d(ActivityMain.LOG_TAG, "cursor count = " + cc.getCount()); 
     listOfBtn = new ArrayList<Map<String,String>>(cc.getCount()); 
     int btnIdIndex = cc.getColumnIndex(CalcDBHelper.BTN_ID); 
     do{ 
      mapOfBtn = new LinkedHashMap<String, String>(); 
      mapOfBtn.put(ActivityMain.btnId, getBtn.getBtnTextViaId(cc.getInt(btnIdIndex))); 
      listOfBtn.add(mapOfBtn); 
     } while(cc.moveToNext()); 
    } 
    for(int i = 0; i < listOfBtn.size(); i++){ 
     Log.d(ActivityMain.LOG_TAG, "pos " + i + " text - " + listOfBtn.get(i)); 
    } 
    adapterAddBtnDialog = new SimpleAdapter(this, listOfBtn, R.layout.dialog_add_btn_item, FROM, TO); 
    lstViewBtnAdd.setAdapter(adapterAddBtnDialog); 
    lstViewBtnAdd.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Log.d(ActivityMain.LOG_TAG, "!!!!!!! - "); // never call :(((
     } 
    }); 
    return lstViewBtnAdd; 
} 

我打電話給我的AlertDialog是這樣的:

 myDialog.setViewForAddBtnView(createListViewForAddBtnDialog()); 
     myDialog.showDialog(DIALOG_ADD_BUTTONS, getResources().getString(R.string.dialogAddBtnWhereToAdd), this, this, null, orient); 

,它看起來像

https://www.dropbox.com/s/r2hqb9a9nt9g4hm/alertDlg.png (不能附加圖片)

但是當我嘗試選擇任何元素(複選框) - 沒有反應... 請幫助。

回答

0

您應該爲每個複選框添加監聽器。對於作爲列表視圖上的列表項監聽器的textView工作,但不適用於複選框。

+0

thx諮詢。我在這裏找到解決方案 - http://www.vogella.com/articles/AndroidListView/article.html –

相關問題