从Sqlite填充Listview以进行过滤

时间:2016-01-31 19:04:47

标签: android android-sqlite android-arrayadapter

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

当我尝试运行活动时,我面临一个空指针异常,我知道什么是空指针以及它们是如何工作的,但对于我的生活,我无法弄清楚这个。

填充数组

        // 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);

3 个答案:

答案 0 :(得分:0)

当您致电db时,显然您的getAllRecipes()为空。

答案 1 :(得分:0)

我无法看到您声明db的位置。我希望看到这样的事情:

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

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

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

代替。

答案 2 :(得分:0)

空指针总是让我知道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(new TextWatcher(){

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