我必须在listview上显示已保存的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;
}
}
此代码有什么问题?
答案 0 :(得分: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;
}
其中columns
是包含String[]
names of all the columns in your database table.
这样您将获得所有列,然后您可以选择要从哪个列读取数据。
希望这有帮助。
答案 1 :(得分:0)
您只需要一个适配器到您的列表中。在你的情况下,我认为 SimpleCursorAdapter 是最好的。
答案 2 :(得分: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;
}
}
试试这个。