从数据库中读取的机器人到一个ArrayList [英] Read from a database in android to an arraylist
本文介绍了从数据库中读取的机器人到一个ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是很新的Android的开发和无法弄清楚如何从我的数据库/表
读入的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();
}
我如何读入一个ArrayList呢?
How do i read this into an arraylist?
推荐答案
下面是一个code这表明你如何从数据库中查询得到的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.
这篇关于从数据库中读取的机器人到一个ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文