【发布时间】:2015-01-18 05:27:58
【问题描述】:
我试图在fragments 中显示来自数据库的listview 数据。出于这个原因,我在数据库中添加了一个表。现在我正在尝试创建数据适配器。这是我的代码:
list=(ListView)rootView.findViewById(R.id.listView1);
try{
DatabaseHandler db = new DatabaseHandler(getActivity());
List<Label> allLabel = db.getAllLabels();
for (Label label : allLabel) {
ArrayAdapter<Label> dataadapter= new ArrayAdapter<Label>(getActivity(),android.R.layout.simple_list_item_1,allLabel);
list.setAdapter(dataadapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,View v, int position,long id)
{
Toast.makeText(getActivity(), ((TextView)v).getText(), Toast.LENGTH_LONG).show();
}
});
}
}
catch (Exception e)
{
e.printStackTrace();
}
这是我的输出包含字段值而不是数据值:
com.qcash.atmlocator.Label@52852d88
com.qcash.atmlocator.Label@52852dd8
com.qcash.atmlocator.Label@52852e08
com.qcash.atmlocator.Label@52852e38
这是我的代码中使用的 getAllLabels() 函数:
public List<Label> getAllLabels(){
List<Label> labels = new ArrayList<Label>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_LABELS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Label label = new Label();
label.setId(cursor.getInt(0));
label.setName(cursor.getString(1));
label.setLatitude(cursor.getDouble(2));
label.setLongitude(cursor.getDouble(3));
labels.add(label);
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}
如何获取数据库值?请帮帮我。
【问题讨论】:
标签: android sqlite android-fragments android-listview android-arrayadapter