search

Home  >  Q&A  >  body text

Android 如何刷新当前activity的显示数据?

如何我在一个界面上有两个listview,当触发某项操作,使数据库数据发生变化,两个listview里面的数据都需要更新一下,应该如何刷新呢?

PHPzPHPz2772 days ago609

reply all(5)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 16:23:24

    Adapter has notifyDataSetChanged() method

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 16:23:24

    Add a listener. The implementation of the function is to execute notifyDataSetChanged of two listview adapters
    Write a function to monitor database changes. When receiving database changes, execute the listener interface function

    reply
    0
  • PHPz

    PHPz2017-04-17 16:23:24

    After you add the list to the adapter, ListView set adapter, when the data source list changes, calling the adapter's notifyDataChange method should refresh it~

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 16:23:24

    Use eventbus to distribute events, and then call the adapter’s notifyDataSetChanged() method

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 16:23:24

    If I understand "a certain operation causes data changes" correctly, you can use the observer mode to handle it, because one operation triggers another operation, and it is relatively simple and worry-free.

    First register data monitoring:

     getContentResolver().registerContentObserver(Uri uri, boolean notifyForDescendents, ContentObserver observer) ;
    

    ContentObserver accepts callbacks after changes occur. You can put the code to update the ListView here:

     private ContentObserver mObserver = new ContentObserver(new Handler()) {
            @Override
            public void onChange(boolean selfChange) {
                 // 重新查询数据
                 
                 ...
                 
                 // 更新ListView1
                 listView1.notifyDataSetChanged();
                 // 更新ListView2
                 listView2.notifyDataSetChanged();
            }
        }
    

    Notify ListView to update data:

    getContentResolver().notifyChange(Uri uri, null, false);
    

    The above is just an explanation of the idea. The actual implementation is up to you. You can also refer to my project

    PS: Thanks @dabaooline for the reminder

    reply
    0
  • Cancelreply