【问题标题】:AlertDialog, ListView with simpleAdapter and onItemClickListener带有 simpleAdapter 和 onItemClickListener 的 AlertDialog、ListView
【发布时间】:2013-01-11 09:35:28
【问题描述】:

我有一个带有 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;
  }

     }

主类中的一些方法(扩展 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(无法附加图片)

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

【问题讨论】:

    标签: android listview android-alertdialog onitemclicklistener simpleadapter


    【解决方案1】:

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

    【讨论】:

    猜你喜欢
    • 2016-12-11
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多