Does exist a library that receives multiple edit text objects, get the text from them, and put them in new object? Instead to call getText for every field separately.
You can try Android databinding library. It help you to bind your data (as a Simple POJO) with views.
You should use RecyclerView to achieve your task, add EditText in your item layout file and in editText add addTextChangedListener listener which gives you 3 methods, in afterTextChanged save string to where you want, may be in an array.
Related
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
I have vector of EditTexts that I'm dynamically generating and dynamically adding addTextChangedListeners.
The issue is that the returned Editable on "afterTextChanged" doesn't let me know which EditText this listener is associated with.
Is there a way to call the respective EditText from within the TextWatcher?
Thanks,
I would suggest creating your own TextWatcher but before I put down my untested pseudo-code here, I discovered there's another guy named Sebastian Roth who had already done that.
Check out his answer:
TextWatcher for more than one EditText
I have an array of EditTexts called ArrayEt with 25 elements.
How can I get handles to the widgets using the findViewByID method and a for loop (their ids are strings) ?
You should probably create these editText programmatically in your onCreate method and add them to the view.
You can use Resources#getIdentifier(), but be warned that it could be very inefficient.
i have been trying to filter my list view all day with referring SO solutions but was not able to do it. Hence i am pasting the code below for your reference to know exactly what i was doing.. please some one help me solve it and if possible edit my code or point out my mistake.. the list does not get filtered. thanks in advance
The problem seems to be in your publishResults method. The results obtained from performFiltering are never applied to the object list inside the array adapter. The easy way to do this is to create a new ArrayAdapter with the filtered items and update your list view.
The default behavior of the array list is to filter on a prefix. If this is ok, you could just call setFilterText on the list view with the text to filter on without having to implement your own filtering.
Also, as a side, you might want to move your Filter newFilter = null; to outside your method. Otherwise you are creating the filter every time. And should not need to set constraint to the contents of etsearch inside your performFiltering method since you are invoking the filter with the text from the text watcher which should have the same string value.
I'm trying to come up with a scalable way to link two edit text boxes together for unit conversion. The user would enter a value in either of the boxes and the converted value would show up in the other.
I would normally just make it so when one EditText was edited, it would get the input, pass it through a method to convert the value and set the text of the other field.
The problem is I have a lot of these pairs and each pair is used to convert a different kind unit. If I used a TextWatcher for each box, you can see where that would start to get out of hand.
I thought about extending the TextWatcher so I could pass it the EditText View and its partner's view, but I'm not sure how to pass it what kind of conversion method needs to be used. I could assign an int to each kind of conversion and use a switch, but that doesn't seem like a good solution to me.
Is there a better way?
This is how I would do it. Create a class, say PartnerEditTextInfo ,which contains a reference number and a EditText obj. Attach this as a tag to every EditText in your app. Set the editText in the PartnerEditTextInfo to the partner editText and have a unique reference number. Thus every EditText can get a hold of its partner.
Extend the EditText class and over ride onTextChanged() method to call a common conversion method(This can be a static class static method) . You can get the partner editText by getting the tag object of the editText whose text has changed. Then ,based on which pairs of editText(based on reference number) is changed apply required conversion formula and do the setText on both the editTexts in the pair.
Caution- You need have a way to make sure you wont get into a infinite loop, have some sort of flag to differentiate changes to editText's text made by user and made by the conversion method.