【问题标题】:How to select/unselect RadioButton of ListView array adapter in android如何在android中的ListView arrayadapter上选择/取消选择单选按钮
【发布时间】:2017-01-16 14:48:34
【问题描述】:

我在列表视图适配器中使用单选按钮,我想要的是选择/取消选择我单击的单选按钮。

【问题讨论】:

  • 你尝试了什么?并分享一些代码
  • 好的,我尝试在适配器类中使用单选按钮,但是一旦选中,我就无法选中/取消选中单选按钮。
  • 在下面提供了答案。
  • 嘿,我有ListView,根据用户需要,用户可以根据需要添加尽可能多的数据,并根据他的点击选择或取消选择列表项
  • 你试过这个解决方案了吗? stackoverflow.com/a/41670455/7267105

标签: android radio-button


【解决方案1】:

将您的单选按钮状态保存在列表中,或者如果在这种情况下只有一个选择,您可以将特定位置保存在 int 变量中,然后在选择时更新变量的值,然后执行adapter.notifyDataSetChanged()。您需要在getView 中编写代码以反映更改。

粗略的例子:(我现在对listview(检查recyclerview)很老了,但是你的adpater应该是这样的)

Adapter{
 int selectedPosition = -1;  // initially nothing selected  

 getView(..,..., int position){

   if(selectedPosition==position){
   holder.radioButton.setChecked(true);
   }else{
   holder.radioButton.setChecked(false);
   }

   holder.radioButton.setTag(position);

   holder.radioButton.onClick(){
   selectedPosition = (Integer)holder.radioButton.getTag();
   notifyDataSetChanged();
   }  

 )


static class Holder {
    RadioButton radioButton;
}

【讨论】:

  • 是的,我试过这个,但它一次只选择一个单选按钮,但我想要的是,当用户单击列表项的任何单选按钮时,它可以被选中,当单击同一个单选按钮时再次按钮,它应该被取消选中。
  • 对于单选来说,如果值>0,则更改 selectedPosition =-1 的值。但是在单个项目中,它应该在单击时自动切换,因为按钮已经是该属性。
  • 我使用了复选框,然后它工作正常,单选按钮不可能
  • 你应该使用 converterView.setTag("pos",position); ,而不是 holder.radioButton 它将不可用于检索
【解决方案2】:

给radionButton添加一个clickListener,这样如果你点击radionButton,如果它被选中,它将取消选择。 (默认情况下,如果不勾选,点击会勾选)

radioButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (radioButton.isChecked()) {
                radioButton.setChecked(false);
                }
            }
        });

【讨论】:

    【解决方案3】:

    在这一步首先我们得到ButtonListView的引用。之后我们为问题创建一个字符串数组,然后设置一个adapter 来填充ListView 中的数据。最后,我们在Button 上执行setOnClickListener 事件,因此每当用户点击Button 时,选择的问题答案都会使用Toast 显示。

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
    ListView simpleList;
    String[] questions;
    Button submit;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // get the string array from string.xml file
    questions = getResources().getStringArray(R.array.questions);
    // get the reference of ListView and Button
    simpleList = (ListView) findViewById(R.id.simpleListView);
    submit = (Button) findViewById(R.id.submit);
    // set the adapter to fill the data in the ListView
    CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), questions);
    simpleList.setAdapter(customAdapter);
    // perform setOnClickListerner event on Button
    submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    String message = "";
    // get the value of selected answers from custom adapter
    for (int i = 0; i < CustomAdapter.selectedAnswers.size(); i++) {
    message = message + "\n" + (i + 1) + " " + CustomAdapter.selectedAnswers.get(i);
    }
    // display the message on screen with the help of Toast.
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
    }
    });
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
    return true;
    }
    
    return super.onOptionsItemSelected(item);
    }
    }
    
    
    In this step we create a custom adapter class and extends BaseAdapter in this class. In this step we firstly set the data in the list and then perform setOnCheckedChangeListener event on RadioButton.
    
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.CompoundButton;
    import android.widget.RadioButton;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    
    
    public class CustomAdapter extends BaseAdapter {
    Context context;
    String[] questionsList;
    LayoutInflater inflter;
    public static ArrayList<String> selectedAnswers;
    
    public CustomAdapter(Context applicationContext, String[] questionsList) {
    this.context = context;
    this.questionsList = questionsList;
    // initialize arraylist and add static string for all the questions
    selectedAnswers = new ArrayList<>();
    for (int i = 0; i < questionsList.length; i++) {
    selectedAnswers.add("Not Attempted");
    }
    inflter = (LayoutInflater.from(applicationContext));
    }
    
    @Override
    public int getCount() {
    return questionsList.length;
    }
    
    @Override
    public Object getItem(int i) {
    return null;
    }
    
    @Override
    public long getItemId(int i) {
    return 0;
    }
    
    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
    view = inflter.inflate(R.layout.list_items, null);
    // get the reference of TextView and Button's
    TextView question = (TextView) view.findViewById(R.id.question);
    RadioButton yes = (RadioButton) view.findViewById(R.id.yes);
    RadioButton no = (RadioButton) view.findViewById(R.id.no);
    // perform setOnCheckedChangeListener event on yes button
    yes.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    // set Yes values in ArrayList if RadioButton is checked
    if (isChecked)
    selectedAnswers.set(i, "Yes");
    }
    });
    // perform setOnCheckedChangeListener event on no button
    no.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    // set No values in ArrayList if RadioButton is checked
    if (isChecked)
    selectedAnswers.set(i, "No");
    
    }
    });
    // set the value in TextView
    question.setText(questionsList[i]);
    return view;
    }
    }
    

    【讨论】:

    • 嘿,我有ListView,根据用户需要,用户可以根据需要添加尽可能多的数据,并根据他的点击选择或取消选择列表项
    • 这取决于用户,它在列表视图中添加了多少项目,所以单选按钮不固定
    • 所以你需要使用 RadioGroup。例如: RadioGroup radiogroup=(RadioGroup)findViewById(R.id.rdbGp1); radiogroup.setOnCheckedChangeListener(newOnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // 单选按钮 RadioButton rb1=(RadioButton)findViewById(R.id.rdb1); if (rb1.isChecked()){ Toast .makeText(MainActivity.this, "选择男性选项", Toast.LENGTH_LONG).show(); } else{ Toast.makeText(MainActivity.this, "选择女性选项", Toast.LENGTH_LONG).show(); } } });
    • 嘿,不是你的解决方案,它通过使用复选框来工作
    • @Shivangi 首先粘贴您有问题的完整代码,以便我们可以看到确切的问题在哪里
    【解决方案4】:
    radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // Radio button
                    RadioButton rb1=(RadioButton)findViewById(R.id.rdb1);
                    if (rb1.isChecked()){
                        Toast.makeText(MainActivity.this, "1 option selected", Toast.LENGTH_LONG).show();
                    }
                    else{
                        Toast.makeText(MainActivity.this, "2 option selected", Toast.LENGTH_LONG).show();
    
                    }
                }
            }); 
    

    【讨论】:

      猜你喜欢
      • 2011-07-17
      • 2012-02-21
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2013-10-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      相关资源
      最近更新 更多