2

I just want to make a Single Choice ListView with two TextViews (side by side in a row).

Actually I want it to let a user select a Product according to its Size-price values. These values are shown in that ListView with those two TextViews representing Size-Price values.

The problem is that I am not able to make it a single choice list (also showing a radio Button in front of it). Below is the row layout I am using for it:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:id="@+id/rowLayout" >
    <TextView
        android:id="@+id/tv_size"
        android:layout_width="180dp"
        android:layout_height="wrap_content"
        android:text="Size"
        android:textSize="15sp" />
    <TextView
        android:id="@+id/tv_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Price"
        android:textSize="15sp" />
   <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right" />
</LinearLayout>

Please tell how to code with Java for that. If it is possible with simple_list_item_2 layout then how ?

Nitin4Android
  • 864
  • 2
  • 12
  • 25
  • I've answered similar question: http://stackoverflow.com/questions/8295560/listview-with-checkbox-why-the-checkbox-doesnt-show – QuickNick Nov 28 '11 at 12:38

2 Answers2

1

I'm not sure tat we can create radio group in ListView, what i'm doing s simply handle the selections in Java. Example:

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        final ViewHolder holder;

        if (view == null) {
            holder = new ViewHolder();

            view = mInflater.inflate(R.layout.customer_invoice_row, null);

            holder.selectionRB = (RadioButton) view
                    .findViewById(R.id.selectionRB);
            holder.sizeTV = (TextView) view
                    .findViewById(R.id.sizeTV);
            holder.priceTV = (TextView) view
                    .findViewById(R.id.priceTV);

            holder.selectionRB.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    RadioButton rb = (RadioButton) v;
                    if (mSelectedRB != null) {
                        if (!rb.getTag().equals(mSelectedRB.getTag()))
                            mSelectedRB.setChecked(false);

                    }
                    mSelectedRB = rb;

                }
            });

            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }


        holder.position = position;

        holder.selectionRB.setTag(productObj);
        holder.sizeTV.setText(productObj.getSize());
        holder.priceTV.setText(productObj.getPrice());
        return view;
    }

Holder: [it can be a inner class]

class ViewHolder {
        int position;

        RadioButton selectionRB;
        TextView sizeTV;
        TextView priceTV;
    }

and mSelectedRB is a global member to your activity.

Jayabal
  • 3,619
  • 3
  • 24
  • 32
0

try to Declare a RadioGroup object in your adapter then in your getView() or bindView() add the RadioButtons to the RadioGroup using RadioGroup.addView()

confucius
  • 13,127
  • 10
  • 47
  • 66