Android EditTexts AutoSave - android

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

Related

Android validate items inside RecycleView

I'm having a big form inside a recycleview, with multiple repeating items. Think of it like multiple forms: data of car 1, data of car 2 etc. under each other.
Now I'd like to validate all data before saving. Basically the problem is, that the views might not be visible when the user presses the save button.
What's a working way to do this?
My idea would be to scroll manually to each form viewholder THEN validate those, and just stop at the first invalid cell.
But is there a better way? Because this is a bit hacky to me. Is there a way to force a recycleview to actually create a view without it being visible on screen?
Thanks
You should do your validation things on your dataList, for example you have a big array in your activity and you passed it to your adapter.after user clicks on save button you should start validating process on your list(not your adapter).
for example in your Activity you have an ArrayList of car object (carList) and you passed it to your adapter.after clicking on save button you should do something like this in your activity class:
for(int i=0;i<carList.size();i++)
{
if(!isDataValid(carList.get(i)))
//do your stuff here
}
//notify your adapter here

listfragment android layout

I'm trying to create a layout in my fragment that allows a user to enter their workout. The fragment has an ArrayList of sets, and each set consists of an exercise name, sets/reps, and rest time. I can only populate so many sets at the beginning, and want to add a button at the bottom 'add exercise' in case the user wants more exercises than what the loaded layout allows...example..
Ex #1
sets/reps
rest time
Ex#2
sets/reps
rest time
[add exercise]
I tried using a listfragment and an adapter, but im not sure how I can withdraw the data that is entered into the edit text fields and save them for later use. Any help is greatly appreciated, thanks in advance.
Using adapter is a good idea.
Here is one way to achieve your goal:
When person clicks on add exercise button, you should get the new exercise information from the user. You can use Dialog or another activity-screen for that.
Once you got the info(sets/reps and rest time). Go and update the ArrayList that populates the data in the adapter. And when you done call: Adapter.notifyDataSetChanged() this will make the adapter to invalidate the view based on the changes in the data(ArrayList in your case).

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 Listener?

I am wanting to display a text field stating that my listview is empty if there is nothing in my list. I know this can be accomplished quite simply using an android:id/empty textview in the xml file, but this also requires that I extend my activity with something other than Activity and I don't want to do that.
I guess my only solution is to create a textview and then set its visibility to gone when my code detects that something has been added to the listview. I can simply check the array that populates the list, but is there some sort of listener so I don't have to run a checkListIsEmpty() constantly throughout the code. I was hoping I could use something that would simply sit quiet and wait for the listview to become populated and when that happened change the visibility to visible and then begin waiting for it to become empty again.
Or you can use setEmptyView(View v) on your ListView in your Activity.

Refresh view display when underlying data changes

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.

Categories

Resources