2

我有一个带有 ListView 的 AlertDialog,但我无法理解如何获取检查项目...在 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;
  }

     }

Main Class 中的一些方法(扩展 Activity):

         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(无法附加图片)

但是当我尝试选择任何元素(复选框)时 - 没有任何反应......请帮助。

4

1 回答 1

0

您应该为每个复选框添加侦听器。对于 textView 作为 listview 上的列表项侦听器有效,但不适用于复选框。

于 2013-01-11T09:53:55.477 回答