2011-04-20 53 views
1

我有一个SQLite数据库,我通过SimpleCursorAdapter从数据库中获取一个ListView。这是现有的代码:数据库到ListView - 筛选出条目

Cursor c = mDatabase.query(
      "database", 
      bla = new String[]{ 
        "_id", 
        "title", 
        "message", 
        "time", 
        "date", 
        "done" 
      }, 
      null, null, null, null, null); 
    startManagingCursor(c); 

    SimpleCursorAdapter listAdapter = 
      new SimpleCursorAdapter(this, 
        R.layout.list_item, c, 
        new String[]{"title", "message", "time", "date"}, 
        new int[]{R.id.txtv_title, R.id.txtv_message, R.id.txtv_time, R.id.txtv_date} 
      ); 
    setListAdapter(listAdapter); 

这按预期工作,我得到我的ListView。 现在我想如果我的数据库中的“完成”字段包含字符串“false”(而不是“true”...),此行不进入ListView。

我该怎么做? 在此先感谢!

回答

1

这更多的是一个SQL,然后与Android相关。你正在寻找的是一个WHERE子句:

String query = "SELECT _id, title, message, time, date, done "+ 
"FROM database "+ 
"WHERE done = 'true' "+ 
"ORDER BY date"; 

要发送此查询到您的SQLite数据库,你可以使用rawQuery() - 方法:

final Cursor c = db.rawQuery(query, null); 
+0

这是解决了,谢谢您!我甚至没有考虑用查询来做这件事...... – CTrox 2011-04-20 17:53:11

+0

但是,您可以通过查询方法提供位置和顺序,而无需使用rawQuery ...方法查询更适合于保留rawQuery fx u可以存储静态列数组和表格名称所以你可以在一个地方改变它,而不是在每个查询字符串 – Selvin 2011-04-20 18:01:49

+0

我不是简单查询()方法的粉丝,因为我能够使用SQL,所以为什么要使用这些东西来为我做这些事情?当然,你也可以使用这种方法,结果应该是一样的。 – 2011-04-20 18:05:35