简体   繁体   中英

Displaying sqlite database info into a listview Android

So far I can make my information display in a toast message. I would like some advice/help how to display this information into a listview. this is the intent to view the assignments. ( I think, I'm new to android )

public void viewAssignments(View v)
{
Intent i = new Intent(this, AssignmentTracker.class);
startActivity(i);
}

this is how the toast i have works:

public void DisplayRecord(Cursor c)
{
    Toast.makeText(this, 
            "id: " + c.getString(0) + "\n" +
            "Title: " + c.getString(1) + "\n" +
            "Due Date:  " + c.getString(2)  + "\n" +
                    "Notes:  " + c.getString(4),
            Toast.LENGTH_SHORT).show();        
} 

Hopefully that's enough information so someone can advice me. thanks. This is how i am trying to display my info

private void displayResultList(Cursor c) {

    String results =  "id: " + c.getString(0) + "\n" +
            "Title: " + c.getString(1) + "\n" +
            "Due Date:  " + c.getString(2)  + "\n" +
            "Notes:  " + c.getString(4);

    final ListAdapter workoutsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Integer.parseInt(results));
    ListView workouts = (ListView) findViewById(R.id.listViewFromDatabase);
    workouts.setAdapter(workoutsAdapter);

}

and oncreate in my class this is called:

   db.open();
    Cursor c = db.getAllRecords();
    if (c.moveToFirst())
    {
        do {
            displayResultList(c);
        } while (c.moveToNext());
    }
    db.close();

This is pretty simple task and you can accomplish it using CursorAdapter, I would suggest you to Study CursorAdapter so you could better understand what is actually happening

Here is the a simple example

SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(Context, //Context of Application
            R.layout.layoutXML, // Layout for  simple textviews for your items 
            cursor, // Cursor for getting the data from Database 
            columns, // Names of columns you want to get data from
            to, 0);

    final ListView listView = (ListView) findViewById(R.id.listView);
    // Assign adapter to ListView
    listView.setAdapter(dataAdapter);

Its a simple example

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM