【发布时间】:2012-03-24 16:26:27
【问题描述】:
希望大家帮忙解决我的问题
我的问题是无法检索列的多个值。在我的数据库中包含 4 列(id、namaStation、ticket、masa)。现在程序只显示 namaStation 的值。这可能是因为 Comment 类中的 toString() 方法,因为它只返回 1 个值(namaStation)。可以帮帮我吗?
这是我的程序。 ##
1。调用查询。
`public void onClick(查看视图) {
@SuppressWarnings("unchecked")
ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
Comment comment = null;
switch (view.getId()) {
case R.id.add:
List<Comment> values = datasource.query("Kelana Jaya");
adapter = new ArrayAdapter<Comment>(this, android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
break;`
2。查询。
@SuppressWarnings("static-access")
public List<Comment> query(String namaStation){
//List<Comment> comments = new ArrayList<Comment>();
database = dbHelper.getReadableDatabase();
List<Comment> comments = new ArrayList<Comment>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
allColumns, MySQLiteHelper.COLUMN_namaStation+"=?",new String[]{namaStation}, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
// Comment comment = cursorToComment(cursor);
Comment comment = new Comment();
comment.setId(cursor.getLong(0));
comment.setNamaStation(cursor.getString(1));
comments.add(comment);
comment.setTicket(cursor.getString(2));
comments.add(comment);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return comments;
3。评论类
public class Comment {
private long id;
public String namaStation;
public String ticket;
public String masa;
public String comment;
public int x;
public Comment(){
}
public Comment(long id, String namaStation, String ticket, String masa){
this.id=id;
this.namaStation=namaStation;
this.ticket=ticket;
this.masa=masa;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getNamaStation() {
return namaStation;
}
public void setNamaStation(String comment) {
this.namaStation = comment;
}
public String getTicket() {
return ticket;
}
public void setTicket(String comment) {
this.ticket = comment;
}
public String getMasa() {
return masa;
}
public void setMasa(String comment) {
this.masa= comment;
}
// Will be used by the ArrayAdapter in the ListView
public String toString() {
return ticket;
}
}
显示(布局不正确,因为我还没有设置它.. :))
【问题讨论】:
标签: android sqlite cursor tostring