I have Two Radio Buttons along with Two Spinner, both are depend on another one. E.g for if I clicked Radio Button1
means Spinner visibility hidden. same for opposite.
View and Hide is working, The Problem is, If i choose first Radio Button and choose some values in Spinner1. but now wrongly choose so i Move onto Radio Button 2 and choose some value from Spinner2
. (Now first Spinner1
values not change (i.e) old value selected. I think whenever change Radio Button Opposite Spinner value set to "Select By something",
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide(); //<< this for hide title bar
setContentView(R.layout.scrap_report);
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
setCurrentDateOnView();
addListenerOnButton();
staff_name = (Spinner) findViewById(R.id.spinner1);//staff spinner
product_name = (Spinner) findViewById(R.id.spinner2);//product spinner
created_date1 = (TextView) findViewById(R.id.tvDate);
created_date2 = (TextView) findViewById(R.id.tvDate2);
//spinner for customer name
staff_name = (Spinner) findViewById(R.id.spinner1);
adapter = new ArrayAdapter<String>(this, R.layout.spinner_scrap_staff, R.id.txt, listItems);
staff_name.setAdapter(adapter);
staff_name.setAdapter(new NothingSelectedSpinnerAdapter(adapter, R.layout.scrap_staff_nthgselect, this));
//spinner for Products
product_name=(Spinner)findViewById(R.id.spinner2);
adapter2=new ArrayAdapter<String>(this,R.layout.spinner_scrap_product,R.id.txt,listItems2);
product_name.setAdapter(adapter2);
product_name.setAdapter(new NothingSelectedSpinnerAdapter(adapter2,R.layout.scrap_product_nthg,this));
final RadioGroup rgroup = (RadioGroup)findViewById(R.id.radioType);
final RadioButton Ta = (RadioButton)findViewById(R.id.type_a);
final RadioButton Tb = (RadioButton)findViewById(R.id.type_b);
rgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (rgroup.getCheckedRadioButtonId() == Ta.getId()) {
staff_names = staff_name.getSelectedItem().toString();
product_name.setSelection(0);
product_name.setVisibility(View.GONE);
staff_name.setVisibility(View.VISIBLE);
}
else if (rgroup.getCheckedRadioButtonId() == Tb.getId()) {
product_names = product_name.getSelectedItem().toString();
staff_name.setSelection(0);
staff_name.setVisibility(View.GONE);
product_name.setVisibility(View.VISIBLE);
}
}
});
/* rgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.type_a){
staff_name.setVisibility(View.VISIBLE);
product_name.setVisibility(View.INVISIBLE);
staff_name = (Spinner) findViewById(R.id.spinner1);
}
else{
staff_name.setVisibility(View.INVISIBLE);
product_name.setVisibility(View.VISIBLE);
product_name=(Spinner)findViewById(R.id.spinner2);
}
}
});
rgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (rgroup.getCheckedRadioButtonId() == Ta.getId()) {
staff_names = staff_name.getSelectedItem().toString();
product_name.setAdapter(adapter2);
}
else if (rgroup.getCheckedRadioButtonId() == Tb.getId()) {
product_names = product_name.getSelectedItem().toString();
staff_name.setAdapter(adapter);
}
}
});*/
Button submit = (Button) findViewById(R.id.loadbtn);
assert submit != null;
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
staff_names = staff_name.getSelectedItem().toString();
product_names = product_name.getSelectedItem().toString();
createddate = created_date1.getText().toString();
created_date = created_date2.getText().toString();
String url_server = "http://192.168.1.13:8090/Vaari_services/getScrapRepo.php";
new get_ScrapDate().execute(url_server);
staff_name.setSelection(0);
product_name.setSelection(0);
}
});
}
i tried
staff_name.setSelection(0,true);
LogCat:
06-28 12:43:58.356 27966-27966/com.example.vari.new_varri E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vari.new_varri, PID: 27966
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.vari.new_varri.Scrap_Report$1.onCheckedChanged(Scrap_Report.java:142)
at android.widget.RadioGroup.setCheckedId(RadioGroup.java:173)
at android.widget.RadioGroup.-wrap0(RadioGroup.java)
at android.widget.RadioGroup$CheckedStateTracker.onCheckedChanged(RadioGroup.java:351)
at android.widget.CompoundButton.setChecked(CompoundButton.java:159)
at android.widget.CompoundButton.toggle(CompoundButton.java:115)
at android.widget.RadioButton.toggle(RadioButton.java:76)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
You can select programatically by using
spinnerObject.setSelection(0)
if still its not working then try this
spinnerObject.post(new Runnable() {
@Override
public void run() {
spinnerObject.setSelection(0);
}
});
radibutton checkedchangelistener
rgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (rgroup.getCheckedRadioButtonId() == Ta.getId()) {
staff_names = listItems.get(spinner1Object.getSelectedItemPosition());
spinner2Object.setSelection(0);
spinner2Object.setVisibility(View.GONE);
spinner1Object.setVisibility(View.VISIBLE);
}
else if (rgroup.getCheckedRadioButtonId() == Tb.getId()) {
product_names = listItems2.get(spinner2Object.getSelectedItemPosition());
spinner1Object.setSelection(0);
spinner1Object.setVisibility(View.GONE);
spinner2Object.setVisibility(View.VISIBLE);
}
}
});