我有一个multiChoiceItems
对话框,其中的数据是从MySQL数据库查询中获取的,然后添加到字符串中,然后再次添加到类型为string的ArrayList
中,然后将其转换为在对话框如下:
游标数据= mDatabaseHelper.getWorkoutNames();
if (data != null) {
if (data.moveToFirst()) {
do {
workoutNameTobeAddded = data.getString(0);
mWorkoutNames.add(workoutNameTobeAddded);
} while (data.moveToNext());
}
//CONVERTING ARRAY_LIST TO A STRING ARRAY
workoutNameList = mWorkoutNames.toArray(new String[mWorkoutNames.size()]);
}
然后,我检查
multiChoice
是否为String数组(如果为空),以显示一个对话框,说明是这样,然后,否则意味着它具有数据,以在带有workoutNameList
的对话框中显示名称,如下所示:if(workoutNameList.length<1){
builder = new AlertDialog.Builder(Main2Activity.this);
builder.setTitle("Workouts not created")
.setMessage("Do you want to create a workout?")
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Do nothing
dialog.dismiss();
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(Main2Activity.this,NewWorkoutActivity.class);
startActivity(intent);
}
}).setCancelable(false);
dialog = builder.create();
dialog.show();
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(Main2Activity.this);
builder.setTitle("Select workout(s) to perform");
builder.setMultiChoiceItems(workoutNameList, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
}
});
builder.setPositiveButton(R.string.Yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setCancelable(false);
AlertDialog dialog = builder.create();
dialog.show();
}
}
轻按按钮即可调用此方法。因此,每次使用该代码时,代码都会运行并不断向对话框中添加相同的数据。如何防止这种情况发生,因此该对话框仅显示DB中的名称而不重复自身,并检查是否有新的名称在数据库中命名并添加它。
最佳答案
确保先清除mWorkoutNames
,然后再(重新)填充它们Cursor data = mDatabaseHelper.getWorkoutNames();
。