无法在游标适配器上使用setFilterQueryProvider方法

时间:2018-12-01 18:54:57

标签: android listview android-cursoradapter

我想根据从sqlite数据库中选择的时间(选择)将数据加载到不同的listView(相同的活动),但是我不知道为什么我的应用程序不断崩溃... 请有人告诉我下面的代码有什么问题...

在MainActivity.java

    mCursorAdapter = new FoodCursorAdapter(this,null);
    mCursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {
        public Cursor runQuery(CharSequence constraint) {
            return FP.fetchdatabyfilter(constraint.toString(),"time" );
        }
    });
    mCursorAdapter.getFilterQueryProvider().runQuery("D");
    dinner.setAdapter(mCursorAdapter);

在FoodProvider.java

    public Cursor fetchdatabyfilter(String inputText,  String filtercolumn) throws SQLException {
    String selection = filtercolumn + "=?";
    String[] selectionArgs = {inputText};
    String[] projection = {
            FoodEntry._ID,
            FoodEntry.COLUMN_FOOD_NAME,
            FoodEntry.COLUMN_FOOD_CALORIES,
            FoodEntry.COLUMN_FOOD_TIME
    };
    Cursor row = null;
    if (inputText == null  || inputText.length() == 0 ) {
        row = getContext().getContentResolver().query(FoodEntry.CONTENT_URI, projection,null,null,null);
    } else {
        row = getContext().getContentResolver().query(FoodEntry.CONTENT_URI, projection,selection,selectionArgs,null);
    }
    if (row != null)
    {
        row.moveToFirst();
    }
    return row;
}

0 个答案:

没有答案