如何在android listview中使用simple_list_item_2?

时间:2011-05-12 18:48:08

标签: android

我如何扩展我的示例以使用两行listview(例如从数据库中读出电子邮件)?

    ArrayList<String> db_results = new ArrayList<String>();
    Cursor c = db.rawQuery("SELECT lastname, firstname FROM contacts ORDER BY lastname",null);
    while(c.moveToNext()) {
        db_results.add(String.valueOf(c.getString(c.getColumnIndex("lastname"))) + ", " + String.valueOf(c.getString(c.getColumnIndex("firstname"))));
    }
    c.close();
    lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,db_results));

谢谢...

1 个答案:

答案 0 :(得分:4)

为什么不使用SimpleCursorAdapter?这是一个例子。

// SimpleListAdapter is designed for binding to a Cursor.
         ListAdapter adapter = new SimpleCursorAdapter(
                 this, // Context.
                 android.R.layout.two_line_list_item,  // Specify the row template to use (here, two columns bound to the two retrieved cursor
 rows).
                 mCursor,                                              // Pass in the cursor to bind to.
                 new String[] {People.NAME, People.COMPANY},           // Array of cursor columns to bind to.
                 new int[] {android.R.id.text1, android.R.id.text2});  // Parallel array of which template objects to bind to those columns.

         // Bind to our new adapter.
         setListAdapter(adapter);