android 如何为 ArrayList 设置 notifySetDataChanged()
android how to set notifySetDataChanged() for an ArrayList
当我从另一个 activity 返回到 activity 时,我正在尝试刷新 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
,并且有效!谢谢
adapter.notifyDataSetChanged()
用于告诉ListView
数据集中的项发生了变化。
您需要修改实际数据集才能使其具有任何视觉效果,因为只要调用该方法,就会在调用该方法前后显示相同的值。
如果您想保留这种方法,您需要在 onResume
中再次查询数据库 以重新加载数据。 然后调用adapter.notifyDataSetChanged()
.
当您使用 onBackPressed() 返回时,onCreate 不会被调用;您需要将所有带有设置数据的逻辑放在 onResume() 方法中的适配器
1) 要刷新您的列表,您需要清除数据 (names
),然后从数据库重新加载它。
2) onResume()
方法被频繁调用。为了获得更好的性能,我建议您在从孩子的活动中更新数据时改用 onActivityResult()
。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// refresh your list here.
}
在这种情况下,不要忘记使用 startActivityForResult()
而不是 startActivity()
。
我建议使用 SimpleCursorAdapter
而不是 SimpleAdapter
。当您的数据库中的数据发生变化时,它会为您处理所有细节。
当我从另一个 activity 返回到 activity 时,我正在尝试刷新 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
,并且有效!谢谢
adapter.notifyDataSetChanged()
用于告诉ListView
数据集中的项发生了变化。
您需要修改实际数据集才能使其具有任何视觉效果,因为只要调用该方法,就会在调用该方法前后显示相同的值。
如果您想保留这种方法,您需要在 onResume
中再次查询数据库 以重新加载数据。 然后调用adapter.notifyDataSetChanged()
.
当您使用 onBackPressed() 返回时,onCreate 不会被调用;您需要将所有带有设置数据的逻辑放在 onResume() 方法中的适配器
1) 要刷新您的列表,您需要清除数据 (names
),然后从数据库重新加载它。
2) onResume()
方法被频繁调用。为了获得更好的性能,我建议您在从孩子的活动中更新数据时改用 onActivityResult()
。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// refresh your list here.
}
在这种情况下,不要忘记使用 startActivityForResult()
而不是 startActivity()
。
我建议使用 SimpleCursorAdapter
而不是 SimpleAdapter
。当您的数据库中的数据发生变化时,它会为您处理所有细节。