开发者

how to retrieve all content from sqlite and display it in listview [duplicate]

开发者 https://www.devze.com 2023-04-05 20:36 出处:网络
This question already has answers here:开发者_JAVA技巧 Closed 11 years ago. Possible Duplicate: Good Tutorial for SQLite and ListView
This question already has answers here: 开发者_JAVA技巧 Closed 11 years ago.

Possible Duplicate:

Good Tutorial for SQLite and ListView

i have created a database that have already insert value in it. but i want to retrieve all my content in the database in listview, i tried but there were nothing. can anyone help me ?

This is my codes:

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


Simplest way to do is, create your cursor adapter by extending the Simple cursor adapter, you just need to override the getView method to put your values into the text.

Link for similar question on Stackover flow is here..


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

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

actually the problem is that you are not passing the field name which you want to fetch

0

精彩评论

暂无评论...
验证码 换一张
取 消