本文介绍了从android中的数据库读取到arraylist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
限时送ChatGPT账号..我对 Android 开发非常陌生,无法弄清楚如何从我的 database/table
读取到 arrayList.现在我有:
I'm very new to android development and can't figure out how to read from my database/table
into an arrayList. Right now I have:
Cursor c = myDB.rawQuery("SELECT * FROM " + TableName , null);
int Column1 = c.getColumnIndex("Field1");
int Column2 = c.getColumnIndex("Field2");
// Check if our result was valid.
c.moveToFirst();
if (c != null) {
// Loop through all Results
do {
String Name = c.getString(Column1);
int Age = c.getInt(Column2);
Data =Data +Name+"/"+Age+"\n";
}while(c.moveToNext());
}
TextView tv = new TextView(this);
tv.setText(Data);
setContentView(tv);
}
catch(Exception e) {
Log.e("Error", "Error", e);
} finally {
if (myDB != null)
myDB.close();
}
如何将其读入数组列表?
How do I read this into an arraylist?
推荐答案
这是一段代码,向您展示了如何从数据库查询中获取 ArrayList.
Here is a code which shows you how to get ArrayList from Database query.
public ArrayList<String> GetAllValues(String aTable,String[] aColumn)
{
ArrayList<String> list = new ArrayList<String>();
Cursor cursor = sqlDB.query(aTable, aColumn, null, null, null, null, null);
if (cursor.moveToFirst())
{
do
{
list.add(cursor.getString(0));
}
while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed())
{
cursor.close();
}
return list;
}
如果您在aColumn"中提供空值,它将为您提供所有列.我希望它会有所帮助.
If you provide null in "aColumn", it will give you all columns. I hope it will help.
这篇关于从android中的数据库读取到arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论