android - 使用 Sqlite Cursor 时如何让对话框中的 setMultiChoiceItems 复选框更新

标签 android dialog cursor

使用游标实现 setMultiChoiceItems 时,您必须指定一个 isCheckedColumn。

问题,如 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) {

                }
            });

这很有效。用户可以在不影响底层数据库的情况下改变主意,并且复选标记会正确更新。一旦用户完成了他的选择,他点击“确定”按钮并更新数据库。

关于android - 使用 Sqlite Cursor 时如何让对话框中的 setMultiChoiceItems 复选框更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14865242/

相关文章:

android - 如何更改 Android 对话框背后的颜色

android setOnClickListener 给出空指针异常?

mysql - 返回计算结果集的存储过程

java - 在(Espresso)Android 仪器测试中启动特定的导体 Controller

android - 无法在 Android 设备上运行 Flash 视频播放器

android - Enter 事件不会在使用 Angular 的 Android 设备上触发该功能

mysql游标更新位置索引与循环计数

android - Sqlite 数据库应该只创建一次

android - 如何从自定义对话框中删除标题?

c - 我代码中的\b 不会使光标返回