0
votes

I have created the dynamic spinner with values in dynamic layout.Spinner will be displayed by button click.

If I displayed the more than one spinner how to get selected values from all spinner.

preview

Code: public class MainActivity extends Activity { String[] itemlist = {"Select Item", "Apple", "Mango", "Grapes"}; int[] itemrate = {0, 10, 8, 6}; Spinner ItemSpinner; Button btnPlus, btnMinus; int i = 1; LinearLayout main; List row_list = new ArrayList(); List qty_list = new ArrayList(); TextView txt_nettotal,txt_qty;

String text;
String pres_doctor;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    main = (LinearLayout) findViewById(R.id.main);
    txt_qty =new TextView(this);

    addNewRow();
    Button btnadd = (Button) findViewById(R.id.add_new_row);
    btnadd.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            addNewRow();
        }
    });
    Button btnSubmit=(Button)findViewById(R.id.btn_submit);
    btnSubmit.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {






        }
    });

}

private LinearLayout CreateSpinner() {

    final LinearLayout rowLayout = new LinearLayout(this.getApplicationContext());
    final TextView serialNumberText = new TextView(this);
    serialNumberText.setText(String.valueOf(i++));
    ItemSpinner = new Spinner(this);
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, itemlist);
    adapter1.setDropDownViewResource(android.R.layout.select_dialog_item);
    ItemSpinner.setAdapter(adapter1);

    text = ItemSpinner.getSelectedItem().toString();

    final EditText ItemQuantity = new EditText(this);
    int x = 0;
    ItemQuantity.setText("" + x);
    ItemQuantity.setTextColor(Color.GREEN);
    ItemQuantity.setInputType(InputType.TYPE_CLASS_NUMBER);
    ItemQuantity.setGravity(Gravity.CENTER);

    final EditText ItemPrice = new EditText(this);
    ItemPrice.setText("0");
    ItemPrice.setTextColor(Color.GREEN);
    final EditText RowTotal = new EditText(this);
    RowTotal.setText("0");
    RowTotal.setTextColor(Color.BLUE);


    AdapterView.OnItemSelectedListener onItemSelectedListener1 =
            new AdapterView.OnItemSelectedListener() {

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

                    pres_doctor = ItemSpinner.getSelectedItem().toString();
                    RowTotal.setText("0");
                    String val = String.valueOf(itemrate[position]);
                    ItemPrice.setText(val);
                    ItemPrice.setGravity(Gravity.CENTER);
                    int Quantity = Integer.parseInt(ItemQuantity.getText().toString());
                    int Price = Integer.parseInt(ItemPrice.getText().toString());
                    RowTotal.setText(String.valueOf(Quantity * Price));
                    txt_nettotal = (TextView) findViewById(R.id.textView);
                    txt_nettotal.setTextColor(Color.BLUE);
                    int value = 0;
                    for (EditText editText : row_list) {
                        int list = Integer.parseInt(editText.getText().toString());
                        value += list;
                    }
                    txt_nettotal.setText(String.valueOf(value));

                }


                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                }

            };
    ItemSpinner.setOnItemSelectedListener(onItemSelectedListener1);


    btnMinus = new Button(this);
    btnMinus.setText("-");
    btnMinus.setTextColor(Color.BLACK);
    btnMinus.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int oldValue = Integer.parseInt(ItemQuantity.getText().toString());
            int newValue = 0;
            if (oldValue > 0) newValue = oldValue - 1;
            ItemQuantity.setText(String.valueOf(newValue));
            ItemQuantity.setGravity(Gravity.CENTER);
            final TextView txt_nettotal = (TextView) findViewById(R.id.textView);
            txt_nettotal.setTextColor(Color.BLUE);
            int value = 0;
            for (EditText editText : row_list) {
                int m = Integer.parseInt(editText.getText().toString());
                value += m;
            }
            txt_nettotal.setText(String.valueOf(value));

        }
    });

    btnPlus = new Button(this);
    btnPlus.setText("+");
    btnMinus.setTextColor(Color.BLACK);
    btnPlus.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int oldValue = Integer.parseInt(ItemQuantity.getText().toString());
            ItemQuantity.setText(String.valueOf(oldValue + 1));
            ItemQuantity.setGravity(Gravity.CENTER);
            final TextView txt_nettotal = (TextView) findViewById(R.id.textView);
            txt_nettotal.setTextColor(Color.BLUE);
            int value = 0;
            for (EditText editText : row_list) {
                int m = Integer.parseInt(editText.getText().toString());
                value += m;

            }
            txt_nettotal.setText(String.valueOf(value));

        }
    });
    ItemQuantity.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            try {
                int Quantity = Integer.parseInt(ItemQuantity.getText().toString());
                int Price = Integer.parseInt(ItemPrice.getText().toString());
                RowTotal.setText(String.valueOf(Quantity * Price));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
    row_list.add(RowTotal);
    qty_list.add(ItemQuantity);
    rowLayout.addView(serialNumberText);
    rowLayout.addView(ItemSpinner);
    rowLayout.addView(btnMinus);
    rowLayout.addView(ItemQuantity);
    rowLayout.addView(btnPlus);
    rowLayout.addView(ItemPrice);
    rowLayout.addView(RowTotal);

    return rowLayout;
}

private void addNewRow() {
    GridLayout gridLayout = new GridLayout(this);

    gridLayout.addView(CreateSpinner());

    main.addView(gridLayout);
}
4
I have asked how to get all selected items from all spinnerTamizh arasu

4 Answers

1
votes

You can add spinner in list

    List<Spinner> list=new ArrayList<>();

//and getting all spinner selected value

       for(int i=0;i<list.size();i++)
        {
            list.get(i).getSelectedItemPosition()
        }
1
votes

you can get the spinner values from dynamic layout by using yourdynamicelayout.getChildCount() and yourdynamicelayout.getChildAt() . I think your spinner position is zero. use for loop from zero to getChildCount. getChild at zero cast it to spinner view & get the selected values

// Updated answer

You can achieve the selected value of the spinner by two ways

1) maintain a arrayList of spinner. & get the selectedvalue from it

2) Psuedo code.

// if i m not worng, you are having mainlayout in which you are adding Gridlayout & in grid there's a linearlayout & inside that there's the spinner

for(int i=0;i<main.getChildCount();i++){
GridLayout tempGrid=(Gridlayout)main.getChildAt(i);
LinearLayout templinear=(LinearLayout) tempGrid.getChildAt(0);
Spinner tempSpnr=(Spinner) tempLinear.getChildAt(0);
String value=tempSpnr.getselecteditem().tostring();

}

//its a pseudo code, make changes according to your requirement & code

Hope its works

0
votes

Add new spinner button - how are you adding it? If you are adding a new item to a list and showing the list in the adapter then you can just use a for loop to run through the list and get selected item in each of the spinner.

0
votes

You have

getSelectedItem()

getSelectedItemPosition()

getSelectedItemId()

EX: String text = spinner.getSelectedItem().toString();

On button click event :

buttonClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            //TO GET POSITION
                    int spinnerValue1 = customerSpinner.getSelectedItemPosition();
                    int spinnerValue2 = employeeSpinner.getSelectedItemPosition();
                    int spinnerValue3 = productSpinner.getSelectedItemPosition();
            //TO GET VALUE
                    String spinnerValue3 = (String) productSpinner.getSelectedItem();
            }
        });