I have listview of edittext boxes. I also have a button to add a new row into the list, thus, the list length is dynamic.
However, when I entered text into the edittext and press the add-button, my input disappears. I think my input is not captured when I added a new row into the list. How can I save my input so when I change the amount of rows, my text remains?
Your idea is fine, but you need to persist the edittext's data. Try adding textwatcher to each edittext and save the text for each edittext in a array or something
A ListView recycles its row views anyway, so this would still be a problem without changing the list length. Rows that scroll off the visible portion of the ListView would lose their contents. To fix the problem, text entered into the EditText needs to be stored in the underlying data model, not just in the view.
You'll probably need to add a TextWatcher (with addTextChangedListener()) to each EditText. When one of the TextWatcher callbacks is called, get the text from the EditText and save it into the data model.
To keep track of which data element to save to during the TextWatcher callback, you might use a technique similar to the one used with the RatingBar in this CommonsWare sample.
Related
Suppose we have a bunch of files. I want to show them in a list that each item has some editable fields, user fills them and at the end, clicks on save button to make changes to items.
I tried a ListView with custom adapter having some EditTexts on it. but when I try to save, I don't know how to retrieve edited fields for each item and save changes to them. I think ListView is not for that purpose. It is just for representing items for user. What should I do?
Yes you can do this. Create arraylist of models which will hold all values inserted into editable and this arraylist will be in the activity class where listview is implemented. Then in list adapter handle click for editable based on position and whatever value is there in editable store it instantly in arraylist. That's it !
Need to implement edit in runtime. Which one is the best way to achieve it.
Edittext in Listview or dynamic table layout(inflating row xml) with edittext
Update:
My listview contains 7-8 view(Text view) in a list item. On click edit button using view switcher changed textviews to edit text. To get the entered value in edittext listening onfocuschanged. It brings very slow performance. Any better way to achieve it?
Update:
If my listview have 100 list items. Each item having 7-8 edittext. Need to listen all the edittext focuschange. My app hangs. What should i do?
EditText within ListView can cause you great grief down the lane given that views are recycled in listview.
Say you have tapped on the second row in a listview where all item rows contain a edittext and you have set adjustResize in your AndroidManifest.xml; after the soft keyboard pops up, the focus goes into the first view that can accept the focus which in this case will be the first edittext (row) of your listview. Yes, you can tap again on the desired edittext to regain focus. But it is an annoyance nevertheless. If you set adjustPan, then I have seen the the problem does not exist as such; if I recollect correctly, you cannot scroll down all the way to the end of your list. Again, another annoyance.
I'd suggest, you go with a ScrollView if the number of items in the list are less. I have been trying to solve this for the last couple of days - I ended up doing this - I replaced the edittext's with textview's in the listview; tapping on the textview would bring up a dialog fragment that contains the edittext.
Have you looked into the concept of a ViewHolder to keep a reference to the items of the listview? That should solve your multiple focus listener problems.
I have a listView with a edittext in all the rows of the listView and i want get all the changed editText values when i clicked to a button which is in same layout but is not part of the listView.
Please help me.
If you built your Adapter properly, your EditTexts will be recycled, so you won't have one EditText for all of your items. Instead, you should be saving the data to the backing array every time an EditText is updated (using a listener). When the user clicks on the button you should read the data from the backing array, not the actual ListView views.
I am developing an application for Android, and for that I am trying to make a ListView act in such a way, that when a user presses an empty entry, he can start typing text directly into that empty entry, and that when the user touches any other part of the screen, it is saved. Is there a way to do this? I was thinking of using onClick somehow, but I have no concrete approach.
Here is one basic, general approach:
For each row of the ListView, create a layout that has a visible TextView and a EditText with the visibility set to gone.
Use an onClickListener for each row to swap the visibilities of the TextView and EditText (respectively, gone and visible) when the row is selected.
Track the active row for clicks to another row or background.
When the active row changes, set the value of the EditText to the TextView and return them to their original visible states.
Other approach that could be taken is using the View.OnFocusChangeListener and the TextWatcher you can get more detail about them here http://developer.android.com/reference/packages.html
I have a GridView that displays several dozen rows worth of a custom layout, each of which consists of an EditText and a TextView object.
If I understand correctly, I should extend the BaseAdapter class to accomplish that. If so, how can I get access to a specific EditText object?
Also, will recycling of views cause me to lose the text that the user has entered if one of the EditText views is no longer visible?
Is there a simpler means to accomplish what I am describing here (perhaps something other than GridView) ?
When you extend BaseAdapter one of the methods you have to override is getView(...). In this method, you create the view that needs to be shown. If you want to persist text that is entered in the EditText, what you need to do is set a a TextWatcher on the EditText, passing in the position of the EditText (parameter in getView(...)), and whenever the text is changed, save the text in an array of sorts. Then, whenever that position comes back through the getView(...) method, grab the text from the array and populate the EditText.
You should use an underlying data structure ( say a List ) which should hold the data for each element of the Grid.
TextView and EditText should be populated from this List.