在Android中显示ListView中的数据
我正在从SQLite检索数据并尝试在ListView中显示它。问题是数据库中有两列,而listview只显示一列中的数据。为此,我使用以下代码在Android中显示ListView中的数据,android,sqlite,listview,Android,Sqlite,Listview,我正在从SQLite检索数据并尝试在ListView中显示它。问题是数据库中有两列,而listview只显示一列中的数据。为此,我使用以下代码 public List<entry> getAllEntries() { List<Entry> entries = new ArrayList<Entry>(); Cursor cursor = database.query(MySQLiteHelper.TABLE_ENTRIES,
public List<entry> getAllEntries() {
List<Entry> entries = new ArrayList<Entry>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_ENTRIES,
allEntries, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Entry entry = cursorToEntry(cursor);
Entry entry1 = cursorToEntry(cursor);
Log.d(TAG, "get entry = " + cursorToEntry(cursor).toString());
entries.add(entry);
cursor.moveToNext();
Log.d(TAG, "get amount = "+ cursorToEntries(cursor).toString1());
entries.add(entry1);
cursor.moveToNext();
}
cursor.close();
return entries;
}
这就是我在列表视图中显示它的方式
List<Entry> values = datasource.getAllEntries();
ArrayAdapter<Entry> adapter = new ArrayAdapter<Entry>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
List values=datasource.getAllEntries();
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,值);
setListAdapter(适配器);
}
您可以使用游标或适配器,它允许您指定行布局以及如何从游标中的列映射到布局中的视图;或者,您可以编写自己的适配器实现,该实现从BaseAdapter扩展而来,并在内部由游标支持。我建议你看。你可以发布你的代码,说明如何显示listview。它只显示一个元素,因为你使用的是预定义的布局。您需要编写自定义适配器或使用simpledapter adapter=new simpledapter(这个,list,android.R.layout.simple_list_item_2,新字符串[]{“name”,“id”},新int[]{android.R.id.text1,android.R.id.text2});//列表应该是hashmap才能正常工作。这里要提到哪个列表?列表,给出一个错误。
List<Entry> values = datasource.getAllEntries();
ArrayAdapter<Entry> adapter = new ArrayAdapter<Entry>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}