Android validate items inside RecycleView - android

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

Related

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).

Array Adapter for an AdapterView that only shows data one way, (Android)

I get data from my server returned as an array of objects. Each object is itself an array of strings that describe the object.
For example let's use cars as the object. In this case, the array of strings are descriptors like 'year built', 'horsepower', 'automatic or manual', 'color', etc etc.
What I'd like to do is display only 1 car at a time for the user. The user can choose whether he likes or dislikes the car. Either way, a choice is final, and the next car will show up. Also, the user should not be able to go back to the previous car (not with a swipe or a clicking of aback button). In other words, he can never see his choice on the previous car again.
If I make a call to my server on every single 'like/dislike', this will be a very slow app. If I inflate a bunch of data into many views that are out of sight, it will also be a very slow app.
Most Array Adapter examples I see online illustrate how to show data bi-directionally. They are viewpagers or listviews that you can swipe left (or up) to view data that has been previously already viewed. This is not what I'm looking for.
Is there a proper way to implement what I'm trying to do? Any help will be appreciated as I'm just hoping to get some direction and can implement on my own. So far I'm thinking about possibly a viewpager that deletes items as you view them, but I have a feeling this will be really hard to manage the position of the views...
Use an array to hold the data you get from the server. You can request that the server send you 10 or 25 or 50 at a time.
Since you only want to show the user 1 at a time, and he can't scroll or swipe through the list, you only need a single set of views (enough to show all attributes of a single entry). You don't need an array adpater for this. Whenever you step from one entry to the next you just need to adjust the index into your array and then copy all the attributes of the new item into the individual views with setText() or setImageBitmap() or whatever.

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

Changing an activities setContentView() in Android

I just have a question regarding changing an Activities layout.
Basicly my problem is:
I have a list view populated with a series of Strings.
I have an OnItemClick event assigned to the ListView,
When the user clicks an item in the list view, I want the current layout to disapear and an image to take its place.
From reading other posts, I understand that the recommended way is to set a separate activity for each item in the ListView, however, seeming as all the activity is doing is displaying an image I think it would be a waste of effort to set up a separate activity for each item in the ListView...
Could anyone give me some help on this?
thanksin advance.
Shaw
You really should create a new activity (ar a new fragment if you have space, but this is another question) which displays the image, for at least the following reasons
it is very, very simple to code and mantain such a solution. What if tomorrow you want to add 2 buttons and some text and maybe a menu for that image? you have your brand new activity to edit and upgrade without risking to damage your list activity.
it is more user friendly. If the user presses back when the image is shown, with 2 activities he will be back to the list, with your solution he will go back BEFORE the list, and this is not what he'd espect
remember this piece of advice: 1 activity = 1 simple task or interaction with user.
list + image display = 2 activities (or fragments)
PS: you do not need to define a different activity for each list item, just pass with the intent to the "ImageActivity" and specify there which image to show!
EDIT 2: to pass to the next activity your current selection, just use the putExtra(String key, T value) (T may vary, check documentation) method of Intent class.
example: intent.putExtra("imgCodeSelected", index) where index is fetched by the onItemClick event. You can put as many extras as you want as long as they have different keys.

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.

Categories

Resources