Question

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

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top