0

I have a main activity with an options menu which contains a check-able item. I also have three tabs with an activity, containing a listview each.

What I'm trying to do is refresh the selected tab's listview when I change the check-able item menu option in main activity.

Any idea about how can I achieve this?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Mazinger
  • 633
  • 1
  • 6
  • 12

3 Answers3

0

Usually when you have tabs, you're using a view pager so each fragment should be able to specify it's own option menu thus keeping the scope to the currently selected list.

L7ColWinters
  • 1,342
  • 1
  • 14
  • 31
0

call notifyDataSetChanged() on your adapter in onResume() on the Fragment which has listview.

Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41
  • The problem is that when I touch the menuitem, which is placed in main activity, selected tab activity (the one who is showed to the user) doesn't trigger onresume, so the activity don't know about the change. That's the problem in fact. – Mazinger Aug 18 '18 at 21:20
  • Every tab has a Fragment right.. and the listview is in that fragment.. so call notifyDataSetChanged() in that onResume() of Fragment, not the activity.. – Ümañg ßürmån Aug 18 '18 at 21:52
  • But Tab's onresume don't trigger when I touch the item which is placed in main activity. Maybe you can tell me a way to do it. – Mazinger Aug 18 '18 at 22:01
  • Sure.. Can you paste your code.. So that I can see and let you know how to do it.. – Ümañg ßürmån Aug 19 '18 at 04:26
0

You can do a workaround to do that. Using a Global Class. You can store the selected checked item value in a global class. And access that value from anywhere in your code. How to create a global class

Then use a TimerTask in fragments to check whether the selected item is changed and reload the data.

Here is how to implement a TimerTask Keep the previousItem and currentItem as string values to check for modifications.

TimerTask timerTask;
Timer timer;
timerTask = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //check if the checkable item is modified
                    currentItem = globalClass.getCheckedItem();
                    if(!previousItem.equalsIgnoreCase(currentItem)){
                        previousItem = currentItem;
                        //load your data
                    }
                }
            });
        }
    };
    timer = new Timer();
    timer.scheduleAtFixedRate(timerTask, 0, 200);

globalClass.getCheckedItem(); is the method used to get the selected item from the globalClass. You can set the value when you select an item from the Option Menu And make sure to cancel the timer onPause()

@Override
protected void onPause() {
    if(timer != null){
        timer.cancel();
    }
    super.onPause();
}

Hope this works for you.

NirmalCode
  • 2,140
  • 1
  • 14
  • 19