从 Sqlite 填充 Listview 以进行过滤

Populating Listview from Sqlite for Filtering

我正在尝试在我的应用程序中实现带有编辑文本的过滤列表,但未能成功。目前,我可以使用预定义的元素字符串填充列表,但是当我根据 SQL 查询的游标结果创建数组时,我无法获取该数组来填充列表。

当我尝试 运行 activity 时,我遇到了一个空指针异常,我知道空指针是什么以及它们是如何工作的,但是对于我来说我无法弄清楚这个一.

填充数组

        // Predefined List element
    String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE",
            "iPhone 4S", "Samsung Galaxy Note 800",
            "Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"};



    lv = (ListView) findViewById(R.id.list_view);
    inputSearch = (EditText) findViewById(R.id.inputSearch);

    //calling to database to get elements
    List<String> array = new ArrayList<String>();
    Cursor cursor = db.getAllRecipes();
    cursor.moveToFirst();
    while (cursor.moveToNext())
    {
        String name = cursor.getString(cursor.getColumnIndex("recipe_name"));
        array.add(name);
    }

    // Adding items to listview
    adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.recipeName, array);
    lv.setAdapter(adapter);

数据库查询

    //get all recipes
public Cursor getAllRecipes()
{
    return db.query(RECIPE_TABLE, new String[] {KEY_ID, KEY_RECIPE_NAME}, null, null,null, null, null);
}

错误

01-31 19:01:01.876 6305-6305/com.example.rory.prototypev2 E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference
01-31 19:01:01.876 6305-6305/com.example.rory.prototypev2 E/AndroidRuntime:     at com.example.rory.prototypev2.DBMain.getAllRecipes(DBMain.java:242)
01-31 19:01:01.876 6305-6305/com.example.rory.prototypev2 E/AndroidRuntime:     at com.example.rory.prototypev2.dynamicSearch.onCreate(dynamicSearch.java:51)

数据库初始化

DBMain db = new DBMain(this);

显然,当您调用 getAllRecipes() 时,您的 db 为空。

我看不到你在哪里声明db。我希望看到这样的内容:

DatabaseHelper db = new DatabaseHelper(this); 
//^^I've guessed the name "Database Helper", and that it takes a Context argument^^
db.getAllRecipes();

此外,如果 getAllRecipes() 是扩展 SQLiteOpenHelper 的 class 中的一个方法,那么您可能想写:

public Cursor getAllRecipes() {
    return this.getReadableDatabase().query(RECIPE_TABLE, new String[] {KEY_ID, KEY_RECIPE_NAME}, null, null,null, null, null);
}

相反。

空指针总是让我找到 XML 中元素的 id,所以也可以检查那里。但是对于列表视图的简单游标适配器,请尝试以下

带有数据库查询结果的游标

        db.open();

    db.getAllRecipes();

    Cursor cursor2 = db.getAllRecipes();

    //String[] columns = column names from database
    String[] columns = new String[] {column names from database};

    int[] to = new int[] {R.id.'id for XML element'};

    final SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,R.layout.'layout for individual list item', cursor2, columns, to, 0);
    //myCursorAdapter.getFilter().filter();
    lv.setAdapter(myCursorAdapter);
    ListView lv = (ListView) findViewById(R.id.list_view);
    lv.setAdapter(myCursorAdapter);

//更新列表视图 inputSearch.addTextChangedListener(新的 TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            myCursorAdapter.getFilter().filter(cs);
        }