2011-05-12 70 views
1

我该如何扩展我的例子以使用双行列表视图(例如从数据库中读出电子邮件)?如何在android listview中使用simple_list_item_2?

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

谢谢...

回答

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

嗨,谢谢,我试过了,但是我遇到了一个问题,即字段_id是必需的。这对我来说不是解决方案,因为我只有字段ID(它在我的桌面应用程序中使用,并且无法更改)。有没有办法让我的代码上面有2列? – fillibuster 2011-05-12 21:29:53

+1

对不起。这是一个简单的解决方案:SELECT id as _id – fillibuster 2011-05-12 21:48:35

相关问题