addTextChangedListener get EditText dynamically - android

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

Related

Way to get multiple editext text?

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.

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

Dynamic Layout Creation inside on Focus change

I need to add some fields (textview, edit and checkbox) dynamically to a tablelayout on focus change of edittext which is already present and visible. I need to validate the text scanned and if validated need to populate the views below for user to scan....
Could please suggest a good way to go about this? I have written the onfocus change validation logic.. but unable to manipulate the tablelayout within focus change
Many thanks in advance.

Linking two EditText boxes for unit conversion

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.

Array of EditText controls

It looks like a rather common thing.
I am trying to display a list of EditText controls, like
Label1 EditText1
Label2 EditText2
…
so that user can edit text “in-place”, and this list has to be shown in Dialog (AlertDialog?).
I am using ArrayAdapter to fill the list. The problem is that I can not find the right EditText for its label to get the user-modified values.
BTW, It’s hard to believe how many passes the system makes filling the list with getView(). It is also being called when I click on one of EditText.
Maybe my approach is wrong and there is a different way to do this?
Take a look at this example. Note the use of ViewHolder and how they package groups of elements using a container like class and then set the Views tag to point at that object. There is an interesting discussion here as well. To be clear, ViewHolder is just a class you create and define and is more of a pattern then an answer. But it can provide a performance improvement as well as an organizational improvement in many cases. The example may give you other insight into what you are working on.

Categories

Resources