2012-12-29 41 views
1

我的ListView目前只能顯示一列數據。我希望它顯示兩列數據。如何顯示ListView上同一個表中的多個列數據?

這裏是我的ListView代碼:

public class ProjectExplorer extends ListActivity { 

private projectdatabase database; 
protected Cursor cursor; 
protected ListAdapter adapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    database = new projectdatabase(ProjectExplorer.this); 

    getinfo(); 
} 

private void getinfo() { 

    database.open(); 
    cursor = database.getDataforDisplay(); 
    adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, new String[] {"project_name"}, new int[] { android.R.id.text1 }, 0); 
    setListAdapter(adapter); 

} 

@Override 
public void onListItemClick(ListView parent, View view, int position, long id) { 

    super.onListItemClick(parent, view, position, id); 

    Cursor c = ((SimpleCursorAdapter)parent.getAdapter()).getCursor(); 
     c.moveToPosition(position); 

     // get project name here 

     String str_projectname= c.getString(c.getColumnIndex("project_name")); 

     Toast.makeText(this, str_projectname, Toast.LENGTH_LONG).show(); 


}  

這裏是數據庫類返回光標的方法:

public Cursor getDataforDisplay() {  

    String[] columns = new String[] {KEY_ROWID, PROJECT_NAME, PROJECT_FINISH_DATE, PROJECT_DIFFICULTY, PROJECT_STATUS}; 
    Cursor c = projectDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 

    projectDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 

    c.moveToFirst(); 
    return c; 

} 

我也想顯示KEY_ROWID其定義爲_id

回答

3

您可以使用android.R.layout.simple_list_item_2指定可從中檢索數據的表中的第2列,並指定可以在其幫助下顯示的位置活動 - android.R.id.text2

adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, new String[] {"project_name","column_no2"}, new int[] { android.R.id.text1, andriod.R.id.text2 }, 0); 

您也可以創建自定義適配器指定自己的ListView和從無到有,定製一切創造它。

1

至少在我看來,最好的方法是爲您的ListView創建自定義適配器,在這種情況下,您可以設置自己的設計如何使用單個listview元素。只需使用Cursor填充一些陣列,並將它們設置爲您的自定義適配器。

相關問題