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

Camo picture Camo · Mar 20, 2014 · Viewed 8.4k times · Source

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

Answer

Camo picture Camo · Mar 24, 2014

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