How to set radio button to checked when i click on list item

懵懂的女人 提交于 2019-12-08 06:24:24

问题


I am having a problem with my listview ( 2 textview and 1 radio button).

The problem: My idea is that the user clicks on the item in the listview and the radio button gets checked automatically.

I have been searching for a while, but I can't get the radio button to work.

My XML

   <RadioButton
        android:id="@+id/rdBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false" />

My Adapter

    r = (RadioButton) convertView.findViewById(R.id.rdBtn);
        r.setChecked(selectedPosition == position);
        r.setTag(position);
        r.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //r.setChecked(true);
                Toast.makeText(context, InformationActivity.result,
                        Toast.LENGTH_SHORT).show();
                selectedPosition = (Integer) view.getTag();
                notifyDataSetInvalidated();

            }
        });
        return convertView;

I tried

r.setChecked(true);

inside my activity class and the first click worked, but the second chooses a different item on the listview.

I hope some of you can help me. Thanks


回答1:


I found the solution here: How to check checkbox on clicking an image?

        searchList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view,
                int position, long arg3) {

            LinearLayout item_view = (LinearLayout) view;
            final RadioButton itemcheck = (RadioButton) item_view
                    .findViewById(R.id.rdBtn);

            if (itemcheck.isChecked()) {
                itemcheck.setChecked(true);
            } else {
                itemcheck.setChecked(false);
            }

            itemcheck.setChecked(true);


<RadioButton
        android:id="@+id/rdBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false" />



回答2:


I'm not sure I understand what you are trying to achieve, but maybe this would help?

r.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            // radio button is checked
        } else {
            // radio button is not checked
        }
    }
});

Edit.

Assuming list is your ListView, you could do the following:

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView <? > parent, View view, int position, long id) {
        r.setChecked(true); // true to make r checked, false otherwise.
    }
});



回答3:


you can set the list setOnItemClickListener.




回答4:


You should set OnClick Listener for 'convertview' and toggle radioButton as follows-

 convertview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           radioButton.toggle()
        }
    });



回答5:


I solved this issue by using below functionality,

1.Use radiogroup.clearCheck(); To clear the all the checked state of radio buttons in listview scrolling.

2.Maintain the checked state of each radio button in a list. If radio button checked state is changed update the value in list.




回答6:


You may use checktextview inside the listview and set the type of checktextview to look like radiobutton or use background image for checktextview check so that it look like radio button.Hope this helps you



来源:https://stackoverflow.com/questions/22534312/how-to-set-radio-button-to-checked-when-i-click-on-list-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!