Listview与警告对话框中的复选框

时间:2022-04-10 09:14:44

I'm displaying the contacts in a Multi-select "Ok, Cancel" dialog box. I've implemented Filterable for the adapter that displays the contacts in the dialog. The problem is, once I try and select (check) a contact while I'm using a type ahead, the check box in that particular position is checked and not the contact.

我在多选“确定,取消”对话框中显示联系人。我已经为在对话框中显示联系人的适配器实现了Filterable。问题是,一旦我在前面使用类型时尝试选择(检查)联系人,则会检查该特定位置的复选框,而不是联系人。

The initial screen goes like

初始屏幕就像

After type-ahead,

提前输入后,

When I hit backspace, to see the original list, the selected contact is not checked.

当我点击退格键时,要查看原始列表,不会选中所选联系人。

This is my activity.

这是我的活动。

Cursor c = getContentResolver().query(People.CONTENT_URI,
    PROJECTION,
    null,
    null,
    Contacts.People.DEFAULT_SORT_ORDER
);

startManagingCursor(c);
ListAdapter adapter1 = new ContactListAdapter(this, c);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = (View) inflater.inflate(R.layout.list_view, null);
listView = (ListView) view.findViewById(R.id.contactlist);
listView.setTextFilterEnabled(true);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(adapter1);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnItemSelectedListener(this);
alertDialog.setView(view);

The adapter goes like:

适配器如下:

public class ContactListAdapter extends CursorAdapter implements Filterable
{
    public static final String[] PEOPLE_PROJECTION = new String[] {
        People._ID,
        People.NAME,
        People.NUMBER
    };

    public ContactListAdapter(Context context, Cursor c) {
        super(context, c);
        mContent = context.getContentResolver();
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        final LayoutInflater inflater = LayoutInflater.from(context);
        final TextView view = (TextView) inflater.inflate(
            android.R.layout.simple_list_item_multiple_choice,
            parent,
            false
        );
        view.setText(cursor.getString(1));
        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        ((TextView) view).setTag(cursor.getLong(0));
        ((TextView) view).setText(cursor.getString(1));
    }

    @Override
    public String convertToString(Cursor cursor) {
        return cursor.getString(1);
    }

    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
        if (getFilterQueryProvider() != null) {
            return getFilterQueryProvider().runQuery(constraint);
        }

        StringBuilder buffer = null;
        String[] args = null;
        if (constraint != null) {
            buffer = new StringBuilder();
            buffer.append("UPPER(");
            buffer.append(Contacts.ContactMethods.NAME);
            buffer.append(") GLOB ?");
            args = new String[] { constraint.toString().toUpperCase() + "*" };
        }

        return mContent.query(Contacts.People.CONTENT_URI,
            PEOPLE_PROJECTION,
            buffer == null ? null : buffer.toString(),
            args,
            Contacts.People.DEFAULT_SORT_ORDER
        );
    }

    private ContentResolver mContent;

}

1 个解决方案

#1


0  

"When I hit backspace, to see the original list, the selected contact is not checked."

“当我点击退格键时,要查看原始列表,不会选中所选联系人。”

Try set for your dialog.setOnCancleListener

尝试为dialog.setOnCancleListener设置

and on onCancel call adapter.notifyDataSetChanged()

并在onCancel上调用adapter.notifyDataSetChanged()

maybe it's help

也许这是有帮助的

#1


0  

"When I hit backspace, to see the original list, the selected contact is not checked."

“当我点击退格键时,要查看原始列表,不会选中所选联系人。”

Try set for your dialog.setOnCancleListener

尝试为dialog.setOnCancleListener设置

and on onCancel call adapter.notifyDataSetChanged()

并在onCancel上调用adapter.notifyDataSetChanged()

maybe it's help

也许这是有帮助的