向listview添加单选按钮

时间:2013-12-20 10:14:47

标签: android listview radio-button selection

我想在我现有的列表视图中添加一个单选按钮,这样一次只需要选择一个单选按钮。

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;

}

适配器:

    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>

我见过很多SO anwsers,但是没有一个人在我的案例中说要添加

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

有谁能说我如何实现这个目标?

3 个答案:

答案 0 :(得分:1)

对于复杂的ListView项,您应该自己完成。只需在boolean isSelected中添加ItemDetails,然后在checkBox.setChecked(item.isSelected);的{​​{1}}中getView()添加ItemListBaseAdapter。现在,当ListView中的某个项目被点击时,只需更改itemDetailsrrayList中的数据并拨打notifyDataSetChanged()

答案 1 :(得分:1)

我已经制作了我的getView()方法。只需制作单选按钮而不是简单的按钮。我希望这会有所帮助。

        @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;
   }

答案 2 :(得分:0)

我认为你应该尝试这两个,

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

在xml文件中,您需要添加

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

还可以在你的单选按钮的xml中添加它,

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

创建HashMap并初始化

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

现在,setViewBinder到适配器

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

});

它会像魅力一样起作用。

选中此Example link