我有两个单选按钮和两个Spinner,两者都依赖于另一个。例如,如果我单击单选按钮1意味着隐藏了Spinner可见性。同样相反。
查看和隐藏正在工作,问题是,如果我选择第一个单选按钮并在Spinner1中选择一些值。但现在错误地选择所以我移动到单选按钮2并从Spinner2中选择一些值。 (现在第一个Spinner1值不会改变(即)选择的旧值。我想每当改变单选按钮对面的Spinner值设置为"选择By somthing",
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);
}
});
}
我试过
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)
答案 0 :(得分:1)
您可以使用
以编程方式选择spinnerObject.setSelection(0)
如果仍然无效,请尝试此
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);
}
}
});