Why there is only one data in the List<String>?

感情迁移 提交于 2020-01-25 12:30:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!