0

我的多选对话框正在运行并弹出正确的值。但是,当单击保存按钮时,我很难理解如何设置和检索值。

我的代码如下:

   public void addCondition(View view){

        SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
        Cursor f = db.rawQuery("select * from assetobservationtypes", null);  
        Log.e("Asset Helper Types:", "Cursor run");
         List<CharSequence> mHelperNames = new ArrayList<CharSequence>();
         final ArrayList<String> mHelperNamesID= new ArrayList<String>();  

        if(f.getCount() > 0) {
            f.moveToFirst();
               while(!f.isAfterLast()) {
                   mHelperNames.add(f.getString(f.getColumnIndex("Observation")));
                   mHelperNamesID.add(f.getString(f.getColumnIndex("AssetObsID")));

                    f.moveToNext();
               }
        }

        f.close();

        final List<Integer> mSelectedItems = new ArrayList<Integer>();

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("My Title")
                    .setMultiChoiceItems(mHelperNames.toArray(new CharSequence[mHelperNames.size()]), null,
                            new DialogInterface.OnMultiChoiceClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which, boolean isChecked) {
                                    if (isChecked) {

                                        mSelectedItems.add(which);
                                    } else if (mSelectedItems.contains(which)) {

                                        mSelectedItems.remove(Integer
                                                .valueOf(which));
                                    }
                                }
                            })

                   .setPositiveButton("Save", new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialog, int id) {                      
                           txtCondition.setText("set");
                           txtCondition.setTextColor(Color.parseColor("#4c9226"));
                           count++;
                       }
                   })
                   .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialog, int id) {
                           dialog.dismiss();
                       }
                   });
            builder.show();
    }

我知道该列表在数组 mSelectedItems 中,但是当我尝试在 setPositiveButton 的 onclick 上记录 mSelectedItems.get(0) 以测试它是否包含应用程序刚刚崩溃的任何内容时。

如何检索/存储这些值?然后下次预填充列表

汤姆

4

1 回答 1

0

我猜想通过在下次您想在AlertDialog再次显示对话框时显示选中的项目时预先填充列表。在这种情况下,mSelectedItems在您的Activity(最初将是一个空列表)中创建 a 字段:

final List<Integer> mSelectedItems = new ArrayList<Integer>();

和你的调用方法:

public void addCondition(View view) {
    SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
    Cursor f = db.rawQuery("select * from assetobservationtypes", null);  
    Log.e("Asset Helper Types:", "Cursor run");
    List<CharSequence> mHelperNames = new ArrayList<CharSequence>();
    final ArrayList<String> mHelperNamesID= new ArrayList<String>();  
    while(f.moveToNext()) {
        mHelperNames.add(f.getString(f.getColumnIndex("Observation")));
        mHelperNamesID.add(f.getString(f.getColumnIndex("AssetObsID")));
    }
    f.close();
    boolean[] checkedItems = new boolean[mHelperNames.size()];
    for (int  i = 0; i < checkedItems.length; i++) {
        checkedItems[i] = false;
    }
    for (int i = 0; i < mSelectedItems.size(); i++) {
        checkedItems[mSelectedItems.get(i).intValue()] = true;
    }
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("My Title").setMultiChoiceItems(mHelperNames.toArray(new CharSequence[mHelperNames.size()]), checkedItems,
                        new DialogInterface.OnMultiChoiceClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which, boolean isChecked) {
                                if (isChecked) {

                                    mSelectedItems.add(which);
                                } else if (mSelectedItems.contains(which)) {

                                    mSelectedItems.remove(Integer
                                            .valueOf(which));
                                }
                            }
                        })

               .setPositiveButton("Save", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {                      
                       txtCondition.setText("set");
                       txtCondition.setTextColor(Color.parseColor("#4c9226"));
                       count++;
                   }
               })
               .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {
                       dialog.dismiss();
                   }
               });
        builder.show();
}

请记住,mHelperNames列表应该是相同的(您不能更改 中的值Cursor),否则您可能会在选中/未选中的项目甚至异常之间存在一些不一致的风险。我希望代码是正确的,我没有测试过。

于 2012-11-26T17:12:04.707 回答