【问题标题】:Display sqlite data on listview在 listview 上显示 sqlite 数据
【发布时间】:2013-09-13 15:03:23
【问题描述】:

我必须在列表视图上显示保存的 sqlite 数据。但是只有一列的数据就足够了。这是我的代码。我想获取“tarih”列的数据。

public class TarihDataSource {

private SQLiteDatabase database;
private DatabaseOlusturma dbOlustur;
private String []allColumns = {"tarih","gunluknotu","resim"};

public TarihDataSource(Context context) {
    dbOlustur = new DatabaseOlusturma(context);
}

public void open() throws SQLException {
    database = dbOlustur.getReadableDatabase();
}

public void close() {
    dbOlustur.close();
}

public List<TarihListHelper> getAllTarih() {
    List<TarihListHelper> tarihlistesi = new ArrayList<TarihListHelper>();
    Cursor cursor = database.query("gunluker", allColumns, null, null,
            null, null, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        TarihListHelper comment = cursorToComment(cursor);
        tarihlistesi.add(comment);
        cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return tarihlistesi;
}

private TarihListHelper cursorToComment(Cursor cursor) {
    TarihListHelper comment = new TarihListHelper();
    comment.setTarih(cursor.getString(1));
    return comment;
  }

}

这段代码有什么问题?

【问题讨论】:

    标签: android sqlite android-listview


    【解决方案1】:

    这段代码应该对你有所帮助。

    public List<TarihListHelper> getAllTarih()
    {       
        List<TarihListHelper> tarihlistesi = new ArrayList<TarihListHelper>();
    
        Cursor cursor = database.query(DB_NAME, columns, null, null, null, null, null);
    
        while(cursor.moveToNext())
        {
            TarihListHelper tarihlistesi= cursorToComment(cursor);
            tarihlistesi.add(comment);
    
    
        }
    
        cursor.close();
        return tarihlistesi;
    }
    

    其中columnsString[],其中包含names of all the columns in your database table.

    这样你会得到所有的列,然后你可以选择从哪一列中读取数据。

    希望这会有所帮助。

    【讨论】:

    • TarihListHelper 数据 = cursor.getString(c.getColumnIndex(DB_COMMENT_COL)); cmets.add(数据); // 您将传递给 ListView 以显示数据的 List 我不明白这个 row.cursor.getString 返回 String 类型。但是数据的类型是 TarihListeHelper
    • TarihListHelper 数据 = cursor.getType(cursor.getColumnIndex("tarih"));这给出了错误。
    • 您愿意发布 logcat 吗?
    • 不,不接受。 cursor.getType(...) 返回整数。但是我们有 TarihListHelper 类型。
    • type 将getType返回的数据转换成TarihListHelper
    【解决方案2】:

    您只需要一个适配器到您的列表中。在您的情况下,我认为 SimpleCursorAdapter 是最好的。

    检查this official pagethis example

    【讨论】:

      【解决方案3】:
      public class TarihDataSource {
      
      private SQLiteDatabase database;
      private DatabaseOlusturma dbOlustur;
      private final Context context;
      private String []allColumns = {"tarih","gunluknotu","resim"};
      
      public TarihDataSource(Context _context) {
          context = _context;
          dbOlustur = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
      
      public void open() throws SQLException {
          database = dbOlustur.getReadableDatabase();
      }
      
      public void close() {
          dbOlustur.close();
      }
      
      public List<TarihListHelper> getAllTarih() {
          List<TarihListHelper> tarihlistesi = new ArrayList<TarihListHelper>();
          Cursor cursor = database.query("gunluker", allColumns, null, null,
                  null, null, null);
          cursor.moveToFirst();
          while (!cursor.isAfterLast()) {
              TarihListHelper comment = cursorToComment(cursor);
              tarihlistesi.add(comment);
              cursor.moveToNext();
          }
          // Make sure to close the cursor
          cursor.close();
          return tarihlistesi;
      }
      
      private TarihListHelper cursorToComment(Cursor cursor) {
          TarihListHelper comment = new TarihListHelper();
          comment.setTarih(cursor.getString(1));
          return comment;
        }
      
      }
      

      试试这个。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多