【问题标题】:android how to set notifySetDataChanged() for an ArrayListandroid如何为ArrayList设置notifySetDataChanged()
【发布时间】:2016-05-15 19:00:10
【问题描述】:

当我从另一个活动返回活动时,我正在尝试刷新 ListView 内容。当我进一步返回主应用程序屏幕并返回时,它只会刷新内容。

这是我的 Activity,我在其中设置了 ListView,它是适配器:

public static final String ID = "Id";
public static final String iidd = "iidd";
private static final String DESC = "Description";

private AppCompatDelegate delegate;
Cursor c;
ListView listFood;
SimpleAdapter myAdapter;
ArrayList<Map<String, String>> names = new ArrayList<Map<String, String>>();
Integer i_d;
Integer iddd;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activityfood_main);

    listFood = (ListView) findViewById(R.id.listViewF);
    listFood.setOnItemClickListener(viewFoodListener);

    // database read
    DatabaseConnectorFood db = new DatabaseConnectorFood(this);
    db.open();
    c = db.getFoodData();

    while(c.moveToNext()) {
        Map<String, String> data = new HashMap<String, String>(2);
        data.put("Description", c.getString(c.getColumnIndex("Description")));
        data.put("Serving_Size", c.getString(c.getColumnIndex("Serving_Size")));
        names.add(data);
    }
    db.close();

    myAdapter = new SimpleAdapter(this, names, R.layout.list_food,
            new String[] {"Description", "Serving_Size"},
            new int[] {R.id.ViewFoodNotesOne, R.id.ViewFoodNotesTwo});

    listFood.setAdapter(myAdapter);
}

我尝试在onResume() 下添加它,但它不起作用

@Override
protected void onResume() {
    super.onResume();

    myAdapter.notifyDataSetChanged();
}

编辑:我按照以下答案的建议进行了如下操作,但没有结果:

@Override
protected void onResume() {
    super.onResume();

    DatabaseConnectorFood db = new DatabaseConnectorFood(this);
    db.open();
    c = db.getFoodData();

    while(c.moveToNext()) {
        Map<String, String> data = new HashMap<String, String>(2);
        data.put("Description", c.getString(c.getColumnIndex("Description")));
        data.put("Serving_Size", c.getString(c.getColumnIndex("Serving_Size")));
        names.add(data);
    }
    db.close();

    myAdapter = new SimpleAdapter(this, names, R.layout.list_food,
            new String[] {"Description", "Serving_Size"},
            new int[] {R.id.ViewFoodNotesOne, R.id.ViewFoodNotesTwo});

    // Set the Adapter into SimpleCursorAdapter
    listFood.setAdapter(myAdapter);

    myAdapter.notifyDataSetChanged();
}

已解决: 将names.clear(); 添加到此处的代码中,最重要的是,按照@Rami 的建议首先清除ListView,它可以工作!谢谢

【问题讨论】:

  • 您需要在调用 notifyDataSetChanged() 之前更新您的适配器数据。
  • 我在提到的下一个活动中更新 SQLite 项目,其中我有一些带有保存按钮的 EditTexts,保存按钮被发送到我已添加到原始帖子中的 DatabaseConnector 的 UpdateNoteFood()
  • 更新适配器数据与更新 SQLite 数据库不同。您需要清除您的 names 列表并从数据库中重新加载数据。

标签: android listview arraylist adapter


【解决方案1】:

adapter.notifyDataSetChanged() 用于告诉ListView 数据集中的项目发生了变化。

您需要修改实际数据集以使其具有任何视觉效果,因为只需调用该方法,就会在调用该方法之前和之后显示相同的值。

如果您想保持这种方法,您需要在onResume 中再次查询数据库 以重新加载您的数据。 然后致电adapter.notifyDataSetChanged()。

【讨论】:

    【解决方案2】:

    当您使用 onBackPressed() 返回时不会调用 onCreate;您需要在 onResume() 方法中将所有逻辑设置数据设置为适配器

    【讨论】:

      【解决方案3】:

      1) 要刷新列表,您需要清除数据 (names),然后从数据库中重新加载。

      2) onResume() 方法被频繁调用。为了获得更好的性能,我建议您改用onActivityResult(),同时从孩子的活动中更新您的数据。

      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
           // refresh your list here.
      
      }
      

      在这种情况下,不要忘记使用startActivityForResult() 而不是startActivity()。

      【讨论】:

        【解决方案4】:

        我建议使用SimpleCursorAdapter 而不是SimpleAdapter。当数据库中的数据发生变化时,它将为您处理所有细节。

        【讨论】:

          猜你喜欢
          • 2012-12-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多