Android: Refresh an activity - android

I've just a little question (I hope it's just a little question ;-)).
My current app-project has, of course, a bunch of activities. In one of these activities (I call it DataActivity) I can enter some informationen (stored on a webserver). Another activity (called ListViewActivity) should show all available information stored onto the webserver.
The problem is: My first step is to open the ListViewActivity with a few entries (it works proper at first time). Then I go to the DataActivity to input a new listitem. When I now go back to my ListViewActivity the new entry isn't listed. I think I have to refresh the ListViewActivity ... but I don't exactly know how. ;-)
What can I do to refresh this ListView after adding the new row/listitem, so that all available information will be displayed?

I assume you are calling the init of the list in onCreate. Just implement onResume (or onStart should work as well) and add a refresh() method that recalls the data from your server for your list.

You need to call notifyDataSetChanged() or notifyDataSetInvalidated() of your list adapter when you are back to your ListViewActivity.

Related

Android ListView.getChildCount() greater than getCount()

I have an Activity with a Fragment that displays a ListView containing simple TextViews. A menu item can trigger another Activity via an Intent. That new Activity clears the ArrayList underlying the ArrayAdapter for the ListView using ArrayList.clear().
When I backup from the new Activity to my original one with the ListView, and get control in onResume(), I find that my ListView.getChildCount() is the same as when it was left due to the Intent, but the ListView.getCount() is now properly zero!
I have tried using the adapter's clear() method, I have tried Adapter.notifyDataSetChanged() (although I should not have to).
If I modify the underlying ArrayList from within the fragment itself, all seems fine. For example, clicking on an element gives you an option to remove it, move it up or down, etc... That works OK.
Also, If I then leave the List Activity and return to it again, all is well. So clearly the ArrayList is the same list. I never create a new list, only .clear() it.
Any idea how the Child Count can possibly be more than the underlying element count? Perhaps some kind of observer for the ArrayList does not trigger because the Activity is suspended? In which case how could I sync them up again? I have tried invalidate() for example.
This is under API 23.
I seem to have found a workaround. I noticed another post here discussing thread safety. It seemed to have implications if you do not modify your list from the original UI. So, in my onResume(), I just did a setAdapter() again. With the same adapter and list, and viola! All seems well again.

Updating Listview

I am developing an android application similar to Facebook android application. On the timeline screen I have list of posts , user can like or comment any post. When user like a post, to highlight the like button I took the object from array , change its liked status and notify adapter so like button gets highlighted.
If user clicks on a Post a detail screen opens up. In detail screen user also has option to like the post. Now my problem is, if user like a post from detail screen and went back, he should be at the same position in the list and like button of the post on that position should be highlighted.
I tried to do it by starting detail activity with StartActivityForResult() method and passed the selected object. In onActivityResult() method I am getting back the modified object from detail Activity. I replaced the original post object by the modified object in array and notify the adapter.
I am not sure about my logic that its a good one or not. I need a better solution to do this. I will be very thankful to any good suggestion.
onActivityResult() would be just the right answer but you are not finishing your activity but pressing back/home button instead. So in this case no information is being passed to the parent activity.
A solution that comes to my mind is modifying your objects more permanentle via sharedPreferences or some ORM. But it may be something unpleasant being collecting data from memory in every movement of your app.
What I suggest is transforming your detailActivity to a detailFragment. It would be easier to pass information to the parent activity. In this case the activity that has the full list would start the fragment passing the single element that has been clicked. After that, you would implement a interface to comunicate with the parent activity and in this case the main list would always be updated correctly.
This method requires a very long explanation, too much in fact. I would point out the main steps in order to give a starting point for researching.
Create a fragment similar to your detailActivity. It needs a declaration of the proper interface to communicate with the activity.
Make your activity to implement the interface and override its definition. This function will handle like button being pressed. So is it here where you need to update the main arrayList.
Change one main fragment with detail fragment when an item is clicked.
I hope it helps you to find final solution for your code.
PD: I found this url that may content the whole process, just very well detailed.
Yes. onActivityResult() is a good implementation for activity to activity communication.
Your logic is not wrong.
But if you want it to be simple, you might want to try some third part library like EventBus, it could help you to deliver your message between components like activities easily.
In your case, a StickyEvent is helpful.

ListActivity with Recursion Android

my problem is the next one:
I want to use a ListActivity with recursion, so that it is called "x" times, and everytime i click in one article on the list, it is re-calling again the same Activity but it is loading different data, cause what I wanna do is to make menu and sub-menus, and the expandableList is not enough for me because there are gonna be n-levels(i will know dinamically.....).
Anyone has an idea how can I implement it??
Thanks!
Store every data in some kind of N level ArrayList. Write your Adapter class to accept a level of this list. On the item click, it passes to the next level of your ArrayList and you call a notifyDataSetChanged() on your list.

Best Practice activity- switching and view- changing in android

I am still searching for the best solution howto use a layout with a menu and a toolbar and inflate or start activities in android. My question may sound confusing, but im trying to explain it in an example.
Lets say im programming an android app (surprise.. i really do)
My app can do following:
User can log in [3] or register [2]. If he logs in, a new activity starts and his dashboard will be shown. If he registers: an activity for the registrationprocess starts.
Registrationprocess: user puts in his desired username and password and presses a button to accept. His data will be formvalidated and if valid, a new activity starts where he can choose his settings. Backbutton works and data can be passed to the new activity. After the last registrationwindow data will be saved and dashboard started. Starting new Activities is fun!
Now THATS where it gets complicated. Dasboard has an 'actionbar'(top) and a 'toolbar' (bottom, like tabs). So everything should be viewed in the middle part of the viewport(from now called main view). No more activity switching :(, tho.
Currently each tabclick removes all views from the main view and adds its new view. Look great, can be animated and works like a charm. Except: its currently not dynamic.
So... i don't know how to solve it the best way. For example: i fetch data from a webservice, create a listview out of it and it's extending listactivity. This activity i can't start but this data need to be put into the main view. How can i do it the best way?
And is it efficient?
I'm practicing and it's actually my first small discussion i want to start. So... FIGHT! ;)
UPDATE:
I've seen an interesting way to start activities and get results.
Launching activity through intents
. Is it possible to insert new/ update views after activity started? I would then generate my results in a separate activity. Update the view. Return back to 'dashboard' and load the view that was just updated. Possible? Or inefficient? And how can i update a view out of another activity? There is so much i need to learn :/
UPDATE2:
A good example of an app that has done it: Google+
Too bad i don't have their sourcecode ;)
UPDATE3:
What is best?
load a new activity, disable animation and set selected toolbox tab +
disable backbutton functionality
startActionForResults, fetch results and update current view (still don't really know how that would be possible)
viewFlipper onflip changing+updating data in flipped view.
I still don't know any efficient solution. Or am i missing something essential? I've just finished my ListActivity to fetch data from my webservice. But it still runs in a separate activity. How can i implement it into my "main view" now? Ofcourse... i could set a list my custom adapter. But currently im updating and fetching data from the server when i create the listactivity.
Im afraid this could be the only answer i'll get: Embed external Intent in main Activity
UPDATE4: I'm trying something.
Based on nininho's answer (thank you!) im trying the following approach:
Start Dashboardactivity and create a ViewFlipper.
Each Toolbarclick represents a certain ViewFlipper page.
Each Page has a Listadapter implemented and shows different results (different webservice queries). (ListView, GridView, with profileimage, without profileimage)
On Toolbarclick start AsyncTask or Service and notify List in current Page that data has changed. (ofcourse IF data has changed). Switch to page that was clicked.
Implement updatefeature. On scroll to bottom of list = fetch more data and add it. Update other lists automatically after 5min. or update list on update-button click.
PROs so far: Backbutton standalone for whole activity. Page-flip-animation possible. Async updating of lists and still possible to switch to another list.
CONs: ... someone has any? What about efficiency of such an approach? Does the ViewFlipper carry all the information so the performance would go down or does the viewflipper recycle its Views (like ListView)?
UPDATE5:
If i have some time i will make everything here more read- and discussable. Don't be mad at me for reading my rubbish ;)
From what I understand you want your app to start, fetch some data from the internet and after show this data on the main screen.
I don't see the need of a second activity to fetch the data because from your explanation you want to use it only to fetch the data, so the best approach would be:
Create one Activity (your dashboard)
Start an AsyncTask or Service on the background to fetch the data.
When the fetch ends, notify the activity that it ended.
Change your dashboard to show the list (you can use a ViewSwitcher if you want some animation or just create a layout with the list invisible and then change to visible).
ps: you can use a ListView outside of a ListActivity, just create a ListAdapter to create the ListView items and add this as the adapter for the ListView.

How to manage activities in Android?

I identified a problem in changing one activity using tab. In one tab activity I'm adding data to my SQLite database, and in the other tab activity I am displaying them using listview(array adapter). But when I come back to add data after adding new items to SQLite, the newly added records are not updated in my listview.
How do I fix this?
You seem to be pulling the list data from a DB. Is there a reason why you are using an ArrayAdapter instead of a CursorAdapter?
Anyway, you should call notifyDataSetChanged() on your list adapter when the data has changed so it can refresh the view.
you can add code to update your listview (via notifyDataSetChanged or some such) by overriding the onResume() method in your activity which is called whenever the activity is brought back to the foreground.
See http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

Categories

Resources