数组适配器显示字段值,而不是 android 中数据库的数据值
array adapter is showing field value, not data values from database in android
我正在尝试显示 fragments
数据库中的 listview
数据。出于这个原因,我在数据库中添加了一个 table 。现在我正在尝试创建数据适配器。这是我的代码:
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;
}
如何获取数据库值?请帮帮我。
覆盖 Label class 中的 toString() 方法。我假设你 class 有一个变量 'name'.
class Label{
// other variables.
@Override
public String toString() {
return name;
}
}
更新:
更改 onItemclick 侦听器
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,View v, int position,long id)
{
Label label = (Label)parent.getItemAtPosition(position);
Toast.makeText(getActivity(), "" + label.toString(), Toast.LENGTH_LONG).show();
}
});
将游标数据添加到字符串并将该字符串添加到调用函数片段解决了这个问题。这是代码:
String name=cursor.getString(0)+"\n"+cursor.getString(1)+"\n"+cursor.getString(2)+"\n"+cursor.getString(3);
FindPeopleFragment.ArrayofName.add(name);
最终将该数组显示给适配器完成:
ArrayAdapter<String> dataadapter= new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,ArrayofName);
我正在尝试显示 fragments
数据库中的 listview
数据。出于这个原因,我在数据库中添加了一个 table 。现在我正在尝试创建数据适配器。这是我的代码:
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;
}
如何获取数据库值?请帮帮我。
覆盖 Label class 中的 toString() 方法。我假设你 class 有一个变量 'name'.
class Label{
// other variables.
@Override
public String toString() {
return name;
}
}
更新:
更改 onItemclick 侦听器
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,View v, int position,long id)
{
Label label = (Label)parent.getItemAtPosition(position);
Toast.makeText(getActivity(), "" + label.toString(), Toast.LENGTH_LONG).show();
}
});
将游标数据添加到字符串并将该字符串添加到调用函数片段解决了这个问题。这是代码:
String name=cursor.getString(0)+"\n"+cursor.getString(1)+"\n"+cursor.getString(2)+"\n"+cursor.getString(3);
FindPeopleFragment.ArrayofName.add(name);
最终将该数组显示给适配器完成:
ArrayAdapter<String> dataadapter= new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,ArrayofName);