0

我必须在列表视图上显示保存的 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;
  }

}

这段代码有什么问题?

4

3 回答 3

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;
}

which 包含columns在哪里String[]names of all the columns in your database table.

这样您将获得所有列,然后您可以选择要从哪个列中读取数据。

希望这可以帮助。

于 2013-09-13T15:09:14.273 回答
0
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;
  }

}

尝试这个。

于 2013-09-13T15:13:17.603 回答
0

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

检查这个官方页面这个例子

于 2013-09-13T15:10:34.913 回答