[英]Android API ListView doesn't update when using notifyDataSetChanged();
I'm sorry to write about what seems to be a common problem, but I've already been on this one task for five hours, and I simply give up. 我很遗憾地写了一个似乎是常见问题的内容,但我已经完成了这个任务五个小时了,我只是放弃了。
I'm making a small android app, all it's supposed to do right now is keep a small SQLite database and be able to write/edit notes on it. 我正在制作一个小型的Android应用程序,它现在应该做的就是保留一个小的SQLite数据库,并能够在其上编写/编辑注释。 Everything works fine with SQL, my problem is with the ListView.
一切都适用于SQL,我的问题是ListView。 No matter what I do, it just refuses to update when I add a new note.
无论我做什么,它只是在我添加新笔记时拒绝更新。 The only way to view the new note is when I enter edit mode in another and come back out.
查看新笔记的唯一方法是当我在另一个笔记中进入编辑模式并返回时。
Here's the relevant part of the source: 以下是来源的相关部分:
public class MainActivity extends Activity {
ArrayAdapter<NoteItem> mAdapter;
public static NoteDataBase ndb;
ListView listView;
ArrayList a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ndb = new NoteDataBase(this);
a = ndb.getAllNotes();
//LISTVIEW DO NOT TOUCH
/***********/ mAdapter = new ArrayAdapter<NoteItem>(this,
/***********/ android.R.layout.simple_list_item_1, a);
/***********/ listView = (ListView) findViewById(R.id.listView1);
/***********/ listView.setAdapter(mAdapter);
//*****************
}
//New Note button
public void newNoteAction(View view){
ndb.addNote(new NoteItem("Title", "Contents"));
a = ndb.getAllNotes();
mAdapter = new ArrayAdapter<NoteItem>
(this, android.R.layout.simple_list_item_1, a);
mAdapter.notifyDataSetChanged();
System.out.println(ndb.getAllNotes().size());
}
The print at the end is just to see if it's actually creating the new note. 最后的印刷品只是为了看它是否真的创造了新的音符。
I hope I gave enough info, tell me if you need anything else. 我希望我提供足够的信息,告诉我你是否需要其他任何东西。 I tried almost everything that I read already, but after almost five hours on one task, there's a point where I'm gonna go crazy :)
我几乎尝试了所有我读过的东西,但是经过一个任务差不多五个小时之后,有一点我会发疯:)
Thanks, 谢谢,
Shef SHEF
public void newNoteAction(View view){
ndb.addNote(new NoteItem("Title", "Contents"));
// don't do this
// you are creating a new adapter and not binding it back to listview
// a = ndb.getAllNotes();
// mAdapter = new ArrayAdapter<NoteItem>(this, android.R.layout.simple_list_item_1, a);
mAdapter.add(new NoteItem("Title", "Contents"));
mAdapter.notifyDataSetChanged();
System.out.println(ndb.getAllNotes().size());
}
Looks like the problem is that you're creating a new instance of ArrayAdapter
and adding to it, which isn't associated with the ListView
. 看起来问题是你正在创建一个新的
ArrayAdapter
实例并添加到它,它与ListView
无关。
notifyDataSetChanged()
will send events to whatever is listening - the ListView
is listening to the old adapter. notifyDataSetChanged()
将事件发送到正在侦听的内容 - ListView
正在侦听旧适配器。
It seems like you wanted to update the adapter but instead you're creating a new adapter. 看起来你想要更新适配器,而是你要创建一个新的适配器。 Here, if you just call
listView.setAdapter(mAdapter)
again, it would set this new adapter to the ListView
and it would work. 在这里,如果你再次调用
listView.setAdapter(mAdapter)
,它会将这个新的适配器设置为ListView
,它会工作。
But creating a new adapter every time isnt a good way to do it. 但是每次创建一个新的适配器都不是一个好方法。 Since you're using a database, try reading up on CursorAdapter .
由于您正在使用数据库,请尝试阅读CursorAdapter 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.