-2

I create a table dynamically, and inside the table I create a spinner dynamically. When a user selects a string using the spinner, I want to know what was selected.

the GUI: enter image description here

the dynamic created spinner:

TextView t7v = new TextView(this);

            /**********ADD SPINNER*******/
            ArrayList<String> spinnerArray = new ArrayList<String>();
            if(tempBranch[i].equals("karmiel")) {
                spinnerArray.add(" " + tempBranch[i] + " ");
                spinnerArray.add("batyam");
                spinnerArray.add("natbag");
            }else if(tempBranch[i].equals("batyam")){
                spinnerArray.add(" " + tempBranch[i] + " ");
                spinnerArray.add("karmiel");
                spinnerArray.add("natbag");
            }else{
                spinnerArray.add(" " + tempBranch[i] + " ");
                spinnerArray.add("batyam");
                spinnerArray.add("karmiel");
            }
             spinner = new Spinner(this);
            spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
            spinner.setAdapter(spinnerArrayAdapter);
            tbrow.addView(spinner);

            /*********END ADD SPINNER*****/

            final Button rowButton2 = new Button(this);// create a new button
            //Button OK
            rowButton2.setText("OK");
            rowButton2.setId(i);//set the number of count
            // b.addView(rowButton);
            tbrow.addView(rowButton2);
            // save a reference to the button for later
            myButton[i] = rowButton2;
            myButton[i].setOnClickListener(btnClick2(rowButton2));//click the button

the Button when I want receive the value:

private View.OnClickListener btnClick2(final Button button) {//clock button that create dynamic
        return new View.OnClickListener(){
            public void onClick(View v){

                //create array
                idEmpl = id[v.getId()];
                tmpBranch = spinner.getSelectedItem().toString();//NOT WORK TEMPORARY


                String type = "changeTempBranch";
              /*  ManagerListInBackground managerListInBackground = new ManagerListInBackground(v.getContext());
                managerListInBackground.execute(type,idEmpl,tmpBranch);//send the data to valid*/
            }
        };
    }
evgeny
  • 51
  • 11
  • *(How to get) value from dynamic spinner in android?* ... in the same way as from not dynamic spinners - and such question was asked multiple times already – Selvin Jan 16 '18 at 15:26

2 Answers2

0

Try this answer: https://stackoverflow.com/a/1714426/9217218

Then inside the onItemSelected method, add the you can get the string value like this:

String selectedItemTitle = mySpinner.getSelectedItem().toString();
Luke C
  • 140
  • 2
  • 12
0

So with help of IdleApps Inc and his link:enter link description here

I solved my problem, that is answer:

enter code here/* Value Spinner */

            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                   /* Toast.makeText(parent.getContext(),
                            "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
                            Toast.LENGTH_SHORT).show();*/

                    tmpBranch = parent.getItemAtPosition(pos).toString();
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {
                    tmpBranch = spinner.getSelectedItem().toString();
                }


            });

        /* End of Value Spinner */

that code inside in the build dynamic table, so be careful, that not will work in another place (for me minimum) thank you to everyone!

evgeny
  • 51
  • 11