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();
Related
I have a database and adapter for the first activity showing the name and description. There is a button on each list item which takes you to the second activity displaying a unique image related to that item.
I have included an Intent from the first activity to the second activity.
So on the second activity I would like to add the image related to the item clicked.
Question:
(a)Do I include the image in the same database for the first activity or do I need a separate database and adapter for the second activity?
(b)Also do I need to create a separate intent for each item in the first activity as each item has a separate image that it will link to via the button which will be displayed on the second activity.
Your click listener will be one and generic as same lite item view is inflated for all items of adapter.
2.on click you need to pass the Uri string to second activity via intent and display the image Uri in second activity after receiving it from getIntent() in oncreateview() of second activity.
I would like to answer this in two parts.
Part 1: the database: You should use the same table for the image as well. You can always talk to the same database and all the tables from any activity, so no need to have a separate database. So the name, description and image in the same activity.
Part 2: The intent
If you are in a scenario where you have to add any action on click of an adapter item, always use a callback.
When you click on any item, this callback will tell you in the activity that which item in the adapter is clicked.
This medium blog is a very good example to demonstrate this.
In this blog's code, there is a block in adapter where you pass the values from the activity. It is the constructor.
RecyclerViewAdapter(RecyclerViewClickListener listener) {
mListener = listener;
}
If you had added some code, it would help, but I am sure you also have this constructor in your code, so add this listener along with the other data and see how it works out.
Thanks
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();
I have an activity A with a listview and upon selecting some items in the list, and navigating to another activity B with the selected items in the list of Activity A. And made some modifications. After return to A. My screen is not updating with the latest modifications.
Please guide me how to do this. Thank you in advance.
If you have unified data storage for both activities (so that you know that this problem is purely UI issue, because the data itself is updated upon returning from activity B), then you should call this method on your ListView adapter:
yourListView.getAdapter().notifyDataSetChanged()
In order to display what you want, you need to have some TextViews in your other activity and then just add a text to them to display what you need.
When you get the data, just do like this in the onCreate method:
TextView tv = (TextView)inflate.findViewById(R.id.myTextView);
tv.setText(theDataStringYouReceived);
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
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.