android - 获取当前Android View 并强制重绘

标签 android android-layout invalidation

如何在显示已更新数据时获取当前Android View 并强制重绘?我研究过 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?

最佳答案

根据教程,现有笔记列表显示在 ListView 中。这是一个基于适配器的 View ,因此它显示的项目来自扩展 BaseAdapter 的适配器。类(class)。在这些情况下,您应该通过调用其 notifyDatasetChanged 通知适配器内容已更改。方法。这将向 ListView 发出更新和重绘其行的信号。

编辑:

抱歉,我现在意识到这个例子使用了 CursorAdapters。它们从从数据库查询中获得的 Cursor 对象获取要显示的项目。现在,notifyDatasetChanged() 告诉适配器的是,支持适配器的数据已更改,因此显示基于此适配器的内容的 View 需要重绘其内容。对于 CursorAdapter,此数据来自游标。因此,您还需要重新查询该游标,从数据库中刷新它,如下所示:

private void expandTitles() {
        mDbHelper.expandNoteTitles();

        CursorAdapter adapter = (CursorAdapter)getListAdapter();
        adapter.getCursor().requery();
    }

在这种情况下,requery() 方法会自动调用 notifyDatasetChanged(),因此您无需担心,列表会自行更新。另请参阅此线程:https://groups.google.com/forum/?fromgroups#!topic/android-developers/_FrDcy0KC-w%5B1-25%5D .

关于android - 获取当前Android View 并强制重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11912390/

相关文章:

自定义 View 中的 Android ObjectAnimator

Android Studio - 无效与重新启动与无效并重新启动

android - 如何备份 Android 数据库 - 启用 WAL

java - Android 中无需登录的 Firebase OTP 验证

android - 如何在Flutter中获取复杂的Json对象?

android - 从 onDraw() 调用 invalidate() 会导致堆栈溢出吗?

android - 我可以检测用户是否在trigger.io上全局禁用推送通知吗?

android - RelativeLayout 背景可绘制重叠内容

来自代码的 Android 屏幕截图。明白但不完美

java - 当我打开任何 Android 应用程序时,如何显示我的布局(xml 文件)?