我有一个SQLite数据库,其中包含一个名为author
的表。我正在尝试提取id
和name
值并将它们传递给MainActivity,其中id
将存储为变量,name
将显示在ListView中。
但这就是日志(和我的ListView)的外观:
[com.example.apple.bookshelf.Author@31e44c75,
com.example.apple.bookshelf.Author@24fe640a,
com.example.apple.bookshelf.Author@2c87d47b,
com.example.apple.bookshelf.Author@2ab52298,
com.example.apple.bookshelf.Author@393a3af1,
com.example.apple.bookshelf.Author@2326b6d6]
我可以看到问题是因为我将id
和name
添加到AuthorList数组,但没有指定要在ListView中显示哪一个。但是我该怎么做?
DatabaseHelper类的片段:
public List<String> getAllAuthors() {
List authorList = new ArrayList();
// Select all query
String selectQuery = "SELECT * FROM " + AUTHORS + " ORDER BY name_alphabetic";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Author author = new Author();
author.setID(Integer.parseInt(cursor.getString(0)));
author.setName(cursor.getString(1));
authorList.add(author);
} while (cursor.moveToNext());
}
// return author list
return authorList;
}
作者类:
public class Author {
int id;
String name;
String name_alphabetic;
public Author() {
}
public Author(int id, String name, String name_alphabetic) {
this.id = id;
this.name = name;
this.name_alphabetic = name_alphabetic;
}
// getters
public int getID() {
return this.id;
}
public String getName() {
return this.name;
}
public String getNameAlphabetic() {
return this.name_alphabetic;
}
// setters
public void setID(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setNameAlphabetic(String name_alphabetic) {
this.name_alphabetic = name_alphabetic;
}
}
来自MainActivity的片段:
// connect authorsListView variable to XML ListView
authorsListView = (ListView) findViewById(R.id.authors_list_view);
// create new database helper
DatabaseHelper db = new DatabaseHelper(this);
// create new list through getAllAuthors method (in DatabaseHelper class)
List authorList = db.getAllAuthors();
Log.i("authors", authorList.toString());
// create new Array Adapter
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, authorList);
// link ListView and Array Adapter
authorsListView.setAdapter(arrayAdapter);
修改
我使用下面评论中的this thread解决了问题。将此代码添加到我的Author类以覆盖从toString()
类继承的Object
方法可以解决问题:
@Override
public String toString() {
return name;
}
答案 0 :(得分:0)
您并没有真正使用Author
课程。我建议您执行以下操作:
将您的List
更改为List<Author>
:
public List<Author> getAllAuthors() {
List<Author> authorList = new ArrayList<>();
// Select all query
String selectQuery = "SELECT * FROM " + AUTHORS + " ORDER BY name_alphabetic";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Author author = new Author();
author.setID(Integer.parseInt(cursor.getString(0)));
author.setName(cursor.getString(1));
authorList.add(author);
} while (cursor.moveToNext());
}
// return author list
return authorList;
}
然后在MainActivity.java
中更改ArrayAdapter
:
ArrayAdapter<Author> arrayAdapter =
new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, authorList);
确保toString()
Author
返回按您希望的方式格式化的字符串。