I wish to set spinner items using program code and not using android:entries in xml layout. But i am failing to do so. I want to set spinner items according to a condition so cannot set it using xml statically so kindly help me to set dynamically. here's the code: public class Converter extends AppCompatActivity {
EditText et2;
TextView tv;
Spinner spr2,spr3;
ArrayAdapter adap2,adap3;
String []spr_2;
String []spr_3;
Button btn2;
String s1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_converter);
et2=(EditText) findViewById(R.id.et2);
tv=(TextView) findViewById(R.id.tv);
spr2=(Spinner) findViewById(R.id.spr2);
spr3=(Spinner) findViewById(R.id.spr3);
btn2=(Button) findViewById(R.id.btn2);
Bundle b=getIntent().getExtras();
et2.setText(b.getString("Value1"));
s1=b.getString("SpinnerValue");
adap2=adap3=new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line);
if(s1 == "height") {
spr_2 = spr_3 = getResources().getStringArray(R.array.height);
adap2 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_2);
adap3 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_3);
spr2.setAdapter(adap2);
spr3.setAdapter(adap3);
}
if(s1 == "weight") {
spr_2 = spr_3 = getResources().getStringArray(R.array.weight);
adap2 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_2);
adap3 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_3);
spr2.setAdapter(adap2);
spr3.setAdapter(adap3);
}
if(s1 == "distance") {
spr_2 = spr_3 = getResources().getStringArray(R.array.distance);
adap2 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_2);
adap3 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_3);
spr2.setAdapter(adap2);
spr3.setAdapter(adap3);
}
if(s1 == "currency") {
spr_2 = spr_3 = getResources().getStringArray(R.array.height);
adap2 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_2);
adap3 = new ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, spr_3);
spr2.setAdapter(adap2);
spr3.setAdapter(adap3);
}
}
}