DialogFragment / setMultiChoiceItems with Cursor

时间:2014-07-14 02:20:56

标签: android android-dialog android-cursor

我试图在带有光标的DialogFragment中使用setMultiChoiceItems,我遇到的问题是我没有显示任何对话框,我做错了什么?

我的DialogFragment实现如下

public class ContactPickerFragment extends DialogFragment implements
        LoaderCallbacks<Cursor> {
    ArrayList mSelectedItems;
    ArrayList arrayList;
    Cursor listCursor;
    private static Context context;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
        getLoaderManager().initLoader(0, null, this);

    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mSelectedItems = new ArrayList();  
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setTitle("Title")

                .setMultiChoiceItems(listCursor, "ischecked", "fname",
                        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(R.string.ok,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {

                            }
                        })
                .setNegativeButton(R.string.cancel,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {

                            }
                        });

        return builder.create();
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {

        return new ContactListCursorLoader(getActivity());
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        listCursor = cursor;
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        listCursor = null;
    }

    private static class ContactListCursorLoader extends SQLiteCursorLoader {

        public ContactListCursorLoader(Context context) {
            super(context);
        }

        @SuppressLint("NewApi")
        @Override
        protected Cursor loadCursor() {
            return new DBHelper(context).queryContacts();

        }
    }

}

我的光标位于

之下
public ContactCursor queryContacts() {

    Cursor wrapped = getReadableDatabase().query("contacts", null, null, null,
            null, null, "time" + " desc");
    return new ContactCursor(wrapped);
}




public static class ContactCursor extends CursorWrapper {

    public ContactCursor(Cursor c) {
        super(c);
    }

    public Contacts getContacts() {

        if (isBeforeFirst() || isAfterLast())
            return null;

        Contacts contacts = new Contacts();

        contacts.setFname(getLong(getColumnIndex("fname")));


        return contacts;
    }

}

我的数据库就是这个

db.execSQL("create table contacts (_id integer primary key autoincrement, num UNSIGNED big int, fname text, sname text, lname text, ischecked int, time int ) ");

更新1

当我这样做时,它工作正常,

.setMultiChoiceItems( new DBHelper(context).queryContacts(), "ischecked", "fname",

所以现在的问题是如何更新光标&#34; listCursor&#34;装载机完成加载后?

0 个答案:

没有答案