为什么List <string>中只有一个数据?</string>

时间:2014-05-20 13:33:07

标签: android arraylist

我使用游标进行数据库选择,并希望将所有数据放入List中,然后返回mainactivity。但经过调试后,我发现返回值中只有第一个数据。

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 个答案:

答案 0 :(得分:0)

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;
    }

试试这个。希望这会有所帮助。