2

J'ai un AlertDialog avec ListView et je ne peux pas comprendre comment obtenir des éléments cochés ... Dans AlertDialog je crée vue dans le code. Résoudre mes problèmes s'il vous plaît.AlertDialog, ListView avec simpleAdapter et onItemClickListener

AlertDialog. TRAVAIL CORRECT:

 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; 
    } 

    } 

une méthode de classe principale (étend l'activité):

  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; 
} 

J'appelle mon AlertDialog comme ceci:

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

et il ressemble à

https://www.dropbox.com/s/r2hqb9a9nt9g4hm/alertDlg.png (ne peut pas joindre des images)

mais quand j'essaie de choisir des éléments (checkBoxes) - rien ne se passe ... aide s'il vous plaît.

Répondre

0

vous devez ajouter un écouteur à chaque case à cocher. Pour textView en tant qu'écouteur d'élément de liste sur listview fonctionne, mais pas pour la case à cocher.

+0

Merci pour le conseil. J'ai trouvé une solution ici - http://www.vogella.com/articles/AndroidListView/article.html –

Questions connexes