3
使用遊標實現setMultiChoiceItems時,必須指定isCheckedColumn。如何在使用Sqlite光標時更新對話框中的setMultiChoiceItems複選框
問題如other sites所述,當用戶從列表中選擇一項時,複選框不會更新。有些人建議每次用戶選擇一個項目時更新SqLite表格,但這在我的應用程序中不起作用。這是我想出的解決方案。
使用遊標實現setMultiChoiceItems時,必須指定isCheckedColumn。如何在使用Sqlite光標時更新對話框中的setMultiChoiceItems複選框
問題如other sites所述,當用戶從列表中選擇一項時,複選框不會更新。有些人建議每次用戶選擇一個項目時更新SqLite表格,但這在我的應用程序中不起作用。這是我想出的解決方案。
這是我想出了:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int myDialogChoice = getArguments().getInt("whichDialog");
mSelectedItems = new ArrayList(); // Where we track the selected items
mCurrentFavoritesSelection = new ArrayList();
myDataBaseAdapter = new AthleteDbAdapter(getActivity());
// int myAthleteId;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
switch(myDialogChoice) {
case Select_From_Favorites:
myCursorFromSqLite = myDataBaseAdapter.fetchAllFavorites(getActivity());
// You need a Primative Boolean Array to specify which items were selected last time.
boolean[] booleanPrimativeArray = new boolean[myCursorFromSqLite.getCount()];
final ArrayList mArrayListOfIDs = new ArrayList();
ArrayList<Boolean> myBooleanList = new ArrayList<Boolean>();
// This array will be the choices that appear in the Dialog.
ArrayList<String> mArrayListOfNames = new ArrayList<String>();
myCursorFromSqLite.moveToFirst();
/* Populate Arrays
*
*/
int iCount = 0;
while(!myCursorFromSqLite.isAfterLast()) {
// put _id's from SqLite data into an array.
mArrayListOfIDs.add(Integer.valueOf(
myCursorFromSqLite.getString(myCursorFromSqLite.getColumnIndex(KEY_ROWID))));
// put series of booleans into Primative Array depending upon whether user selected them last time.
if(Integer.valueOf(myCursorFromSqLite.getString(myCursorFromSqLite.getColumnIndex("checked"))) == 1){
booleanPrimativeArray[iCount] = true;
mSelectedItems.add(
Integer.valueOf(myCursorFromSqLite.getString(myCursorFromSqLite.getColumnIndex(KEY_ROWID)))
);
// I kept track of what selections from last time were.
mCurrentFavoritesSelection.add(
Integer.valueOf(myCursorFromSqLite.getString(myCursorFromSqLite.getColumnIndex(KEY_ROWID)))
);
} else booleanPrimativeArray[iCount] = false;
iCount++;
mArrayListOfNames.add(myCursorFromSqLite.getString(myCursorFromSqLite.getColumnIndex("fullName")));
myCursorFromSqLite.moveToNext();
}
// Change the ArrayList of names to a Char Sequence
CharSequence[] charSeqOfNames = mArrayListOfNames.toArray(new CharSequence[mArrayListOfNames.size()]);
try{
myCursorFromSqLite.close();
} catch (Throwable t) {
Log.e(APP_TAG,"Error closing myCursorFromSqLite Cursor " + t);
}
builder.setTitle(R.string.pick_athletes)
.setMultiChoiceItems(charSeqOfNames, booleanPrimativeArray,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, build an array containing the selected items _id's.
mSelectedItems.add((Integer) mArrayListOfIDs.get(which));
} else if (mSelectedItems.contains((Integer) mArrayListOfIDs.get(which))) {
// Else, if the user changes his mind and de-selects an item, remove it
mSelectedItems.remove((Integer) mArrayListOfIDs.get(which));
}
}
})
// Set the action buttons
.setPositiveButton(R.string.pullathletesbutton, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// User clicked OK, so save the mSelectedItems results somewhere
// or return them to the component that opened the dialog
Log.d(APP_TAG,"Call something");
mListener.onDialogPositiveClick(PickListDialog.this, mSelectedItems, mCurrentFavoritesSelection);
}
})
.setNegativeButton(R.string.cancelbutton, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
});
這行之有效。用戶可以在不影響底層數據庫的情況下改變主意,並正確更新複選標記。一旦用戶完成他的選擇,他點擊「正面」按鈕並更新數據庫。
這正是我所需要的,謝謝。我實現了它有點不同壽。在DialogInterface.OnMultiChoiceClickListener()的onClick中,我直接更新了底層表格,當我完成選擇項目時,我通過表格並執行需要使用選中值的操作!但名稱和布爾數組的工作就像一個魅力! – Larphoid 2014-04-28 12:34:05
良好的延伸,Laphoid。很高興我能夠提供幫助。 – PhillipOReilly 2014-05-20 04:35:28