带有自定义光标适配器的Android SearchView小部件

时间:2017-03-19 18:42:12

标签: android listview searchview android-cursoradapter

美好的一天,

我已经为我的listview实现了一个自定义游标adpater。我还有一个与listview相关联的searchview小部件。我在从onCreateOptionsMenu实现搜索小部件时遇到问题,然后执行搜索。这是我到目前为止所拥有的。

 @Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.activity_flavors_actions, menu);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

    mCursor = mDbHelper.getflavorlist(null);
    final FlavorListAdapter adapter = new FlavorListAdapter(this, mCursor);
    adapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            return mDbHelper.getflavorlist(constraint.toString());
        }
    });

    return super.onCreateOptionsMenu(menu);

}

这是我的光标适配器

public class FlavorListAdapter extends CursorAdapter {
public FlavorListAdapter(Context context, Cursor cursor) {
    super(context, cursor, 0);
}

// The newView method is used to inflate a new view and return it,
// you don't bind any data to the view at this point.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.flavorlist, parent, false);
}

// The bindView method is used to bind all data to a given view
// such as setting the text on a TextView.
@Override
public void bindView(View view, Context context, Cursor cursor) {
    // Find fields to populate in inflated template
    TextView nametext = (TextView) view.findViewById(R.id.name);
    TextView vendortext = (TextView) view.findViewById(R.id.vendor);
    TextView notestext = (TextView) view.findViewById(R.id.cnotes);
    // Extract properties from cursor
    String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
    String vendor = cursor.getString(cursor.getColumnIndexOrThrow("vendor"));
    String notes = cursor.getString(cursor.getColumnIndexOrThrow("notes"));
    // Populate fields with extracted properties
    nametext.setText(name);
    vendortext.setText(vendor);
    notestext.setText(notes);
}
}

数据库助手

 public Cursor getflavorlist (CharSequence constraint)  {
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
    queryBuilder.setTables(
            DATABASE_TABLE2
    );

    String asColumnsToReturn[] = {KEY_ROWID,KEY_NAME,VENDOR,BASE,BASEV,PERCT,NOTES,GRAVITY,INVENTORY};

    if (constraint == null  ||  constraint.length () == 0)  {
        //  Return the full list
        return queryBuilder.query(mDb, asColumnsToReturn, null, null,
                null, null, KEY_NAME);
    }  else  {
        String value = ""+constraint.toString()+"%";

        return mDb.query(DATABASE_TABLE2, asColumnsToReturn, "name like ?", new String[]{value}, null, null, null);
    }
}

代码运行时我没有收到任何错误。搜索小部件可以工作,但列表不会过滤。我在这里缺少什么?

0 个答案:

没有答案