当我从 List<T>
添加或删除对象时,我调用myAdapter.notifyDataSetChanged()
“重新渲染”ListView
,但它不起作用...
Activity :
private ListView lv_course; //Liste des matières
private List<Course> courses = new ArrayList<Course>();
private CustomListAdapterCourse customListAdapterCourse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course);
courses = getDataFromDatabase();
lv_course = (ListView) findViewById(R.id.lv_course);
customListAdapterCourse = new CustomListAdapterCourse(this, courses);
lv_course.setAdapter(customListAdapterCourse);
//...
}
适配器:
private Context context;
private List<Course> courses;
public CustomListAdapterCourse(Context theContext, List<Course> theListCourses) {
super(theContext, 0, theListCourses);
this.context = theContext;
this.courses = theListCourses;
}
在其他 Activity 中,我在数据库中添加数据,并在 onResume()
期间添加数据。 :
@Override
protected void onResume() {
super.onResume();
courses = getDataFromDatabase();
customListAdapterCourse.notifyDataSetChanged();
}
ListView
未更新,但当我关闭应用程序并重新运行它时,它包含我创建的所有对象。
最佳答案
您没有更新适配器的数据,只是更改对对象的引用,但适配器中的引用指向旧对象。
使用新对象实例化新适配器,或者在适配器中添加方法来更改数据。
关于java - ArrayAdapter.notifyDataSetChanged() 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28782445/