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.
Related
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.
Is there any way in Android by which we get something like Map and List or any other collection object that give us object of all the fields (like Text-view, Edit-text, Button and any other widgets) that has been used or initialized in an Activity?
What you'd have to do is get a hold of your root view in your xml using getViewById(), and next recursively get its children using getChildCount() and getChildAt() until you have them all.
Note that the getChildCount method only applies to ViewGroups (these are things like LinearLayout, RelativeLayout,...).
Look here for a possible duplate.
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 know I can pass a single variable with android:tag="x" and retrieve it with view.getTag().
How can I pass and retrieve more than one?
EDIT: What I was trying to do was use the tag attribute in a button to pass a value to the method invoked when it is clicked. I was wondering if it is possible to use two or more tags in a Button element. I have since discovered that I am going about it the wrong way (I'm an android noob). I was trying to use the Button in the xml to store button specific data when I should have given it an id, and then used the id in the code to differentiate the buttons.
You can declare global variable and set values of that variables and can use in button onClick(...) method
I'm wondering if there is a way to hide a field in Android.
I tried with setting the value in a TextEdit and then making the TextEdit invisible, but the result is that the value is invisible, but the control takes space.
In my case, I want to store an extra value in a row of a ListView.
Is there another solution besides using hidden fields?
Use View's public static final int GONE field.
In your case textEdit.setVisibility(View.GONE), or in xml android:visibility="gone"
Setting the view to INVISIBLE does not take layout into consideration, but GONE does.
View has methods setTag() and getTag() that you can use to associate some extra data with row of ListView. For example I'm using CursorAdapter class and in newView() and bindView() methods I call view.setTag(). Then in OnItemClickListener I call view.getTag().
I bet the only invisible data contained by rows of ListView is id (long). I was also trying to find a way to pass some data (like uuid) to the row click handler, however seems a "GONE" TextView is the best solution for now...