使用 List 填充 ListView
Populate ListView using List
我正在尝试显示我的 sqlite 数据库中的数据。在我的 DatabaseHandler.java 中,我有这个方法:
public List<Item> getAllItemsinList() {
List<Item> itemList = new ArrayList<Item>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_ITEMS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Item item = new Item();
item.set_id(Integer.parseInt(cursor.getString(0)));
item.set_name(cursor.getString(1));
item.set_description(cursor.getString(2));
// Adding contact to list
itemList.add(item);
} while (cursor.moveToNext());
}
// return item list
return itemList;
}
它 returns 类型为 Item
的列表。
在我的MainActivity.java中我有这个方法:
private void populateListViewUsingList(){
List<Item> items;
items = db.getAllItemsinList();
listView = (ListView) findViewById(R.id.listItems);
ArrayAdapter<Item> itemArrayAdapter = new ArrayAdapter<Item>(this,android.R.layout.simple_list_item_1, items);
listView.setAdapter(itemArrayAdapter);
}
列表视图已填充,但是显示的是对象而不是数据库包含的数据。显示的文本示例为 migueld.rivera.catalogapp.Item@42a97320。解析数据的正确方法是什么?谢谢
Example of text being displayed is
migueld.rivera.catalogapp.Item@42a97320. What is the correct way of
parsing data?
ArrayAdapter 获取对象列表并从 List.get
方法调用项目 return 上的 toString 方法。请参阅此处 getView 实施来自 :
T item = getItem(position);
if (item instanceof CharSequence) {
text.setText((CharSequence)item);
} else {
text.setText(item.toString());
}
所以,
选项 1: 覆盖 Item
class 中的 toString 方法 return Item 对象的可读字符串表示
@Override
public String toString() {
return String.format(name + "---" + description);
}
选项 2: 通过扩展 ArrayAdapter 创建自定义适配器:
发生这种情况是因为您直接显示了该对象。在适配器的 getview 方法中以这种方式尝试
Items item = getItem(position);
textView.setText(item.get_name()+....+item.get_description);
使用您在 class
中声明的适当方法名称
此处显示的自定义列表视图适配器示例
ListView with custom adapter
我正在尝试显示我的 sqlite 数据库中的数据。在我的 DatabaseHandler.java 中,我有这个方法:
public List<Item> getAllItemsinList() {
List<Item> itemList = new ArrayList<Item>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_ITEMS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Item item = new Item();
item.set_id(Integer.parseInt(cursor.getString(0)));
item.set_name(cursor.getString(1));
item.set_description(cursor.getString(2));
// Adding contact to list
itemList.add(item);
} while (cursor.moveToNext());
}
// return item list
return itemList;
}
它 returns 类型为 Item
的列表。
在我的MainActivity.java中我有这个方法:
private void populateListViewUsingList(){
List<Item> items;
items = db.getAllItemsinList();
listView = (ListView) findViewById(R.id.listItems);
ArrayAdapter<Item> itemArrayAdapter = new ArrayAdapter<Item>(this,android.R.layout.simple_list_item_1, items);
listView.setAdapter(itemArrayAdapter);
}
列表视图已填充,但是显示的是对象而不是数据库包含的数据。显示的文本示例为 migueld.rivera.catalogapp.Item@42a97320。解析数据的正确方法是什么?谢谢
Example of text being displayed is migueld.rivera.catalogapp.Item@42a97320. What is the correct way of parsing data?
ArrayAdapter 获取对象列表并从 List.get
方法调用项目 return 上的 toString 方法。请参阅此处 getView 实施来自 :
T item = getItem(position);
if (item instanceof CharSequence) {
text.setText((CharSequence)item);
} else {
text.setText(item.toString());
}
所以,
选项 1: 覆盖 Item
class 中的 toString 方法 return Item 对象的可读字符串表示
@Override
public String toString() {
return String.format(name + "---" + description);
}
选项 2: 通过扩展 ArrayAdapter 创建自定义适配器:
发生这种情况是因为您直接显示了该对象。在适配器的 getview 方法中以这种方式尝试
Items item = getItem(position);
textView.setText(item.get_name()+....+item.get_description);
使用您在 class
中声明的适当方法名称此处显示的自定义列表视图适配器示例 ListView with custom adapter