0

I have a list view and the list contains 2 items, a TextView and a Button/EditText based on some logic as as 2 columns in a table row.

Now in the layout, where I have listview, I also have some button say Save. When I click Save, I should retrieve the values of Button/EditText in the Activity where Listview is residing and need to store them in db.

I am not able to figure out this situation. There could be many list rows, say if there are 5 list rows, then need to retrieve each of the values from the positions 0-4.

Please see the screenshot below to know how the list view looks.

Hope you can see the fields on the right side there. Next to COmments is the Edit text and next to Ship Date is a button.

Can anybody please help me or suggest me something?

enter image description here

Update:

I am now iterating the list view as below,

    View vr;
    EditText ee;
    for (int i=0;i<=list.getLastVisiblePosition()-list.getFirstVisiblePosition();i++) {
        //vr = list.getChildAt(i);
        vr = list.getAdapter().getView(i, null, null);
        ee = (EditText) vr.findViewById(i);
        System.out.println(ee.getText().toString()); 
    }

Edittext ee is always null in this case. I have tried uncommenting vr = list.getChildAt(i); But still it returns null.

Here there can be either Edittext ee or some other button in the same i position based on some requirement. But I'm not understanding why ee is null.

Referred the answer here Iterate through ListView and get EditText-Field values.

Any thoughts where I am going wrong?

Community
  • 1
  • 1
tejas
  • 2,435
  • 10
  • 37
  • 54

2 Answers2

0

You can do this by storing the button/edittext text in your custom list adapter as a list. A OnFocusChangeListener or a TextWatcher can be added on the EditText view to update the list in the adapter. The link below will get you started.
EditText in Listview

oks16
  • 1,294
  • 11
  • 16
0

What I understand from your requirement is, you want to store the values from the TexViews on your List rows to db. And you want to do that on the tap of the button 'Save' which is on the main Activity which has the ListView.

Well you may define an ArrayList on your main activity

List<Integer> listOfDataFromTVsOnList = new ArrayList<Integer>(); 

And then on Change/Focus listener of your TextView which is a part of the List row, you may assign the value of TextBox in question to the ArrayList we defined on the Main activity as:

((YourMainActivityClass) contextPassedToTheListAdapdter).listOfDataFromTVsOnList.add("Data on the TextView of the Row");

This way when all the rows are finished editing (this actually depends on your business logic) the ArrayList would have the entire data set from the TextViews from all the rows of your List. Now on the tap of the save button you can iterate over listOfDataFromTVsOnList and save the data as individual records in the db.

Souvik Ghosh
  • 731
  • 8
  • 22
  • yes what you understood about my requirements is perfect!! But here I cannot assume that there will be only TextView. It can be either a Button/EditText – tejas Jul 05 '13 at 08:03
  • so you mean to say, When I tap on Save all the data will be available in listOfDataFromTVsOnList? I will try this – tejas Jul 05 '13 at 08:04
  • Yes, it can be any type of control as long as your are using the proper event to get the data from it, in fact I had EditText in mind when I answered your the question but referred to TextView (focus listeners won't help there) wrongly. – Souvik Ghosh Jul 05 '13 at 09:30
  • I'm confused where to perform this? ((YourMainActivityClass) contextPassedToTheListAdapdter).listOfDataFromTVsOnList.add("Data on the TextView of the Row"); in List Adapter or Activity? I guess it should be in adapter – tejas Jul 05 '13 at 09:39
  • Obviously on the adapter, use the focus listener of the EditView and store the value from it to the ListArray. – Souvik Ghosh Jul 06 '13 at 07:35
  • Ok, I guess list listOfDataFromTVsOnList should be of String, right? – tejas Jul 08 '13 at 09:50
  • Yes, List listOfDataFromTVsOnList = new ArrayList(); I quickly used one example from my code base! – Souvik Ghosh Jul 08 '13 at 11:21
  • No, problem. I wanted to confirm, datz it :) – tejas Jul 08 '13 at 11:23
  • I have one problem here, the list need not contain data in the same order as the items are placed in the list right? I want them to be in the sequential order as they are sorted in the listview. – tejas Jul 09 '13 at 04:10
  • This is tricky for edittext field. Because unless normal button/other field. I cannot get values of edittext and set it to the List using setonclicklistener. Each time you click a edittext, it will be an empty string. SO if i add this, list has only empty strings at the end – tejas Jul 09 '13 at 05:31