问题
I used cursor for database selecting and want to put all the data inside a List, then return to the mainactivity. But after debugging, I found that there is only the first data inside the return value.
public List<String> select1 (String ReservedState, String mycity, String mystreet, String mypostcode){
ReservedState="Free";
SQLiteDatabase Database1=dbHelper1.getWritableDatabase();
String[] columns={DbHelper1.KEY_ID1,DbHelper1.KEY_RESERVEDSTATE, DbHelper1.KEY_GEOGRAPHICALLOCATION,DbHelper1.KEY_CITY,DbHelper1.KEY_POSTCODE,DbHelper1.KEY_STREET};
Cursor cursor=Database1.query(DbHelper1.TABLE_NAME_1,columns, DbHelper1.KEY_RESERVEDSTATE+"='"+ReservedState+"' AND "+DbHelper1.KEY_CITY+"='"+mycity+"' AND "+DbHelper1.KEY_POSTCODE+"='"+mypostcode+"' AND "+DbHelper1.KEY_STREET+"='"+mystreet+"'",null,null,null,null);
//StringBuffer buffer=new StringBuffer();
List<String> al=new ArrayList<String>();
cursor.moveToFirst();
while(!cursor.isAfterLast()){
int index1=cursor.getColumnIndex(DbHelper1.KEY_ID1);
String id=cursor.getString(index1);
al.add(id);
cursor.moveToNext();
}
return al;
}
回答1:
public List<String> select1 (String ReservedState, String mycity, String mystreet, String mypostcode){
ReservedState="Free";
SQLiteDatabase Database1=dbHelper1.getWritableDatabase();
String[] columns={DbHelper1.KEY_ID1,DbHelper1.KEY_RESERVEDSTATE, DbHelper1.KEY_GEOGRAPHICALLOCATION,DbHelper1.KEY_CITY,DbHelper1.KEY_POSTCODE,DbHelper1.KEY_STREET};
Cursor cursor=Database1.query(DbHelper1.TABLE_NAME_1,columns, DbHelper1.KEY_RESERVEDSTATE+"='"+ReservedState+"' AND "+DbHelper1.KEY_CITY+"='"+mycity+"' AND "+DbHelper1.KEY_POSTCODE+"='"+mypostcode+"' AND "+DbHelper1.KEY_STREET+"='"+mystreet+"'",null,null,null,null);
Log.e("Cursor",""+cursor.getCount());
//StringBuffer buffer=new StringBuffer();
List<String> list=new ArrayList<String>();
do{
int index1=cursor.getColumnIndex(DbHelper1.KEY_ID1);
String id=cursor.getString(index1);
list.add(id);
}while(cursor.moveToNext());
return al;
}
Try this. Hope this will help.
来源:https://stackoverflow.com/questions/23761298/why-there-is-only-one-data-in-the-liststring