【发布时间】:2020-11-01 03:00:37
【问题描述】:
我想在 Android Studio 的列表视图中显示所有数据库内容,我希望看到 3 行,每行包含 "name, family and ID" ,但我看到了一个复杂的包名称和一些其他字符如下:
com.google.www.hmdbtest01.Person@529e71b4
com.google.www.hmdbtest01.Person@529e7238
com.google.www.hmdbtest01.Person@529e7298
如果我的数据库中有更多行,我会在输出中看到更多类似上面的行。
我的代码如下:
public class ListOfData extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_of_data);
ListView list = findViewById(R.id.list1);
HmDbManager01 db= new HmDbManager01(this);
ArrayList personList = db.getAll();
ArrayAdapter<ArrayList> arrayList = new ArrayAdapter<ArrayList>(this,
android.R.layout.simple_list_item_1, personList);
list.setAdapter(arrayList);
}
}
db.getAll()如下:
public ArrayList<Person> getAll(){
SQLiteDatabase sqLiteDatabase = this.getReadableDatabase();
Cursor cursor= sqLiteDatabase.rawQuery("SELECT * FROM tbl_person", null);
cursor.moveToFirst();
ArrayList<Person> allData = new ArrayList<>();
if(cursor.getCount() > 0){
while (!cursor.isAfterLast()){
Person p1 = new Person();
p1.pID=cursor.getString(0);
p1.pName=cursor.getString(1);
p1.pFamily=cursor.getString(2);
allData.add(p1);
cursor.moveToNext();
}
}
cursor.close();
sqLiteDatabase.close();
return allData;
}
而且,这是人:
package com.google.www.hmdbtest01;
public class Person {
public String pID;
public String pName;
public String pFamily;
}
让我知道你的 cmets 解决这个问题。
【问题讨论】: