Refresh view display when underlying data changes - android

I have seen this question answered a few times in which it is suggested to use the method notifyDataSetChanged() from BaseAdapter.
Is there a way to refresh when your application does not use any adapters? I have a simple application where I use a few activities with preferences, and relative layouts with text views and buttons. At the moment I do not use any of the adapters like SimpleAdapter or ArrayAdapter or CursorAdapter. It seems like in my case I have to create one of them just to get to use notifyDataSetChanged()? There is no easier way for me?

Looks like (I may be mistaken, but it really looks like) you don't catch the purpose of those classes - SimpleAdapter, ArrayAdapter or CursorAdapter. They are expected to work with ListView inside of an Activity (or even better - inside of a ListActivity). If you don't use ListView then those adapers are most likely useless for you.
I assume you have your data persisted in some way (SharedPreferences or file). So if you start any of your Activities, then it just reads the data to populate the views. In this case nothing extra is needed. In case if you need to reload data for a currently visible Activity, then just reread the data from persistent storage and repopulate the views.

If your data is changing and you need to refresh your view, how is the data actually changing? Is it in a separate thread or, do you just want something to happen periodically (polling an RSS feed, or something)?

I had a similar problem. I had a radio group, and each radio button had a label. When the screen rotated, I used a different layout with a radio group & radio buttons with the same IDs. I was calling setContentView() in onCreate(): and after rotation, the “old” labels would show up on the new layout (bizarre). When I moved setContentView() to onResume, everything seems to update ok. Thanks to Arhimed for his answer above.

Related

how to saveInstanceState of a view defined in getView() method

I have MainActivity with a ViewPager that hosts three tabs and their respective classes extend Fragment. One of the tabs has a layout with a ListView, the
layout of the each item in the listView is as follows
TextView, checkBox and imageview
This ListView has customized adapter class that extends BaseAdapter.
What I want to do is, to save the check state of the Checkboxes of the ListView.
in the class that extends Fragment I override the method onSaveInstanceState, the problem is the checkBoxes are defined in the getview() method in the
class that extends BaseAdapter! I want to know how can I save the check state of the checkboxes in the method "onSaveInstanceState"?
I think you might be misunderstanding the purpose of BaseAdapter. BaseAdapter-derived classes are meant to serve as an interface between a dataset and the AdapterView in which you display that dataset's information (and/or interact with it.)
With that in mind, I kind of think the easiest way to accomplish what you're doing is going to be to keep track of whether or not the item is checked using a boolean as part of the dataset. For example, if your dataset is an ArrayList<SomeObjectYouMadeUp>, you'll want to add a boolean member to SomeObjectYouMadeUp; set it during the CheckBox's OnCheckedChangeListener, and use it to determine whether the CheckBox should be visually checked during getView().
That might take some rethinking of your code, but trust me, you're probably going to want to do it. It's possible to do it the way you are describing, but it won't be easy or reliable; you'll want to get individual access to each visible view in your AdapterView using the method described here, but again I have to recommend against this.
The biggest reason is that onSaveInstanceState occurs most commonly during configuration changes - for example, a screen rotation. This means it is almost certainly an incorrect assumption that you'll be displaying the same Views. Say there are 10 Views visible in portrait orientation, and 5 visible in landscape. So the user rotates to landscape - which 5 do we get? Do you know for sure? You'll have to check each new View against some identifying information you probably also had to keep in onSaveInstanceState - and what if one of the new Views wasn't visible before the rotation?
The list of questions goes on. Do yourself a favor: save the check state with the rest of your dataset, and let Android figure it out for you.

How to switch an android view based on a condition

I am creating an Android application that will mainly consists of ListViews in each activity. What I hope to achieve is a mechanism that will check to see if network connectivity is present and if so then then the data should be retrieved and supplied to the ListView. If there's no data present or the internet connection is unavailable then a seperate view should be loaded.
Which way is most efficient do to this. So far I have seen answers about using a ViewSwitcher or Viewflipper but I am not sure that's the right approach. Should I use fragments and then load a particular fragment based on the condition that was met? I just need some advice on how to accomplish this.
Have you considered just swapping the adapter? This question might be of some use to you.
I guess you might add some kind of factory method that will decide what adapter to instantiate depending on connectivity availability. Thus you will control both the data to be used and the appearance of list entries (I assume you will be using some custom extension of one of the existing adapters).
The best way is going to be to use the emptyView that's provided as part of the AdapterView base class. You just set the adapter as per usual, but if no data is retrieved (i.e. your adapter's data source is empty) you will show the empty view in place of the list. This empty view can be anything you want it to be.
As #Ivan says you should consider setting adapters in a dinamic way.
And about the fragments thing, only consider using independent fragments (or ListFragments) for this if you are going to re-use that fragments in other activities to avoid code duplication.

Android EditTexts AutoSave

I have a database object which includes a lot of String fields. Right now, these string fields are managed by the user through an activity with a lot of edit texts. However, I'm having trouble saving all of the information from my EditTexts to a database. I've tried doing this when the activity calls onPause but it is not working how I would like (I'm using a ListView with the EditTexts so it's hard to say if the views will be there). I've been looking at the text watcher but it seems really tedious to add one for every EditText, some of which are created dynamically.
I've considered extending EditText and implementing something to use the TextWatcher but I'm again not sure about the best way to go about this.
Anybody have any suggestions on how I can accomplish this? Thanks for the help.
I would say you should hold a reference to each of these EditText objects in an ArrayList and then use an array list adapter to provide the data to your listview.
when any of the EditText's are changed you can call notifyDatasetChanged on the adapter.
in the onPause method you can loop thru the ArrayList and save each to your database.
Couple of ideas spring to mind:
1.
Implement a save button at the top/bottom of the list view or in the action bar.
The save button onclicklistener would grab each text in the list view and update the relevant fields in the database.
Place the saveButton work in the UI thread so it will block until completed and the activity won't be destroyed. you could also create a progress dialog just to let users know it's being updated.
2.
Implement a custom adapter for your listview and in the getView() method of the adapter (you have to override this anyway) add a text watcher dynamically... that way it's not all that arduous for you to add them individually

How to handle ListView in this situation Android?

I ran into the situation that I need a way to edit the data of list-view item from another activity. I can handle the click event on each item, then edit it on the fly. However, I still prefer to handle all the editing in a separate activity. My listview item is customized from BaseAdapter.
Here is my main page,
Each item within the ListView, contains two other TextView. When the I hit the menu setting, it will go to another activity where I can edit and update the information:
I'm currently having two solutions in mind. Either retrieving data from the previous activity and update the whole ListView (which I think it's kinda expensive in the case user just edit one item). Or I can just get rid of the ListView and create a bunch TextView in the xml, by doing this I can just reference to each TextView by their id. So my question is, which way is preferred in this case? Any suggestion would be greatly appreciated.
Your ListView is displaying Email, Name, Headline, etc? That should be a fixed XML layout with TextView entries, I think. ListView is for variable numbers of elements. You CAN implement it with a ListView, but I wouldn't.
However, your concern about updating the whole list being overkill, I wouldn't worry about that either. You're talking about 7-10 fields. The amount of time Android needs to run through its lifecycle and display everything will dwarf you updating a few fields.
You can use SharedPreferences for this. You can create a wrapper class through which you can access the preferences.Thats the usual way to go about solving these kind of problems. You can check this for details.
You can have it as a variable in your application class, so that you can access that in a global context.
Use text views instead. List View code has been optimized for large amounts of data only and not recommended for small data.

Android ListView not retaining values through orientation change

I am a bit perplexed on what Android holds onto in a view orientation change and what it doesn't.
Some things it seems it holds onto nicely, other things it seems it doesn't.. So I am not sure if it is my code, or something else. (I assume I am doing something stupid)
What I have is a view with a listview in it, the listview has a simple adapater and an array of map items to put into the listview.
When the orientation changes, I know that the activity is destroyed and recreated, so I assume that my simple adapater and its tie to the listview are gone.. so the listview won't rotate populated and I'll need to use the onRetainNonConfigurationInstance or onSaveInstanceState methods to pass through either the adapter or the list of values?
This seems excessive, I can't believe the sharp guys at Google don't have an easier way to handle this.. The listview has an ID, and after orientation I can add items to it and it works fine, but it just doesn't hold onto the values it had before.
Is my only way around this to pass the list content through explicitely? Is it the fact I am creating a new SimpleAdapter and linking it to the list in the onCreate that is causing the issue? Or is it the fact the array of Items I have linked to the adapter is whiped out when the activity is?? This seems like something that shouldn't require this much work to accomplish.. am I missing something?
No, you could also have a persistent model through Activity lifecycle like this :
define a singleton app, with a method to get your singleton model.
At on create get your datas from your model.
As your model will no be recreated but will persist, this can be used as a workaround for preserving objets through rotation change.
Regards,
Stéphane

Categories

Resources