ListView adapter data change without ListView being notified

前端 未结 2 1042
终归单人心
终归单人心 2020-12-09 22:48

I\'ve written a ListActivity that has a custom list adapter. The list is being updated from a ContentProvider when onCreate is run. I also have a service that gets started

2条回答
  •  感动是毒
    2020-12-09 23:14

    If you want to update the UI listview from a service then you should call notifyDataSetChanged() on onDestroy of the service

    make the adapter static from your main activity and call that adaptername.notifyDataSetChanged()

    like this

    @Override
           public void onDestroy() {
    
                   if (MainActivity.isInFront == true) {
                           if (MainActivity.adapter != null)
                                   MainActivity.adapter.notifyDataSetChanged();
                           MainActivity.listView.setAdapter(MainActivity.adapter);
                   }
    }               
    

提交回复
热议问题