【问题标题】:Listview repeating informationListview 重复信息
【发布时间】:2014-05-12 14:16:39
【问题描述】:

我正在尝试使用 2 个数据库列进行列表视图。我成功了,但我的问题是只有数据库的第一个信息出现在列表视图中。如果我在数据库中有 x 数据,它将在列表视图中显示 x 行,只有第一个条目。

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
data = new ArrayList();
    Cursor cursor = mydb.rawQuery("SELECT * FROM projeto", null);

    if (cursor.moveToFirst()) {
        Toast.makeText(getApplicationContext(), "Caminhos:", 3000).show();
        data.clear();
        do {
            data.add(cursor.getString(cursor.getColumnIndex("id_proj")));
            data.add(cursor.getString(cursor.getColumnIndex("nome")));
            map.put("id", data.get(0).toString());
            map.put("nome", data.get(1).toString());
            map.put("hora", "17:00");
            mylist.add(map);
        } while (cursor.moveToNext());


        SimpleAdapter resul = new SimpleAdapter(MainActivity.this, mylist,R.layout.list_item,new String[] {"id", "nome", "hora"}, new int[] {R.id.i, R.id.n, R.id.h});
        lista.setAdapter(resul);

【问题讨论】:

  • 我看到了那个帖子,它没有帮助我:(
  • 你为什么不使用CursorAdapter?跳过将所有数据传输到地图和列表之类的步骤,只需直接使用光标即可。事实上,名为data 的 ArrayList 在这里是没有用的——你添加到它,然后在将值放入映射时从它读取,而你最初可以直接添加到映射中。

标签: android android-listview arraylist simplecursoradapter


【解决方案1】:

我在您的代码中发现了两个问题:

  • 您没有清除数据(如上一个答案中所说),因此您始终会获得前两个值
  • 您总是使用同一张地图。由于地图是一个对象,因此您将始终对其进行修改,并且您的列表将填充一张地图。

要解决这两个问题,试试这个:

ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
data = new ArrayList();
Cursor cursor = mydb.rawQuery("SELECT * FROM projeto", null);

if (cursor.moveToFirst()) {
    Toast.makeText(getApplicationContext(), "Caminhos:", 3000).show();
    do {
        HashMap<String, String> map = new HashMap<String, String>();
        data.clear();
        data.add(cursor.getString(cursor.getColumnIndex("id_proj")));
        data.add(cursor.getString(cursor.getColumnIndex("nome")));
        map.put("id", data.get(0).toString());
        map.put("nome", data.get(1).toString());
        map.put("hora", "17:00");
        mylist.add(map);
    } while (cursor.moveToNext());


    SimpleAdapter resul = new SimpleAdapter(MainActivity.this, mylist,R.layout.list_item,new String[] {"id", "nome", "hora"}, new int[] {R.id.i, R.id.n, R.id.h});
    lista.setAdapter(resul);

【讨论】:

  • 它正在工作,但现在的问题是我在 BD 中有 2 个条目,它显示在列表视图 4 中,它重复了两次。我认为这是因为 map.put() 它在循环内,但如果我取出它只会保存最后一个条目。
  • 算了,我用的是同一个数据库。它工作正常。谢谢你:)
【解决方案2】:

看看这个,

map.put("id", data.get(0).toString());
map.put("nome", data.get(1).toString());
map.put("hora", "17:00");

你总是在地图中放同一个键,总是“id、nome 和 hora”你需要放不同的键。

ps:为什么要创建地图列表?你不需要这样做。

【讨论】:

【解决方案3】:

每次循环都需要清除data。您需要将呼叫转移到 data.clear()do { 循环开始之后。您现在所做的将意味着从光标中检索到第一个项目后,数据将包含 2 个项目。第二次之后它将包含 4,然后是 6,等等。然后您总是阅读相同的两个项目。如果您清除data,它应该可以解决您的问题。

【讨论】:

  • 它没有解决问题
  • 您还需要在每次循环中添加一张新地图,而不是重复使用同一张地图。
猜你喜欢
  • 1970-01-01
  • 2018-12-18
  • 1970-01-01
  • 1970-01-01
  • 2011-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-26
相关资源
最近更新 更多