开发者

Android - Radio Button in listView?

开发者 https://www.devze.com 2023-04-12 06:55 出处:网络
I have a scenario that we should have a single choice mode radio button in listview. when i am click on the radiobutton it should go to enable state. when i am click on the whole item then it should r

I have a scenario that we should have a single choice mode radio button in listview. when i am click on the radiobutton it should go to enable state. when i am click on the whole item then it should redirect to开发者_如何学Python the new activity. i refered below link

link1 , link2

i can find out the way for multiple choice mode. but not for the single choice mode. Any Idea on this ?

???Edit??? How can i find the tag of the corresponding tag of an item in the listview


One way you can but I don't know it's perfect or not.

track the position id of the listview on which the radio button was checked now when you click on the another radio button then implement the setOnCheckedChangeListener(listener) and check the position which was already check and uncheck that radiobutton.

You can store the status of radio button into the custom model(Model which contains the controls like textview, imageview, radiobutton etc for listview single row) which was added into the listview

check this article for using model and handle the component into the listview in this given an example of checkbox

Update

I think you can get tag like this way

((View)((ViewGroup)listview.getItemAtPosition(0)).getTag()).getTag();
or
((Button)l.getItemAtPosition(0)).getTag();

update 2

suppose this is your adapter and arraylist object

private List<Model> list_model = new ArrayList<Model>();

private ArrayAdapter<Model> modelAdapter;

you Model class look like this

private class Model{
    private String text1 = "";
    private boolean isChecked   = false;

    public Model(String text1){
        this.text1 = text1;
        isChecked = false;
    }
}

your viewholder

private static class ViewHolder{
    TextView textView;
        RadioButton radioBtn;
}

pass the listmodel to this CustomAdapter class in constructor

private List<Model> list;
private Context context;

public CustomAdapter(Context context, List<Model> list){
    super(context,R.layout.list_layout,list);
    this.list = list;
    this.context = context;
}

now in getView()

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;

    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.gcalendar_list_layout, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.textView = (TextView) view.findViewById(R.id.text1);
        viewHolder.radioBtn = (RadioButton) view.findViewById(R.id.radioBtn);
        viewHolder.radioBtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Model element = (Model) viewHolder.checkBox.getTag();
                element.isChecked = buttonView.isChecked();
                boolean isChecked = true;
                for(int i=0;i<list.size();i++){
                    if(!list.get(i).isChecked){
                        list.get(i).isChecked=false; // more implement here or may be this work
                        break;
                    }
                }
            }
        });
        view.setTag(viewHolder);
        viewHolder.radioBtn.setTag(list.get(position));
    }else{
        view = convertView;
        ((ViewHolder)view.getTag()).radioBtn.setTag(list.get(position));
    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.textView.setText(list.get(position).name);
    holder.radioBtn.setChecked(list.get(position).isChecked);
    return view;
}


When there is only one button widget of choice is CheckBox. You will have to do some tweaking in your list adapter though - you will need to register custom event listener while configuring list entry views, and to keep in mind that those views are reused (or create new views if you do not care about performance)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号