AlertDialog,ListView with simpleAdapter和onItemClickListener

时间:2013-01-11 09:35:28

标签: android listview alertdialog onitemclicklistener simpleadapter

我有一个带ListView的AlertDialog,我无法理解如何获取检查项目...... 在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(无法附加图片)

但是当我尝试选择任何元素(checkBoxes)时 - 没有任何反应...... 求助。

1 个答案:

答案 0 :(得分:0)

你应该为每个复选框添加监听器。对于textView,listview上的List项侦听器可以工作,但不能用于复选框。