我试图从数据库中检索所有数据并将其放入arraylist但我只获得一个表记录。这是我的代码:
where Birthdates is arraylist.
public ArrayList<String> read()
{
Birthdates.clear();
String selectQuery = "SELECT * FROM Birthday_Reminder";
Cursor crs=database.rawQuery(selectQuery,null);
System.out.println("In read");
if(crs.moveToFirst())
{
do {
Toast.makeText(con,"adding to arraylist", Toast.LENGTH_LONG).show();
Birthdates.add(crs.getString(crs.getColumnIndex("B_Date")));
}
while (crs.moveToNext());
}
return Birthdates;
}
答案 0 :(得分:0)
我通过使用getter和setter
这样做package com.sunil.smilee;
public class DataOfUser {
int _id;
String _data;
String _date;
// Empty constructor
public DataOfUser(){
}
// constructor
// getting ID
public int getID(){
return this._id;
}
// setting id
public void setID(int id){
this._id = id;
}
// getting name
public String getD(){
return this._data;
}
// setting name
public void setD(String s){
this._data = s;
}
//
public void setDate(String string) {
// TODO Auto-generated method stub
this._date = string;
}
public String getDate(){
return this._date ;
}
}
// Add it in database class or change accordingly
// Getting All Contacts
public List<DataOfUser> getAllContacts() {
List<DataOfUser> dist = new ArrayList<DataOfUser>();
// Select All Query
String selectQuery = "SELECT * FROM " + ITEM.TABLE_DATA;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
DataOfUser contact = new DataOfUser();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setD(cursor.getString(1));
contact.setDate(cursor.getString(2));
dist.add(contact);//add data in list
} while (cursor.moveToNext());
//
}
// return contact list
return dist;
}