根据值显示sqlite的数据

时间:2013-12-03 01:23:55

标签: android sqlite listview

我需要你的帮助,我确实在列表视图中显示数据,但问题是我

希望数据是根据特定值,这意味着如果id = 1,只有行

如果您有任何建议,我会非常感谢,

这里是代码:

    public class MainActivity extends ListActivity {

  private static final int FLAG_REGISTER_CONTENT_OBSERVER = 2;
  private Cursor cursor;
  SimpleCursorAdapter adapter = null;
  Cursor c;
   DBAdapter db = new DBAdapter(this);
   @SuppressWarnings("deprecation")
   @Override
  protected void onCreate(Bundle savedInstanceState) {

try {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    db.open();
    populateListViewFromDB();
} catch (Exception e) {
    Log.e("ERROR", "Error occured: " + e.toString());
    e.printStackTrace();
  }
  }
 @SuppressWarnings("deprecation")
 private void populateListViewFromDB() {
 Cursor cursor = db.getAllRecords();
 startManagingCursor(cursor);

 String[] databaseColumnNames = new String[] { DBAdapter.col_region, };
int[] toViewIDs = new int[] { R.id.text };

  SimpleCursorAdapter myCursordapter = new SimpleCursorAdapter(this,R.layout.activity_main, cursor, databaseColumnNames, toViewIDs,FLAG_REGISTER_CONTENT_OBSERVER);
         ListView list = (ListView) findViewById(android.R.id.list);

我的DBAdapter是:

 private static final String MENAGE = "table_MENAGE";

   public static final String _id = "Num_du_Questionnaire";

 public Cursor getAllRecords() {
 return db.query(MENAGE, new String[] { _id, col_region,
         }, null, null, null,
          null, null);
  }
          list.setAdapter(myCursordapter);
     }  }

1 个答案:

答案 0 :(得分:1)

您可以查看query文档,函数接受selectionselectionArgs参数,对应于SQL WHERE子句。

因此,要使查询仅限于特定ID,请使用:

db.query(MENAGE, new String[] { _id, col_region}, "id = ?", new String[] {_id}, null, null, null);