Ivan
Ivan

Reputation: 11

How to refresh current listview from another activity?

How to refresh listview in Activity1 from another or Main Activity after clicking refresh icon in toolbar?

Here's my expected coding design:

MainActivity - has a Tab and Toolbar created with a refresh icon.

Activity1 Fragment - has Listview1 in tab1

Activity2 Fragment - has Listview2 in tab2

Once click on the refresh icon, all the listview will be refreshed. However, I have no idea how I can call the refresh function in Activity1 from another activity.

The following coding is for refreshing listview(listView_monitor) in Activity1 and works properly if it's called itself:

        private void Refresh_Listview() {
            monitorList = new ArrayList<>();
            monitorList.clear();
            if (Create_Data_Array()) {
                adapter = new FGT_Monitor_ListView_Adapter(getActivity(), monitorList);     
                listView_monitor.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
        }

Please kindly advise.

Thanks a advance.

Upvotes: 1

Views: 35

Answers (2)

Ban Markovic
Ban Markovic

Reputation: 940

The easiest way to accomplish this is to make Refresh_Listview function public, and call it from MainActivity, by Activity1 reference, like activity1.Refresh_Listview().

But for me, this is not the cleanest approach. The next approach handle this situation with interfaces. If you want to refresh list in Activity1 from MainActivity, this is the case if your refresh button is in MainActivity and list in Activity1. You can define interface in your MainActivity

public interface OnRefreshListClicked {
    void refreshList();
}

Now you need to implement that interface in Activity1, where you want to override function refreshList which will call your function Refresh_Listview().

public Activity1 extends Fragment implements OnRefreshListClicked

When you did that, next step is to pass that referencu to your MainActivity. So in MainActivity make global private OnRefreshListCliked onRefreshListClicked. And when you make your Activity1 reference, you can pass it to your onRefreshListClicked = activity1 (because your Activity1 implements OnRefreshListCliked). And now everything you need to do in your refresh button click listener

if (onRefreshListClicked != null) onRefreshListClicked.refreshList();

Upvotes: 0

Muhaiminur Rahman
Muhaiminur Rahman

Reputation: 3152

There are multiple ways to do this. For me, the simplest way is using a Broadcast receiver. For example

Inside Fragment A

@Override
public void onResume() {
    getActivity().registerReceiver(mReceiverLocation, new IntentFilter("data_action"));
    super.onResume();
}

@Override
public void onPause() {
    getActivity().unregisterReceiver(mReceiverLocation);
    super.onPause();
}

private BroadcastReceiver mReceiverLocation = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            Category category = (Category) intent.getSerializableExtra("category");
            Log.e("data", category.getName());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
};

From Activity(refresh click)

public void onClick(View view) {

            Intent in = new Intent("data_action");
            in.putExtra("category", category);
            context.sendBroadcast(in);
        }

Upvotes: 1

Related Questions