I have added a menu to ListView Activity and wanted to allow the user to select the information that would be displayed in the ListView. So for example:
The ListView is populated in the onCreate method.
The user selects an option in the menu.
Upon the user making this selection the ListView would be populated with a different array.
Is onResume() executed after the menu is closed? I would test this but I am not currently home. What is the best way to do this?
Thanks,
Rob
No, OnResume is not called when the menu is closed. What you want to do is put the code that swaps out the information in the adapter in the OnMenuItemSelected method.
Related
Imagine we want to design G+ application. It has a list activity/fragment and by click on an item then detail activity/fragment will be displayed.
My problem is, I don't understand if user hits plus button in detail activity and click on back button then how its counterpart item in list knows that user has clicked it in detail screen (since it shows number of plus oned as same as detail activity)?
Though it's bit late, thought of sharing my answer as it might help others who are coming across similar situation. Basically, you need to create an interface in the detail fragment class and implement it in the parent activity and use it to update the master list through notifyDataSetChanged().
You might want to refer to following SOF thread as an implmentation reference - Update ListView in Master/Detail after a form is saved
Good day,
I make my test application in Android and find some difficult moments to do by myself.
I have two Tab Fragments and Host Activity (that creates two tab fragments) with Action Bar (actionbarsherlock). One Fragment allows to add some information about product and persist it into SQLite. Other Fragment has a listview that displays all the products from database.
The problem is that in the first tab I persist data in different thread with 5 second delay, so you click "Add Product to Database" button and after 5 seconds it will be added. But right after clicking I can switch to another tab with listview and the situation is as follows: data from the first fragment is not persisted yet because of delay, but after it is persisted, nothing changes in listview - the listview doesn't know about changes. I need listview updated authomatically when it notices there are changes in database. How can I do this? I use ArrayAdapter to populate data into listview.
I think you need necessary use broadcast receiver, and in it call method refreshDrawableState() for you list. Respectively receiver is your activity.
Hi i have an app that contains 2 screens . The first screen(main screen) contains a ListView that is filling the rows dynamically by communicating a server. On clicking the row element a new screen is appears, there is a button on this screen, on pressing this button a communication with the server is made(for this i have used AsyncTask class). After the process is completed , i have to switch back to the first screen(main screen) automatically, and the previous row element whose process of communication with the server is just been finished has to be eliminated from the ListView.
I am facing the problem regarding eliminating the row element of ListView.
Please help!
use StartActivityForResult to open second activity and use Custom adepter in list view and update the data of the adapter with new one and notify change to list.
http://support.xamarin.com/customer/portal/articles/531998-android-startactivityforresult-and-finish-with-video-
I recommend you to start the second activity (when you click in your listview item) with startActivityForResult. So the second activity can perform the remove from the server and notify back to the main activity when the task is done. In your case, the main activity will receive the result from the second activity and will remove the item from the list view.
Hope that help!
For SimpleAdapter use the notifyDataSetChanged() function.
I'm a newbie Android developer and I'm trying to create an Android application with 2 tabs, using Activities for each tab. This is basically how it should work:
Tab A has 1 button
Tab B has 1 ListView
When the user clicks on the button in Tab A the application must put a value in the ListView in Tab B
My question is: how can I update the ListView in Tab B when I click on the button in Tab A? I know how to put values in a ListView when it's on the same tab where the button is located but my approach doesn't work when the ListView is in a different tab. I tried...
ListView myListInTabB = (ListView)findViewById(R.id.list_on_tabB);
but didn't work :-/
Any suggestion?
Thanks in advance.
Im not sure you actually need seperate activites for this, unless you are doing some specific work between your activities. Most tab solutions you see in Android solutions work simply by hiding the Views that are not related to the current shown tab. You will find the "visibility" property helpful for making this happen. Also, making sure all of your Views are in the same Activity lets you access their objects all the time, regardless of their visibility.
You don't do that. Just ensure the underlying Adapter has the latest data. Calling requery() on the Cursor does the trick, it automatically updates the ListView attached to it.
Edit: use notifyDataSetChanged() to let the list know that the data is stale.
I would like to write a rather simple content application which displays a list of textual items (along with a small pic).
I have a standard menu in which each menu item represents a different category of textual items (news, sports, leisure etc.). Pressing a menu item will display a list of textual items of this category.
Now, having a separate ListActivity for each category seems like an overkill (or does it?..)
Naturally, it makes much more sense to use one ListActivity and replace the data of its adapter when each category is loaded.
My concern is when "back" is pressed. The adapter is loaded with items of the current category and now I need to display list of the previous category (and enable clicking on list items too...).
Since I have only one activity - I thought of backup and load mechanism in onPause() and onResume() functions as well as making some distinction whether these function are invoked as a result of a "new" event (menu item selected) or by a "back" press.
This seems very cumbersome for such a trivial usage... Am I missing something here?
Thanks, Rob
If the user hits the back button your Activity will likely get garbage collected.
If you properly start your activity from the menu with the different categories through an Intent, with passing the category etc. and then choosing the content in the onCreate method, you will get a new Instance of your Activity every time the user chooses a category, that will be destroyed after the user hits the back button.
This behaviour is perfectly fine. You don't have to cope with strange error cases and populating the list will take some time so the object creation time of the new ListActivity will be no problem. Try to program it as easy as possible for you and then test if there is a performance problem in the end of doing so.