coder
coder

Reputation: 13250

Adding radio button to listview

I would like to add a radio button to my existing listview so that only one radio button needs to be selected at a time.

ItemDetails:

public class ItemDetails {

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getItemDescription() {
        return itemDescription;
    }
    public void setItemDescription(String itemDescription) {
        this.itemDescription = itemDescription;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    public int getImageNumber() {
        return imageNumber;
    }
    public void setImageNumber(int imageNumber) {
        this.imageNumber = imageNumber;
    }


    private String name ;
    private String itemDescription;
    private String price;
    private int imageNumber;

}

Adapter:

    public class ItemListBaseAdapter extends BaseAdapter {
    private static ArrayList<ItemDetails> itemDetailsrrayList;

    private Integer[] imgid = {
            R.drawable.img1,
            R.drawable.img2,
            R.drawable.img3,
            R.drawable.img4,
            R.drawable.img5,
            R.drawable.img6
            };

    private LayoutInflater l_Inflater;
    public ItemListBaseAdapter(Context context, ArrayList<ItemDetails> results) {
        itemDetailsrrayList = results;
        l_Inflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return itemDetailsrrayList.size();
    }

    public Object getItem(int position) {
        return itemDetailsrrayList.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {

            convertView = l_Inflater.inflate(R.layout.item_details_view, null);
            holder = new ViewHolder();
            holder.txt_itemName = (TextView) convertView.findViewById(R.id.name);
            holder.txt_itemDescription = (TextView) convertView.findViewById(R.id.itemDescription);
            holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.price);
            holder.itemImage = (ImageView) convertView.findViewById(R.id.photo);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }




        holder.txt_itemName.setText(itemDetailsrrayList.get(position).getName());
        holder.txt_itemDescription.setText(itemDetailsrrayList.get(position).getItemDescription());
        holder.txt_itemPrice.setText(itemDetailsrrayList.get(position).getPrice());
        holder.itemImage.setImageResource(imgid[itemDetailsrrayList.get(position).getImageNumber() - 1]);
//      imageLoader.DisplayImage("http://192.168.1.28:8082/ANDROID/images/BEVE.jpeg", holder.itemImage);

        View row = convertView;

        CheckedTextView checkBox = (CheckedTextView) row.findViewById(R.id.checkstate);
        checkBox.setChecked(false);
        return convertView;
    }



    static class ViewHolder {
        TextView txt_itemName;
        TextView txt_itemDescription;
        TextView txt_itemPrice;
        ImageView itemImage;
        CheckedTextView checkBox;
    }
}

ItemDetails.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">
    <ImageView 
        android:id="@+id/photo"
        android:layout_width="150dip"
        android:layout_height="100dip"
        android:paddingRight="15dp"
        android:paddingLeft="15dp"/>
    </LinearLayout>
    <RelativeLayout
        android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_marginLeft="160dp"
            android:layout_marginTop="10dp">
    <TextView android:id="@+id/name"
        android:textSize="14sp" 
        android:textStyle="bold" 
        android:textColor="#000000" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/>
    <TextView android:id="@+id/itemDescription"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_below="@id/name"/>
    <TextView android:id="@+id/price" 
        android:textSize="19sp" 
        android:textStyle="bold" 
        android:textColor="#003399" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_below="@id/itemDescription"/>
    </RelativeLayout>
</RelativeLayout>

ListView:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background ="#CCCCCC">

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:divider="#3366CC"
        android:dividerHeight="2dp" 
        android:choiceMode="singleChoice"/>       
</LinearLayout>

I have seen many SO anwsers but none of them got working in my case saying to add

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

Can anyone say me how to acheive this ?

Upvotes: 3

Views: 6171

Answers (3)

Jaydipsinh Zala
Jaydipsinh Zala

Reputation: 16798

I think you should try both of this,

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

And in xml file you need to add,

<ListView
    ...
    android:choiceMode="singleChoice"
    ... />

Also add this in your radio-button's xml,

<RadioButton
    ...
    android:clickable="false"
    android:focusable="false"
    ... />

Make HashMap m and initialize it

for (HashMap<String, Object> m :list_data) //make data of this view should not be null (hide )
        m.put("checked", false);

Now, setViewBinder to adapter

adapter.setViewBinder(new SimpleAdapter.ViewBinder()
{
      public boolean setViewValue(View view, Object data, String textRepresentation)
      {
            if (data == null) //if 2nd line text is null, its textview should be hidden
            {
                view.setVisibility(View.GONE);
                return true;
            }
            view.setVisibility(View.VISIBLE);
            return false;
      }

});

It will work like charm.

Check this Example link

Upvotes: 0

user1520309
user1520309

Reputation:

I have made my getView() method.Just make radio button instead of simple button.I Hope this helps.

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

        View productListView = null;

        productListView = convertView;

        LayoutInflater inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (productListView == null)
        productListView = inflater.inflate(R.layout.menu_productlist_list,
                    parent, false);

        productListTitle = (TextView) productListView
                .findViewById(R.id.productListTitle);

        String title = menuProductListCollection.get(position).getPD_TITLE();

        productListTitle.setText(title);

        productListCategoryButton = (ImageButton) productListView
                .findViewById(R.id.productListCategoryButton);

        // Perform Button action on click of each row's button 
        productListCategoryButton.setOnClickListener(new OnClickListener() {

              @Override
              public void onClick(View v) {
                // TODO Auto-generated method stub
            //do your stuff here
            }
            });     
                // Perform action on click of each row 
                   productListView.setOnClickListener(new OnClickListener() {

              @Override
              public void onClick(View v) {
                // TODO Auto-generated method stub
            //do your stuff here
            }
            });     

     return productListView;
   }

Upvotes: 1

M-Wajeeh
M-Wajeeh

Reputation: 17284

For complex ListView item, you should do it by yourself. Just add boolean isSelected in your ItemDetails and then checkBox.setChecked(item.isSelected); in your getView() in ItemListBaseAdapter. Now when an item is clicked in ListView just change your the data in itemDetailsrrayList and call notifyDataSetChanged().

Upvotes: 1

Related Questions