2011-11-28 88 views
2

我只是想用两个TextViews(并排连续)制作单选列表视图。单选ListView自定义行布局

其实我想让它让用户根据它的大小价格值选择一个产品。 这些值在那个ListView中显示,这两个TextView代表Size-Price值。

问题是,我不能使它成为一个单选列表(也显示在它前面的单选按钮)。以下是我正在使用的行布局:

<?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> 

请告诉如何使用Java编写代码。 如果simple_list_item_2布局可行,那么该怎么做?

+0

我已经回答了类似的问题:http://stackoverflow.com/questions/8295560/listview-with-checkbox-why-the-checkbox-doesnt-show – QuickNick

回答

0

尝试 声明一个RadioGroup对象适配器然后 在getView()bindView()添加RadioButtonsRadioGroup使用RadioGroup.addView()

1

我不知道针锋相对,我们可以创建ListView的单选按钮组,我”我们只是简单地处理Java中的选择。 实施例:

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

持有人:[它可以是一个内部类]

class ViewHolder { 
     int position; 

     RadioButton selectionRB; 
     TextView sizeTV; 
     TextView priceTV; 
    } 

和mSelectedRB是一个全球性的构件,以您的活动。