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

半世苍凉 提交于 2019-12-08 19:05:29
Camo

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" />

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.
    }
});

you can set the list setOnItemClickListener.

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()
        }
    });

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.

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

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