ListView in Fragment not refreshing - android

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

Related

How to sort items in Recyclerview while its Activity is in onStop() state

I have list of items in RecyclerView. Items can change their positions on their data changes. Data of items can change when this activity is not visible(app is closed, or user has opened another activity over this one). For example when my activity is in onStop state, and I update my items in adapter and call notifyDataSetChanged() items only sorted when I resume this activity(I can see how they change their places).
I want to resume my activity with already new state(items should already have to be in their new positions).
Extra detail: I also enabled default animation for RecyclerView
adapter.setHasStableIds(true);

Refresh Android Activity from outside mainActivity (Adapter class)

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();

fragment does not reflect changes

I am trying to create an application in which my Fragment contains one listview which is populated from a database, and when one of the item is clicked, a new activity starts.
In this new activity, I update the database, and then come back to the first Fragment (by pressing back button).
What happens next is that the ListView in the first Fragment does not take the changes I made in the second Activity into account.
And strange thing is that when I start the application again, the Fragment shows the updated list.
How can I automatically update the ListView when making changes to the database?
Try to use the onResume( ) method in your Fragment, so you can told the Adapter that the Dataset has changed.
#Override
protected void onResume() {
super.onResume();
// Dataset has changed, notify adapter !
mAdapter.notifyDataSetChanged();
}

Refreshing fragment listview

I have a activity (Activity A) with a fragment that has a ListView inside of it and I'm calling another activity (Activity B) to add records to the fragment's list.
My problem is that calling adapter.notifyDataSetChanged on Activity A's onResume/onFragmentResume and the fragment's onResume changes nothing.
My adapter is populated in Activity A's onCreate.
I also tried inserting the adapter.notifyDataSetChanged in a runOnUiThread.
Any one tackled this situation before?
It is not a good suggestion but try to reinitialize the adapter and set it again and tell me if it is work fine.
You should populate your adapter in the fragment in either the onAttach(), or onViewCreated() methods. You could also populate it in the onCreate() of the fragment, then call notifyDataSetChanged in either of these lifecycle methods I suggest.
These methods will be called whenever the fragment is connected and drawn, thus, you can be sure that it will populate your Fragment correctly.
In general, have all your view related items (like adapter creation and population) in your fragment, and navigation (ActionBar, and flowing between Activities) in your Activity.

How do I make a list loader to reload from a detail Activity in Android

I have a ListFragment, which data is loaded by a SimpleCursorLoader(own imlementation; it loads with a curser and does not use URI).
When an item in the list is selected. I either start up a new activity (detail activity) or I show what is selected in the detail fragment next to the ListFragment. Depending on the screen size.
If the detail fragment is added to the ListActivity, I have figured out how to reload the list. I assign an interface to the ListActivity and call it from the detail fragment, when the change is happening. And in the list fragment I reload the list using getLoaderManager().restartLoader(0, null, this);
Now my problem is, if the detail activity is loaded, I don't have access to methods on the ListActivity. I could probably implement some observer pattern. But there must be a best practice for this.
How do you make a ListFragment reload the list, when a detail fragment has changed the data in the database.
Try moving your list creation code to onResume. That way it runs on activity creation and every time you go away from the activity and come back.

Categories

Resources