【问题标题】:CursorAdapter implements FilterableCursorAdapter 实现 Filterable
【发布时间】:2013-01-25 15:06:50
【问题描述】:

我有一个使用字母索引器的自定义 CursorAdapter 类,我想实现可过滤,但在让它正常运行时遇到了一些问题。

    c = db.selectData();


    lv = (ListView) findViewById(android.R.id.list);
    searchTerm = (EditText) findViewById(R.id.inputSearch);

    lv.setFastScrollEnabled(true);

    adapter = new MyCursorAdapter(getApplicationContext(), R.layout.rowlayout, c, new String[]{"_id, name, company, job_title"},
            new int[]{R.id.company, R.id.job_title, R.id.name});

    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            // Search for states whose names begin with the specified letters.
            Cursor cursor = db.searchAttendees(constraint.toString());
            return cursor;

        }
    });

    lv.setAdapter(adapter);

    getListView().setTextFilterEnabled(true);

    searchTerm.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            System.out.println("Text ["+s+"]");
            adapter.getFilter().filter(s.toString());
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

  }

这是 MyCustomAdapter 类

        public class MyCursorAdapter extends CursorAdapter  implements SectionIndexer, Filterable
{

    AlphabetIndexer mAlphabetIndexer;

    public MyCursorAdapter(Context context, int simpleListItem1,
            Cursor cursor, String[] strings, int[] is)
    {
        super(context, cursor);
        mAlphabetIndexer = new AlphabetIndexer(cursor,
                cursor.getColumnIndex("name"),
                " ABCDEFGHIJKLMNOPQRTSUVWXYZ");
        mAlphabetIndexer.setCursor(cursor);//Sets a new cursor as the data set and resets the cache of indices.

    }

    /**
     * Performs a binary search or cache lookup to find the first row that matches a given section's starting letter.
     */
    @Override
    public int getPositionForSection(int sectionIndex)
    {
        return mAlphabetIndexer.getPositionForSection(sectionIndex);
    }

    /**
     * Returns the section index for a given position in the list by querying the item and comparing it with all items
     * in the section array.
     */
    @Override
    public int getSectionForPosition(int position)
    {
        return mAlphabetIndexer.getSectionForPosition(position);
    }

    /**
     * Returns the section array constructed from the alphabet provided in the constructor.
     */
    @Override
    public Object[] getSections()
    {
        return mAlphabetIndexer.getSections();
    }

    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
        //What to do here?

    }

    /**
     * Bind an existing view to the data pointed to by cursor
     */
    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView name = (TextView)view.findViewById(R.id.name);
        name.setText(cursor.getString(
                cursor.getColumnIndex("name")));
        TextView company = (TextView)view.findViewById(R.id.company);
        company.setText(cursor.getString(
                cursor.getColumnIndex("company")));
        TextView jobTitle = (TextView)view.findViewById(R.id.job_title);
        jobTitle.setText(cursor.getString(
                cursor.getColumnIndex("job_title")));
    }

    /**
     * Makes a new view to hold the data pointed to by cursor.
     */
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View newView = inflater.inflate(
                R.layout.attendee_item_row, parent, false);
        return newView;
    }

我知道我需要使用 runQueryOnBackgroundThread,但我不知道我打算在这个方法中实现什么。有人可以在这里帮助我吗?或者指出一些例子的方向?

谢谢!

【问题讨论】:

    标签: android android-listview android-cursoradapter


    【解决方案1】:

    我知道我需要使用 runQueryOnBackgroundThread 但我不知道什么 我打算用这种方法实现。

    如果您在CursorAdapter 上设置了FilterQueryProvider,那么您不会覆盖该方法(默认情况下调用FilterQueryProviderrunQuery 方法)。当您想要过滤适配器时,只需调用 getFilter().filter(constraint) 方法,为其赋予新的约束。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-19
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      相关资源
      最近更新 更多