如何将HashMap与变量一起使用? Java sqlite

时间:2016-03-27 15:03:20

标签: java android sqlite

我有两个来自数据库(sqlite)的变量(nom和marc)。我需要用HashMap显示这些变量。我尝试了以下但是它不起作用:

private void populateList(String nom, String marc) {
    list = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> temp = new HashMap<String, String>();
    temp.put(FIRST_COLUMN, nom);
    temp.put(SECOND_COLUMN, marc);
    temp.put(THIRD_COLUMN, "1");
    list.add(temp);
}

我做错了什么?

在按钮的单击事件中,我使用游标从数据库中获取数据:

Cursor c = db.rawQuery("Select * from prod where id_prod = " + id, null);
if (c.moveToFirst()){
    do {
        String nom = c.getString(1);
        String marc = c.getString(2);

        populateList(nom, marc);
    } while(c.moveToNext());
}

正如您所看到的,PopulateList是一种不同于我需要发送变量的按钮的方法。也许我做错了。任何答案都会有帮助。感谢

1 个答案:

答案 0 :(得分:0)

每次调用populateList()方法时,都会创建要渲染的列表的新实例! 尝试删除

 list = new ArrayList<HashMap<String, String>>();
来自populateList(...)