我的多选对话框正在运行并弹出正确的值。但是,当单击保存按钮时,我很难理解如何设置和检索值。
我的代码如下:
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) 以测试它是否包含应用程序刚刚崩溃的任何内容时。
如何检索/存储这些值?然后下次预填充列表
汤姆