updating a listfragment from an activity - android

I am trying to update my listfragment when a button is pressed. the button puts a name into a database but how do I update the list so that name appears? the listfragment uses a cursorLoader to load from the database. I even tried using a content observer on the database and calling getLoaderManager().restartLoader() but that didnt work either.
So what can I do to update the list when I click a button in an activity? I have found very little information on this please help

I can't be sure, but I assume there is something weird going on with the interaction between your Fragment and your Activity, which you are apparently displaying simultaneously somehow. Is there any reason why you are listening for the Button in the Activity? Are you dealing with a multi-pane screen or something? It seems like it'd be a lot simpler to just have the Fragment deal with this itself (since it is the one implementing the LoaderCallbacks, I assume).
Also, be sure that you are implementing your Fragment lifecycle correctly... i.e. you call initLoader() and set the list's adapter in onActivityCreated() as opposed to onCreate().
Hope that helps!

Related

Restoring position when returning to ListFragment

There are some posts here related to this issue, but I am trying to figure out what is going on in my particular case. Basically I have an Activity that instantiates a ListFragment to display a list of items that the user can scroll. When a list item is selected, I launch a separate Activity that "takes over" the screen to display details of the selection.
The issue is that when the Back button is pressed in this 2nd activity, the ListFragment's list is again displayed, but it's always repositioned to the top of the list, and I can't figure out how to correctly get the previous state of the list so that I could reposition it with setSelection, etc.
I do have a saveInstanceState in the fragment and it is being called when the details activity is launched, just as I would expect. However, I can't see any obvious fragment method that will be called to receive this Bundle back when the details activity returns. onCreateView and onViewCreated are not called again, and I can't use onViewStateRestored since I'm targeting API 14 (if that's even applicable).
Any ideas? This has to be something pretty basic that I'm missing!
The call to the deprecated startManagingCursor was the cause. I removed it and everything works fine. There was no need to get into a CursorLoader since I am using a simple CursorAdapter with a local SQLite database.

ListFragment is empty after navigating back

Okay, I know there is a lot of discussion according to this topic, but I couldn't find any answer.
My problem is the following:
I have an Activity with NavigationDrawer, so the Fragments are added from code. The first fragment is a ListFragment, witch reads data from an SQLite DB stored in the SD card. The data loading is kind of slow, because I load the nearest n place based on location. So I don't want to load the list every time.
Now, if the user clicks an item, a second Activity shows the details. The first strange stuff is the first activity is destroyed almost every time a leave it, witch is awful, but still I could save the state.
The discussions that I rad so far indicated that the Activity should retain the last displayed Activity, and it kind of does, because if I change the content of the first activity from the Drawer before navigation the correct Fragment is created.
The problem is that even if the ListFragment is being re-used its contents will be missing (since I create a new View in the onCreateView() because of custom layout). I guess I should use the Bundle provided as a parameter in onCreateView(), but I just couldn't figure it out.
So summed up: what is the proper way of saving the items of a ListFragment, even if its parent activity is destroyed?
It turned out that I had a developer option turned on that killed all Activities that I navigate from. I didn't know that because I use my phone in Hungarian (guess it'll change from now), and for some reason they felt like they had to translate the word "Activity", making the whole option description not understandable. Sorry about that, now my first Activity is kept in the memory, so the whole issue is not present...

Using ViewHolder-pattern in a detail Fragment

In my Android project I have an Activity that uses a Master-Detail view, created with two fragments.
My detailfragment is giving me some "problems" though.
It consists of 50+ controls (TextViews, EditTexts, CheckBoxes, Spinner). Of this 50+ controls I programmatically get a reference to 32 of these controls in my detail-fragment and load their data from my SQLite database.
When I run this and initialize my controls by using
(SomeControl).findViewById(R.id.mycontrol);
LogCat keeps warning me about that I might be doing too much on the main thread.
I know that findViewById and inflating views is an expensive operation, so I had an idea!
I was wondering if there was some way to use the viewholder pattern or view-recycling on my detail-fragment like I'm doing on my ListFragment. that way I could avoid reinitializing my detailview each time I select another item in my ListView. And avoid calling .findViewById as much as I do. Does anyone have an idea on how to implement something like this. Would it make any difference if I did initialization of the controls in the onCreate-method of my detailsfragment? I was also thinking about making my detailsfragment a "singleton" and then just use getLoaderManager().restartLoader when the selection of my listfragment changes. Any thoughts on all of this would be very much appreciated.
Well unless you're using the exact same layout for each control then I'm not sure if there's a way to do that.
But there may be a way to solve your problem: using an AsyncTask.
As long as those controls aren't necessary for your program to crunch data (and from your explanation I don't think the UI elements would be triggered until the user has interacted with it) you should be fine and the main thread will be free to do whatever it needs. The only downside I see with this method would be that some UI elements might appear maybe a half a second late (not so bad if you think about it).
Found a solution. Had to change my implementation entirely though.
Now my implementation is using loaders on both ListFragment and DetailFragment.
Here's a list of changes I've made:
Created a interface for my ListFragment with one method (onSomethingClicked(SomeObject obj)) and made ListFragment observable through this interface.
Implemented the interface in my DetailFragment and registered a listener on the ListFragment.
Implemented the method onSomethingClicked() in DetailFragment. When this is triggered i pass data from ListFragment to DetailFragment and restart my DetailFragment-loader and load data into my already initialized controls in OnLoadFinished.
No need to inflate the view every time the selection in the list changes and no need to .findViewById's AND MOST IMPORTANTLY no more Choreographer warnings :)

How can I "refresh" the fragments in a ViewPager?

I am having an issue here that is driving me nuts. What I have is the following:
FragmentActivity1 holds the viewPager. It instantiates the FragmentAdapter and two fragments and attach them to the ViewPager.
Fragment1 has only one button. When user clicks this button, I create one Intent and invoke a FragmentActivty. This fragmentActivity contains a form, which the user fills in and press OK. When he does that, I persist the data in the DB of the app.
Fragment2 is a ListFragment, and lists all the data that was inserted previously by the user.
By the time user completes the form and presses OK, I persist the data, like I said and finish() the activity, returning to Fragment1. WHen I swype to Fragment2, the data is not there. I need then to swype to Frag1 and then back to Frag2. Only then I can see the problem.
I have tried setting listeners between activities and fragments but, still, cant make this work.
Have anyone seen? I am willing to share my small project as well, so you guys can take a look.
Thanks,
Felipe
I usually fill my ListView in onResume() in the ListFragment. When I'm done adding new stuff to the list (In a seperate FragmentActivity, just like you), the ListView automatically gets refreshed, because onResume() gets called ;)
Another approach is using onActivityResult(), then you can update list only when something new is added or something is removed

Filtering ListView which is part of a ViewPager Fragment

I recently converted my application from using Activites and TabHost to using Fragments and ViewPager from the Android Compat Library for API v4
I was able to fix/resolve most problems but am unable to retain the previous behavior with filtering text in ListViews using the setTextFilterEnabled method.
My ViewPagerAdapter contains Fragments which each have a ListView. As users swipe through the ViewPager, I would like the currently active Fragment's ListView to filter text as users type, just like I was able to do with the TabHost-Activity model. Currently, it looks like the first Fragment's ListView will respond correctly, but if I swipe to the next Fragment and try to filter its ListView, the filtering will still apply to the first one. If I swipe past the first two and then filter, the results are non deterministic. The currently active Fragment will never apply the filter. Sometimes a neighboring fragment will, sometimes it won't.
I tried to fix this by adding custom callbacks which let me monitor which Fragment is currently visible, and which are hidden. When a fragment becomes visible as the main Fragment of the ViewPager, I set the setTextFilterEnabled on its ListView to true, and set all others to false. This didn't seem to help at all (I verified that I was toggling the flag correctly for the right Fragments).
I suspect this needs some kind of deeper integration with ViewPager, but I can't really figure out what I need to wire up. Any ideas on how I can make this work? I'm happy to muck with the ACL code if needed.
You could implement the method onPageSelected extending OnPageChangeListener. Perhaps you already do this. From there you can set the adapter to the current ListView or requery your cursorAdapter if that's the case. Note that the Adapter used must implement the Filterable interface.

Categories

Resources