1

I created a form using a ListView of EditTexts and Spinners.

It was because I had to provide a "Add a new item" button which would insert more copies of the EditText, Spinner and TextViews. Thought it was easy to do in a ListView.

Considering ListViews recycle Views, how to i iterate over all the form elements?

hoshiKuzu
  • 866
  • 1
  • 12
  • 30

5 Answers5

0

I can give u an idea for this,

Initially while populating the list view, you would use some arraylist of hashmap or some pojo, while doing this if u initially having one row u just need to add one item to that array list and populate the list view.

1.For the Add row button give the functionality like, to that arraylist add one more item and notify the adapter.

2.To save the edit text Data, on text change for the edit text or if u r having button over there to get the text from the edit text, give functionality like, get the item from arraylist at that position which u r getting in the getView() method. and edit that item with ur current data.

If u need more clarification comment the doubts if u have any.

Tamilselvan Kalimuthu
  • 1,534
  • 1
  • 13
  • 28
0

There are only a few adapter-views in existence at any time, due to view recycling. Roughly just enough to fill up the screen. As such, they only hold a slice of your data at any given time -- iterating over them will not work.

The only option is to iterate over the objects that feed your views/adapter data (e.g. lists, maps, cursor, etc). If these views are altered after being presented to the user (e.g. storing input) then this input must also be cached as well; this should already be setup because view recycling would otherwise cause this input to be lost.

NameSpace
  • 10,009
  • 3
  • 39
  • 40
0

Your problem will be solved with this code: ArrayListarrStr = null;

for perticular position this will work :

 View view=listView.getChildAt(position);
    EditText editText=view.findViewById(R.id.editText);
    String string=editText.getText().toString();

but when you want to get for multiple position this will work :

arrStr = new ArrayList<String>();
for(int i=0;i<items.size();i++){
    View view = listView.getChildAt(i);
    EditText editText = view.findViewById(R.id.editText);
    String str = editText.getText().toString();
    arrStr .add(str);

}

and you will get all values from listView in ArrayList<>.

chirag patel
  • 148
  • 1
  • 9
0

I finally ended up using ListView inside a ScrollView. I dont want it to recycle views, since, well, you need to get all the data from every item, and also, its a user form. Usually the listview will contail 3-4 items. I dont think the user will create so many items to cause any performance issue.

Since a ListView with height wrap_contents, inside a ScrollView will cause it to display only the top item, I had to use this hack here https://stackoverflow.com/a/19311197/1067596

Community
  • 1
  • 1
hoshiKuzu
  • 866
  • 1
  • 12
  • 30
-1

Try to create an id with looping for the number of times you are creating new items...Eg. Name your et_number_1,et_number_2,et_number_3...which you can easily generate having

"et_number_"+i

So Again this way you can also access the corresponding edittext value for the number of times you have created. Lemme know if it helps. And if you need any more clarifications.

Legendary Genius
  • 310
  • 3
  • 20