gpt4 book ai didi

android - 如何根据选中的单选按钮将 Spinner 值重置为 "Select by something"

转载 作者:太空狗 更新时间:2023-10-29 13:57:47 27 4
gpt4 key购买 nike

我有两个单选按钮和两个微调器,两者都依赖于另一个。例如,如果我单击 Radio Button1,则表示 Spinner 可见性已隐藏。反之同。

查看和隐藏功能正常,问题是,如果我选择第一个单选按钮并在 Spinner1 中选择一些值。但现在选择错误,所以我移至单选按钮 2 并从 Spinner2 中选择一些值。 (现在第一个 Spinner1 值不会改变(即)选择的旧值。我认为每当更改 Radio Button Opposite Spinner 值设置为“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);

}
});
}

我试过了

 staff_name.setSelection(0,true);

日志:

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)

最佳答案

您可以使用

以编程方式选择
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);
}
}
});

关于android - 如何根据选中的单选按钮将 Spinner 值重置为 "Select by something",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38068854/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com