在列表视图中获取所选单选按钮位置

时间:2013-12-20 17:54:04

标签: android listview position radio-button

我正在尝试使用settag选择单选按钮来存储其在我的BaseAdapter中的位置,并从其他类访问它。

我可以选择单项选择,但我需要在选择单选按钮时获取图像的名称。

这是我的BaseAdapter GetView:

    public View getView(final 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);
        holder.button = (RadioButton)convertView.findViewById(R.id.rbSelectedOption);                                                     

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

    holder.button.setTag(position);  

    holder.button.setOnClickListener(new View.OnClickListener() {  
                    public void onClick(View v) { 
            if((position != mSelectedPosition && mSelectedRB != null)){
                mSelectedRB.setChecked(false);
            }
            mSelectedPosition = position;
            mSelectedRB = (RadioButton)v;
        }
    });

        if(mSelectedPosition != position){
            holder.button.setChecked(false);
        }else{
            holder.button.setChecked(true);
            if(mSelectedRB != null && holder.button!= mSelectedRB){
                mSelectedRB = holder.button;
            }
        }         
    return convertView;
}

这是我的ListViewActivity:

ArrayList<ItemDetails> image_details = GetSearchResults();

    final ListView lv1 = (ListView) findViewById(R.id.list);
    lv1.setAdapter(new ItemListBaseAdapter(this, image_details));
    lv1.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    final RadioButton SelectedOption = (RadioButton) findViewById(R.id.rbSelectedOption);
    lv1.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
            int pos = (Integer) v.getTag();
            Object o = SelectedOption.getTag(pos);
            ItemDetails obj_itemDetails = (ItemDetails)o;
            Toast.makeText(ListViewImagesActivity.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
            CarName =obj_itemDetails.getName();
       }  
    });

我无法通过使用getTag来获取位置:

int pos = (Integer) v.getTag();
Object o = SelectedOption.getTag(pos);

有谁能说我如何从BaseAdapter类到ListViewActivity获得这个位置?

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

在适配器中声明两个公共字段:

public int SelectedRBPosition;
public RadioButton SelectedRB;  

选中时使用它们来保存位置和RadioButton 创建两种方法来获取它们;

public int getSelectedRadioButtonPosition(){
    return SelectedRBPosition;
}

public RadioButton getSelectedRadioButton(){
    return SelectedRB;
}

在活动中为adpter声明一个字段:

private ItemListBaseAdapter adapter = new ItemListBaseAdapter(this, image_details);
final ListView lv1 = (ListView) findViewById(R.id.list);
lv1.setAdapter(adapter);
lv1.setChoiceMode(ListView.CHOICE_MODE_SINGLE);  

当你需要它们时,请调用get方法:

int pos = adapter.getSelectedRBPosition();
RadioButton rb = adapter.getSelectedRB();