【发布时间】:2012-08-11 06:05:52
【问题描述】:
当显示已更新的数据时,如何获取当前的 Android 视图并强制重绘?我通过 Android 的 Notepad tutorial 完成了第三课,没有任何问题——毕竟提供了解决方案——但我被困在我的第一次不平凡的修改上。
我在添加注释按钮旁边的菜单中添加了一个新按钮。当按下该按钮时,该按钮会在系统中每个笔记的标题中添加一个字母。但是,无论我等待多长时间,新标题都不会出现在笔记列表中。我知道更新程序可以工作,因为如果我关闭应用程序并将其重新启动,更改确实会出现。
到目前为止,我发现我必须使用某种失效方法来使程序用新值重绘自身。我知道 invalidate() 用于 UI 线程,postInvalidate() 用于非 UI 线程 1、2,但我什至不知道我在哪个线程中。另外,这两个方法都必须从需要绘图的View 对象中调用,我不知道如何获取该对象。我尝试的一切都返回null。
我的主要课程:
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case INSERT_ID:
createNote();
return true;
case NEW_BUTTON:
expandTitles();
return true;
default:
// Intentionally empty
}
return super.onMenuItemSelected(featureId, item);
}
private void expandTitles() {
View noteListView = null;
// noteListView = findViewById(R.layout.notes_list); // null
// noteListView =
// getWindow().getDecorView().findViewById(android.R.id.content);
// From SO question 4486034
noteListView = findViewById(R.id.body); // Fails
mDbHelper.expandNoteTitles(noteListView);
}
我的 DAO 类:
public void expandNoteTitles(View noteListView) {
Cursor notes = fetchAllNotes();
for(int i = 1; i <= notes.getCount(); i++) {
expandNoteTitle(i);
}
// NPE here when attempt to redraw is not commented out
noteListView.invalidate(); // Analogous to AWT's repaint(). Not working.
// noteListView.postInvalidate(); // Like repaint(). Not working.
}
public void expandNoteTitle(int i) {
Cursor note = fetchNote(i);
long rowId =
note.getLong(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_ROWID));
String title =
note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)) + "W";
String body =
note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY));
updateNote(rowId, title, body);
}
我必须做什么才能在我按下按钮后立即显示更新的笔记标题?
显然,我是 Android 的新手。我指出这一点是为了鼓励您使用小词并解释甚至是显而易见的事情。我知道这是第 100 万个“Android 不重绘”问题,但我已经阅读了数十篇现有帖子,它们要么不适用,要么对我没有意义。
1:What does postInvalidate() do?
2:What is the difference between Android's invalidate() and postInvalidate() methods?
【问题讨论】:
标签: android android-layout invalidation