2013-12-20 5 views
2

Я хотел бы добавить переключатель в свой существующий список, чтобы одновременно выбирать один переключатель.Добавление переключателя в список

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

Может кто-нибудь сказать мне, как добиться этого?

ответ

1

Для получения дополнительной информации о комплектации ListView вы должны сделать это самостоятельно. Просто добавьте boolean isSelected в свои ItemDetails, а затем checkBox.setChecked(item.isSelected); в свой getView() в ItemListBaseAdapter. Теперь, когда элемент щелкнул в ListView, просто измените данные в itemDetailsrrayList и позвоните по номеру notifyDataSetChanged().

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

Я думаю, вы должны попробовать оба этого

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

И в XML-файл, который нужно добавить,

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

добавить Также это в XML вашего радио-кнопки, в

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

Сделайте HashMap m и initia атрических это

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

Смежные вопросы