获取目前Android视图,并迫使它重绘 [英] Obtaining the current Android view and forcing it to be redrawn

查看:256
本文介绍了获取目前Android视图,并迫使它重绘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能获得目前Android视图时,它会显示已更新的数据,并迫使它重绘?我通过Android的记事本教程工作,并完成了第三课没有任何问题 - 解决的办法是提供的,毕竟 - 但我坚持我的第一次不平凡的修改

How can I get the current Android view when it displays data that has been updated, and force it to be redrawn? I worked through Android's Notepad tutorial and completed lesson three without any problems — the solution is provided, after all — but I'm stuck on my first non-trivial modification.

我添加了一个新的按钮菜单,旁边的添加备注按钮。当pressed,该按钮增加了一封信给每个音符在系统的称号。然而,新的头衔不笔记列表中显示出来,不管我多么漫长的等待。我所知道的更新工作,因为变化的的出现,如果我解雇应用程序,并把它带回来了。

I added a new button to the menu, next to the Add note button. When pressed, that button adds a letter to the title of each note in the system. However, the new titles don't show up in the list of notes no matter how long I wait. I know the updater works because the changes do appear if I dismiss the app and bring it back up.

到目前为止,我发现,我必须使用某种失效的方法来使程序重绘自身使用新值。我知道,无效()从UI线程使用和 postInvalidate()从非UI线程使用< SUP> 1,2 ,但我甚至不知道我在哪个线程,而且,两者的这些方法都必须从名为查看对象需要绘制的,我不知道如何获取该对象。一切我尝试返回

So far, I've discovered that I have to use some kind of invalidation method to make the program redraw itself with the new values. I know that invalidate() is used from the UI thread and postInvalidate() is used from non-UI threads 1, 2, but I don't even know which thread I'm in. Also, both of those methods have to be called from the View object that needs drawing, and I don't know how to obtain that object. Everything I try returns 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);
}

什么我必须做,尽快获得最新说明标题,以显示我preSS按钮?

What do I have to do to get the updated note titles to show up as soon as I press the button?

很明显,我是一个完整的新手到Android。我指出这一点,鼓励你用小词,并解释甚至是明摆着的事。我知道这是百万分之一的Android没有重绘的问题,但我看过几十个现有职位,他们要么不适用或不道理给我。

1:什么postInvalidate()做 < BR>
2:<一href=\"http://stackoverflow.com/questions/7596370/query-on-invalidate-and-postinvalidate-in-android\">What是()方法Android的无效()和postInvalidate区别?

推荐答案

据本教程中,现有票据的名单是在ListView psented $ P $。这是一个基于适配器视图,因此它显示的项目从一个适配器扩展 BaseAdapter 类。在这种情况下,你应该通知内容已通过调用其<一改适配器href=\"http://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged%28%29\"相对=nofollow> notifyDatasetChanged 方法。这个信号会在ListView更新和重绘其行。

According to the tutorial, the list of existing notes are presented in a ListView. That is an adapter based View, so the items it shows are sourced from an adapter extending theBaseAdapter class. In these cases, you should notify the adapter that the contents have changed by calling its notifyDatasetChanged method. This'll signal the ListView to update and redraw its rows.

编辑:

对不起,我现在认识到这个示例使用CursorAdapters。这些源的项目从一个从数据库查询得到的Cursor对象显示出来。现在,什么notifyDatasetChanged()告诉适配器,这是备份适配器的数据已经改变,因此视图,显示基于此适配器上的东西需要重新绘制其内容。在一个CursorAdapter的的情况下,该数据是从一个光标来。所以,你还需要重新查询该游标,从数据库刷新,就像这样:

Sorry, I now realize that this example uses CursorAdapters. These source the items to show from a Cursor object that was obtained from a database query. Now, what the notifyDatasetChanged() tells the adapter is, that the data that backs the adapter has changed, so Views that show stuff based on this adapter need to redraw their contents. In the case of a CursorAdapter, this data is coming from a cursor. So you also need to requery that cursor, refreshing it from the DB, like this:

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

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

重新查询()方法自动调用在这种情况下,notifyDatasetChanged(),所以你不必担心,该名单会自动更新。看到这个线程也:的https://groups.google.com/forum/?fromgroups#!topic/android-developers/_FrDcy0KC-w%5B1-25%5D.

这篇关于获取目前Android视图,并迫使它重绘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆