0

Hi I just want to know better on how to use this. Since I'm using this kind of method in doing my custom listView now I wanted to apply it since I love the approach on this but I don't know how I should do it. Anyway What I have is a database query where I get all the result from a search query in SQLite Database. Now I'm sure about the content of my query since I already tested it but when I display it the result always return the same output which is the last row of the query. For better understanding here's my code:

public ArrayList<HashMap<String,String>> getList(String search_param){
        SQLiteDatabase db = this.getWritableDatabase();

        ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
        HashMap<String,String> hashmap = new HashMap<String,String>();

        String query_select = "SELECT column1, column2 FROM tablename WHERE column2 LIKE '%"+ search_param +"%';";

        Cursor cursor = db.rawQuery(query_select,null);
        if (cursor.moveToFirst()) {
            do {
                item_list.put("column1", cursor.getString(0));
                item_list.put("column2",cursor.getString(1));
                list.add(hashmap);
            } while (cursor.moveToNext());
        }
        cursor.close();

        return list;
    }

now to retrieve the list here's what I tried so far:

 ArrayList<HashMap<String,String>> new_list;
 DatabaseClass db = new DatabaseClass(getActivity());

 new_list = db.getList("");
 for(int i=0;i<new_list.size();i++){
            HashMap<String,String> content = new HashMap<String, String>();
            content = new_list.get(i);
            Log.v("The hashmap",content.get("column1").toString());
            Log.v("The hashmap",content.get("column2").toString());
        }

now the content of my Database should be 1,2,3 for column1 and test1,test2,test3 for column2. but what I get from the result are 3,3,3 and test3,test3,test3

I also tried the method of doing newlist.get(i).get("column1").toString; but it doesn't even solve the problem.

Any idea on what I should do about this?

4

2 回答 2

1

在你需要为 hashmap 创建实例的同时,

public ArrayList<HashMap<String,String>> getList(String search_param){
        SQLiteDatabase db = this.getWritableDatabase();

        ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
        HashMap<String,String> hashmap;

        String query_select = "SELECT column1, column2 FROM tablename WHERE column2 LIKE '%"+ search_param +"%';";

        Cursor cursor = db.rawQuery(query_select,null);
        if (cursor.moveToFirst()) {
            do {
                hashMap = new HashMap<String,String>();
                hashMap.put("column1", cursor.getString(0));
                hashMap.put("column2",cursor.getString(1));
                list.add(hashmap);
            } while (cursor.moveToNext());
        }
        cursor.close();

        return list;
    }
于 2013-10-16T11:18:49.927 回答
1

这对我很有魅力

public ArrayList<HashMap<String, String>> getList(String search_param){
        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        String query_select = "SELECT column1, column2 FROM tablename WHERE column2 LIKE '%"+ search_param +"%';";

        Cursor cursor = db.rawQuery(query_select,null);


        int colCount = cursor.getColumnCount();
        int rowCount = cursor.getCount();
        if (cursor.moveToFirst()) {
            do {
                HashMap<String, String> col = new HashMap<String, String>();
                int size =cursor.getColumnCount();
                for (int i = 0; i < size; i++) {
                    col.put(cursor.getColumnName(i), cursor.getString(i));
                }
                list.add(col);
            } while (cursor.moveToNext());
        }
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        return list;
    }
于 2013-10-16T11:25:07.663 回答