1

Here's the thing, I have a layout where I create someviews according to some data I get from a webservice. There are some edittexts, textviews, spinners, etc.

Its kind of a poll, so after I crete the layout and the options I have to fill it and send the data to the server again.

For this I put objects on the spinner, Im using the adapter suggested in this thread https://stackoverflow.com/a/8116756/6323705

My problem is how to get the spinner values at the end? Because when I use this

 mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view,
                int position, long id) {

            User user = adapter.getItem(position);
            // Here you can do the action you want to...
          //BUT I DONT NEED TO DO ANYTHING HERE, I NEED TO DO IT AFTER ALL FIELDS ARE FULL
        }
        @Override
        public void onNothingSelected(AdapterView<?> adapter) {  }
    });

it only saves the last spinner values, and per layout I have like 4 or 5 spinners all differents.

To get the data from layout and then send it Im using something like this:

int childs = parent.getChildCount();
     for(int i=0;i <= childs;i++){
        View v=parent.getChildAt(i);
        if (v instanceof EditText) {
            EditText et = (EditText) v;
            //then save the data in an special Array
        }
            else if (v instanceof TextView) {
                TextView tv = (TextView) v;
                //save it in an other Array
            }

             else if (v instanceof Spinner) {
           Spinner sp = (Spinner) v;
            //putting OnItemSelectedListener here is not working either
            }

How can I do this?

Community
  • 1
  • 1
  • Did you try `String text = sp.getSelectedItem().toString();` ? – greenapps Nov 07 '16 at 21:45
  • Yes, when I use it the creation of the spinner, it only saves the value of the last spinner on the layout. When I use it when I try to get all the data, it doesnt work, cause it never enters to that point for somereason. – Daniel Arteaga Iriarte Nov 07 '16 at 21:59
  • You mean: v is never an instance of Spinner? – greenapps Nov 07 '16 at 22:21
  • Yes it is, but if I put `sp.getSelectedItem()` it only takes the value of the LAST spinner, always. If I put something like `sp.setOnItemSelectedListener(new OnItemSelectedListener() {.....}` after I ask if its instanceof Spinner doesnt work, so never enters to the method – Daniel Arteaga Iriarte Nov 08 '16 at 19:49
  • You do not need an `OnItemSelectedListener()` for the thing you want to do. So throw it away. Its only confusing. You dont have to do anything. Only use the code of my first comment if the instance is of Spinner. If it does not work then your adapters are wrong. Or your choice items. – greenapps Nov 08 '16 at 22:11

2 Answers2

0

I have same requirement in app, And i Uses this and working fine.

List<View> allVIEWS=new ArrayList<>();
int spinnerValue=0;

this list contains all dynamic views.

So your are creating dynamic spinners, so when you defines spinner dynamically, add this line in that code.

spinner.setOnItemSelectedListener(this);

and in override metod:

@Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        spinnerValue=0;
        for (int noOfViews = 0; noOfViews < itemDetailses.getOption().size(); noOfViews++) {
            if (itemDetailses.getOption().get(noOfViews).getOptType().equals("-2")) {
                Spinner s = (Spinner) allVIEWS.get(noOfViews);
                sp1v = itemDetailses.getOption().get(noOfViews).getValues().get(s.getSelectedItemPosition()).getOptPriceDiff();
                spinnerValue+=Integer.parseInt(sp1v);
            }
        }

    }
Divyesh Patel
  • 2,576
  • 2
  • 15
  • 30
  • sp1v = itemDetailses.getOption().get(noOfViews).getValues().get(s.getSelectedItemPosition()).getOptPriceDiff(); Could you explain me that line? I dont understand it pretty well. Also, I think the problem is the adapter, when I create the Views, the adapter contains the last Array Im using to fill SPinner – Daniel Arteaga Iriarte Nov 08 '16 at 19:46
  • sp1v is string value that contain spinner item value from respective spinner. Here, noOfViews is number of selected spinner. S is spinner that is currently selected. Then get value from ArrayList according that position. – Divyesh Patel Nov 09 '16 at 03:54
0

You can override an inbuilt method for Spinner called onItemSelected

Example:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    final String item = parent.getItemAtPosition(position).toString();
}

The string 'item' has the value of the spinner item selected. Hope this helps!

Suhayl SH
  • 1,213
  • 1
  • 11
  • 16