如何从sqlite中检索所有内容并将其显示在listview中

时间:2011-09-20 04:21:47

标签: android sqlite

  

可能重复:
  Good Tutorial for SQLite and ListView

我创建了一个已插入值的数据库。但我想在listview中检索数据库中的所有内容,我试过但没有什么。有谁可以帮助我?

这是我的代码:

protected void onResume() {
     super.onResume();

     // Configure the listview
     events = new ArrayList<String>();
     listview = (ListView)this.findViewById(R.id.listview);
     listview.setAdapter(new ArrayAdapter<String>(this, 
         android.R.layout.simple_list_item_1, events));



       // Open/create the DB
       database dbopener = new database(this);
       db = dbopener.getWritableDatabase();
       Log.d("CalendarRetrieveMain", "Opened DB");       

    // Re-populate the list
       Cursor itemcursor = db.query("eventCal",
           null, null, null, null, null, null, null);
       int count = itemcursor.getCount();
       Log.d("CalendarRetrieveMain", "Loading DB contents: " + count + " events");
       for (int j = 0; j < count; j++) {
           itemcursor.moveToPosition(j);
           String eventName = itemcursor.getString(1);
           String date = itemcursor.getString(2);
           String time = itemcursor.getString(3);
           String venue = itemcursor.getString(4);
           String eventDescription= itemcursor.getString(5);




       // Get number of items in list, then loop through each one
       int num = listview.getCount();
       for (int i = 0; i < num; i++) {

       event=  eventTitle[i].toString();
       date =  Date[i].toString();
       time = Time[i].toString();
       venue = Venue[i].toString();
       Description= Content[i].toString();


           // Add item to database
           ContentValues values = new ContentValues();
           values.put("event", event);
           values.put("date", date);
           values.put("time", time);
           values.put("venue", venue);
           values.put("eventDescription", Description);
           db.insert("eventCal", null, values);

2 个答案:

答案 0 :(得分:0)

最简单的方法是,通过扩展Simple游标适配器来创建游标适配器,只需要覆盖getView方法就可以将值放入文本中。

关于Stackover流的类似问题的链接是here ..

答案 1 :(得分:0)

String[] columns=new String[]{"event","date","time","venue","eventDescription"};

Cursor itemcursor = db.query("eventCal",
           columns, null, null, null, null, null, null);

实际上问题是你没有传递你想要获取的字段名称