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.
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);
}