获取ArrayList的值<hashmap <string,string>&gt; for display </hashmap <string,string>

时间:2013-10-16 11:12:31

标签: android hashmap

您好我只想更好地了解如何使用它。由于我现在正在使用这种方法进行自定义listView,所以我想应用它,因为我喜欢这个方法,但我不知道应该怎么做。无论如何我拥有的是一个数据库查询,我从SQLite数据库中获取搜索查询的所有结果。现在我确定我的查询内容,因为我已经测试了它,但是当我显示它时,结果总是返回相同的输出,这是查询的最后一行。为了更好地理解这里是我的代码:

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

现在要检索列表,这是我到目前为止所尝试的内容:

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

现在我的数据库的内容对于column1而言应该是1,2,3,对于column2,test1,test2,test3应该是test3。但我得到的结果是3,3,3和test3,test3,test3

我也尝试过这样做的方法 newlist.get(i).get("column1").toString;但它甚至没有解决问题。

关于我应该怎么做的任何想法?

2 个答案:

答案 0 :(得分: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;
    }

答案 1 :(得分: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;
    }