Refresh Android Activity from outside mainActivity (Adapter class) - android

I searched over it and found two results:
Call
1
recreate();
2
startActivity(getIntent());
finish();
But I want to refresh the activity NOT from within the activity.
I have more than one fragments, each fragment has listViews which has buttons.
My listView is populated by the data on the server. On button Click I am changing my data on the server and want to refresh all the listViews (so as to make them load new content). The onClick() function is in the listAdapter. So is there any way to refresh whole Activity from this listAdapter class.
OR
Way to refresh fragments from the listAdapter class.

You should call notifyDataSetChanged() on your Adapter. See this question

Pass the context of your activity to your ListView and call this method from your list view
((Activity)context).recreate();

Related

Is it a good practice to start a fragment from an adapter?

I have an activity with fragment A. Fragment A hosts a list in recyclerview which requires an adapter. When an item from the list is clicked I want to open another fragment, say B, showing additional details about the item.
I can open fragment B in three ways:
From the recyclerview adapter itself where I will have the item position etc.
From fragment A using callback from the adapter, since the adapter has all the required info like position, object etc.
From the activity, again using callback. If i do from the activity, i will have to add callback interface from adapter to fragment A, and finally to the activity. Looks too much.
I want to know what is the best way to open fragment B.
Hey its not good practice to start the fragment from adaptor. because it will be very complex to find the container of fragment. so please always try to start the fragment from main activity which will be parent of all fragment.I hope its help you.
Option 2 is always good approach->
2. From fragment, A using callback from the adapter, since the adapter has all the required info like position, object etc.
"According to MVC pattern adapter is always used for binding view with lists. So adapter should always independent from the fragment so the Single Responsibility Principle will always be handled. So, there should be no dependency from the adapter to fragment but fragment to the adapter. When you call fragment or activity from adapter it will create a cyclic dependency with one another so memory will not clear until you finish the apps. You can call any callback method of the fragment from the adapter which method will call the desired fragment you want."

ListView in Fragment not refreshing

I have a custom ListView inside a fragment that represents a list of activities. Each activity is saved in a database (I'm using Parse), and the ListView is populated with the Activities from the database. There is an add button in the fragment that takes the user to an screen where he/she can create a new Activity object. When the user is done, the activity is finished (I call finish() on the Activity), and the fragment is brought back up. The problem is that the ListView doesn't refresh- it's the same list before the user was sent to the Activity Create screen.
I tried to override the onStart and onResume in my fragment class and call notifyDataSetChanged() or invalidateViews() but neither method works, the list view doesn't refresh so it includes the new Activity that was just created by the user.
Start the activity that adds Activity with startActivityForResult(). Override the onActivityResult() in your listview fragment to get notified when the user has finished adding an Activity. Then, refresh your ListView by querying the database again or adding the new Activity to the adapter (depending on how you implemented the listview adapter).
In your case it looks like you forget to refill the activity list attached to adapter from which listview generate views.
Refill the list again from scratch and then call notifyDataSetChanged();. Your listview will be updated with new entries.
Cheers

how to call method another activity inside tabhost from current adapter

I have a tabhost with 3 activities A,B,C
activity A has a listview with current adapter, then inside adapter i do a sqlite insert, then in activity C also has a spinner listview with another adapter. In activity C, i added method to refresh spinner content and also method for onResume() with refresh spinner content inside it. If I did insert from adapter of listview from activity A, spinner did not get any changes (I hope it can get changes from onResume(), because there is some code to refresh spinner content), but nothing happened. Also if I call the method for refresh spinner content using below code :
Context mycontext;
((Activity C) mycontext).RefreshSpinner();
then also nothing happened. So how can I solve for this problem ? i did it with 2 ways. Thanks in advance.
Hie,
In your Adapter, if the context your application is getting is from Activity A suppose and you want to refresh Spinner content which is in Activity C, you can put your code in onResume() method of activity C, or adapter used to bind Spinner of Activity C. For this, you can either create a static method with a changes you want and update it first in the Adapter class of Activity A. Then that changes to reload a spinner with some random list/data which u have updated in Adapter for Activity A can be used directly in Adapter for Activity C or in onResume() method of Activity C. Else you create a list with updated data and bind that in Adapter for Activity A, and populate it in your onResume() method of Activity C passing context to it.

Activity list view display different data (String array) on click of listitem

I have an activity which contains listview, when click on any list item, i want to display same activity with listview with different data and so on.
When i click on back button i need to display same activity listview with old data.
Is it possible? or is there is any other way to achieve this. I dont want to create any new activities or fragments for this?
Thanks
KrishIndia
You can write the function such that each time you click an item, it generates the list, assigns the data to your adapter and attaches it to your list view of choice.
If fetching the data won't be expensive for you, you could use only one adapter (array adapter or list adapter or simple adapter should be fine) and just re-assign it each time. (Ex. If you were listing files in a directory, inside your onClick() function you would have some function declaration like list_items(directoryName) that you would call each time. That function would declare and set an adapter for your data.)
If you are concerned about the expense of re-fetching the data, just store multiple adapters (outside your onClick() function) and set them to your list view as needed.
on click of list item just set new adapter or new data and call notifyDataSetChanged()
and for backpress override onBackPressed() and handle accordingly
Maintain separate adapter for each new data. When you want to return back just pass the relevent adapter object to that list view

Android refresh listview adapter from another activity

I Googled a lot, but can't find the proper answer.
I've got two activities. Each of that activities 'has' one listview.
Each of that listviews have a custom ArrayAdapter. So :
Activity 1 ----> Listview 1 ------> custom ArrayAdapter 1
Activity 2 ----> Listview 2 ------> custom ArrayAdapter 2
When I press a button in Activity 1, then it refreshes the Listview 1 (it's adapter).
The goal is : I have to do a refresh on Listview 2 too that time. How can I achieve this ? I've read something about broadcasting, but didn't understand it well.
Thank You !
If you are in Activity1, it's useless to try to refresh Activity2's listview as it's not visible. However, what you can do is, when the user is switched to Activity2, just call adapter's notifyDataSetChanged() in it's onResume() method and your Activity2 will be refreshed.
Add listView as global variable in each activity and make it public static.
Then, just call it when you need it.
e.g. in activity1, call the following to let the list in activity2 refresh
if(SecondActivity.listView != null)
((ArrayAdapter)SecondActivity.listView.getAdapter()).notifyDataSetInvalidated();

Categories

Resources