从android中的数据库读取到arraylist
我对android开发非常陌生,不知道如何从从android中的数据库读取到arraylist,android,sqlite,arraylist,Android,Sqlite,Arraylist,我对android开发非常陌生,不知道如何从数据库/表读取arrayList。现在我有: Cursor c = myDB.rawQuery("SELECT * FROM " + TableName , null); int Column1 = c.getColumnIndex("Field1"); int Column2 = c.getColumnIndex("Field2"); // Check if our result was va
数据库/表
读取arrayList。现在我有:
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 of Calllog class objects
ArrayList<CallLog> arrCallLogs = null;
Cursor curCalllog = myDB.rawQuery("SELECT * FROM " + TableName , null);
if(curCalllog != null && curCalllog.moveToFirst()) {
arrCallLogs = new ArrayList<CallLog>();
while (curCalllog.isAfterLast() == false) {
//Calllog is a class with list of fileds
CallLog callLog = new CallLog();
callLog.setId(curCalllog.getLong(curCalllog.getColumnIndex(KEY_ROWID)));
callLog.setName(curCalllog.getString(curCalllog.getColumnIndex(KEY_NAME)));
callLog.setNumber(curCalllog.getString(curCalllog.getColumnIndex(KEY_NUMBER)));
callLog.setNumberType(curCalllog.getInt(curCalllog.getColumnIndex(KEY_NUMBERTYPE)));
callLog.setCallType(curCalllog.getInt(curCalllog.getColumnIndex(KEY_CALLTYPE)));
callLog.setDatetime(curCalllog.getLong(curCalllog.getColumnIndex(KEY_DATETIME)));
callLog.setDuration(curCalllog.getLong(curCalllog.getColumnIndex(KEY_DURATION)));
callLog.set_new(curCalllog.getInt(curCalllog.getColumnIndex(KEY_NEW)));
arrCallLogs.add(callLog);
curCalllog.moveToNext();
}
}
curCalllog.close();
myDB.close();
如何将其读入arraylist //调用日志类对象的ArrayList
//ArrayList of Calllog class objects
ArrayList<CallLog> arrCallLogs = null;
Cursor curCalllog = myDB.rawQuery("SELECT * FROM " + TableName , null);
if(curCalllog != null && curCalllog.moveToFirst()) {
arrCallLogs = new ArrayList<CallLog>();
while (curCalllog.isAfterLast() == false) {
//Calllog is a class with list of fileds
CallLog callLog = new CallLog();
callLog.setId(curCalllog.getLong(curCalllog.getColumnIndex(KEY_ROWID)));
callLog.setName(curCalllog.getString(curCalllog.getColumnIndex(KEY_NAME)));
callLog.setNumber(curCalllog.getString(curCalllog.getColumnIndex(KEY_NUMBER)));
callLog.setNumberType(curCalllog.getInt(curCalllog.getColumnIndex(KEY_NUMBERTYPE)));
callLog.setCallType(curCalllog.getInt(curCalllog.getColumnIndex(KEY_CALLTYPE)));
callLog.setDatetime(curCalllog.getLong(curCalllog.getColumnIndex(KEY_DATETIME)));
callLog.setDuration(curCalllog.getLong(curCalllog.getColumnIndex(KEY_DURATION)));
callLog.set_new(curCalllog.getInt(curCalllog.getColumnIndex(KEY_NEW)));
arrCallLogs.add(callLog);
curCalllog.moveToNext();
}
}
curCalllog.close();
myDB.close();
ArrayList arrCallLogs=null;
游标curCalllog=myDB.rawQuery(“SELECT*FROM”+TableName,null);
if(curCalllog!=null&&curCalllog.moveToFirst()){
arrCallLogs=newarraylist();
while(curCalllog.isAfterLast()==false){
//Calllog是一个包含文件列表的类
CallLog CallLog=new CallLog();
setId(curCalllog.getLong(curCalllog.getColumnIndex(KEY_ROWID));
setName(curCalllog.getString(curCalllog.getColumnIndex(KEY_NAME));
setNumber(curCalllog.getString(curCalllog.getColumnIndex(KEY_NUMBER));
callLog.setNumberType(curCalllog.getInt(curCalllog.getColumnIndex(KEY_NUMBERTYPE));
callLog.setCallType(curCalllog.getInt(curCalllog.getColumnIndex(KEY_CALLTYPE));
setDatetime(curCalllog.getLong(curCalllog.getColumnIndex(KEY_DATETIME));
setDuration(curCalllog.getLong(curCalllog.getColumnIndex(KEY_DURATION));
callLog.set_new(curCalllog.getInt(curCalllog.getColumnIndex(KEY_new));
arrCallLogs.add(callLog);
curCalllog.moveToNext();
}
}
curCalllog.close();
myDB.close();
这是如何从游标创建arrayList的示例
//ArrayList of Calllog class objects
ArrayList<CallLog> arrCallLogs = null;
Cursor curCalllog = myDB.rawQuery("SELECT * FROM " + TableName , null);
if(curCalllog != null && curCalllog.moveToFirst()) {
arrCallLogs = new ArrayList<CallLog>();
while (curCalllog.isAfterLast() == false) {
//Calllog is a class with list of fileds
CallLog callLog = new CallLog();
callLog.setId(curCalllog.getLong(curCalllog.getColumnIndex(KEY_ROWID)));
callLog.setName(curCalllog.getString(curCalllog.getColumnIndex(KEY_NAME)));
callLog.setNumber(curCalllog.getString(curCalllog.getColumnIndex(KEY_NUMBER)));
callLog.setNumberType(curCalllog.getInt(curCalllog.getColumnIndex(KEY_NUMBERTYPE)));
callLog.setCallType(curCalllog.getInt(curCalllog.getColumnIndex(KEY_CALLTYPE)));
callLog.setDatetime(curCalllog.getLong(curCalllog.getColumnIndex(KEY_DATETIME)));
callLog.setDuration(curCalllog.getLong(curCalllog.getColumnIndex(KEY_DURATION)));
callLog.set_new(curCalllog.getInt(curCalllog.getColumnIndex(KEY_NEW)));
arrCallLogs.add(callLog);
curCalllog.moveToNext();
}
}
curCalllog.close();
myDB.close();
这里有一个类名Calllog,它包含文件、setter和getter方法的列表。
这里我使用游标创建了一个Calllog对象的ArrayList,下面是一段代码,它向您展示了如何从数据库查询中获取ArrayList
//ArrayList of Calllog class objects
ArrayList<CallLog> arrCallLogs = null;
Cursor curCalllog = myDB.rawQuery("SELECT * FROM " + TableName , null);
if(curCalllog != null && curCalllog.moveToFirst()) {
arrCallLogs = new ArrayList<CallLog>();
while (curCalllog.isAfterLast() == false) {
//Calllog is a class with list of fileds
CallLog callLog = new CallLog();
callLog.setId(curCalllog.getLong(curCalllog.getColumnIndex(KEY_ROWID)));
callLog.setName(curCalllog.getString(curCalllog.getColumnIndex(KEY_NAME)));
callLog.setNumber(curCalllog.getString(curCalllog.getColumnIndex(KEY_NUMBER)));
callLog.setNumberType(curCalllog.getInt(curCalllog.getColumnIndex(KEY_NUMBERTYPE)));
callLog.setCallType(curCalllog.getInt(curCalllog.getColumnIndex(KEY_CALLTYPE)));
callLog.setDatetime(curCalllog.getLong(curCalllog.getColumnIndex(KEY_DATETIME)));
callLog.setDuration(curCalllog.getLong(curCalllog.getColumnIndex(KEY_DURATION)));
callLog.set_new(curCalllog.getInt(curCalllog.getColumnIndex(KEY_NEW)));
arrCallLogs.add(callLog);
curCalllog.moveToNext();
}
}
curCalllog.close();
myDB.close();
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;
}
public ArrayList GetAllValues(字符串aTable,字符串[]aColumn)
{
ArrayList=新建ArrayList();
Cursor Cursor=sqlDB.query(aTable,aColumn,null,null,null,null);
if(cursor.moveToFirst())
{
做
{
list.add(cursor.getString(0));
}
while(cursor.moveToNext());
}
if(cursor!=null&!cursor.isClosed())
{
cursor.close();
}
退货清单;
}
如果在“aColumn”中提供null,它将提供所有列。我希望这会有帮助
//ArrayList of Calllog class objects
ArrayList<CallLog> arrCallLogs = null;
Cursor curCalllog = myDB.rawQuery("SELECT * FROM " + TableName , null);
if(curCalllog != null && curCalllog.moveToFirst()) {
arrCallLogs = new ArrayList<CallLog>();
while (curCalllog.isAfterLast() == false) {
//Calllog is a class with list of fileds
CallLog callLog = new CallLog();
callLog.setId(curCalllog.getLong(curCalllog.getColumnIndex(KEY_ROWID)));
callLog.setName(curCalllog.getString(curCalllog.getColumnIndex(KEY_NAME)));
callLog.setNumber(curCalllog.getString(curCalllog.getColumnIndex(KEY_NUMBER)));
callLog.setNumberType(curCalllog.getInt(curCalllog.getColumnIndex(KEY_NUMBERTYPE)));
callLog.setCallType(curCalllog.getInt(curCalllog.getColumnIndex(KEY_CALLTYPE)));
callLog.setDatetime(curCalllog.getLong(curCalllog.getColumnIndex(KEY_DATETIME)));
callLog.setDuration(curCalllog.getLong(curCalllog.getColumnIndex(KEY_DURATION)));
callLog.set_new(curCalllog.getInt(curCalllog.getColumnIndex(KEY_NEW)));
arrCallLogs.add(callLog);
curCalllog.moveToNext();
}
}
curCalllog.close();
myDB.close();