【问题标题】:Android & SQLite : how to display the multiple column valueAndroid & SQLite:如何显示多列值
【发布时间】: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;


}

}

显示(布局不正确,因为我还没有设置它.. :))

the display

【问题讨论】:

    标签: android sqlite cursor tostring


    【解决方案1】:

    你应该做你自己的 ArrayAdapter。您可以为您的项目提供自己的布局。

    因此您可以将任何列放置在您想要的位置。

    仅仅加入 toString 中的所有字段并不是“高科技”。 在简单的情况下可能就足够了

    希望以下链接对您有所帮助 http://www.vogella.de/articles/AndroidListView/article.html#listview_adapterintro

    【讨论】:

      【解决方案2】:

      您将comment 两次添加到列表中

          comments.add(comment);
          comment.setTicket(cursor.getString(1));
          comments.add(comment);
      

      旁注:你可以简化

      cursor.moveToFirst();
      while (!cursor.isAfterLast()) {
          // do stuff
          cursor.moveToNext();
      }
      

      到这里

      while (cursor.moveToNext()) {
          // do stuff
      }
      

      您只看到名称的实际问题是您使用了错误的适配器。您需要创建自己的。如果有理由将查询结果转换为数组,则基于ArrayAdapter - 或直接使用CursorAdapter

      here 是您可以使用的示例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-30
        • 2019-01-07
        • 2011-09-15
        • 2015-08-14
        • 1970-01-01
        相关资源
        最近更新 更多