0

我陷入了需要获取选中的 Radiobutton 的 ID 的情况。我知道问题出在哪里,但我无法解决,所以请根据我的代码建议我。

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

    bean=arrayListCountry.get(position);

    LayoutInflater inflater=(LayoutInflater)context.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
    if(convertView==null)
    {
        convertView=inflater.inflate(R.layout.custom_layout_listview, null);
        holder=new ViewHolder();
        holder.textCountryName=(TextView)convertView.findViewById(R.id.textView1);
        holder.radioCountry=(RadioButton)convertView.findViewById(R.id.radioButton1);
        RelativeLayout relativeLayout=(RelativeLayout)convertView.findViewById(R.id.relativeCustomLayout);
        relativeLayout.addView(convertView);
        convertView.setTag(holder);
        convertView.setTag(R.id.textView1,holder.textCountryName);


    }
    else
        holder=(ViewHolder)convertView.getTag();

    holder.radioCountry.setTag(position);

    holder.textCountryName.setText(bean.getCountryName());
    holder.radioCountry.setChecked(bean.getIsSelected());

    holder.radioCountry.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub

            int pos=(Integer)buttonView.getTag();

            Country_Bean country_Bean=arrayListCountry.get(pos);


            coubean.setIsSelected(buttonView.isChecked());


            //buttonView.setChecked(bean.getIsSelected());

            Toast.makeText(context, "POs:"+pos+"\ncountry:"+bean.getCountryName()+"\ncountry_check:"+bean.getIsSelected()+"\nbuttonCheck:"+buttonView.isChecked(), Toast.LENGTH_SHORT).show();
        }
    });

    return convertView;
}

请告诉我如何获得特定的单选按钮。我也想知道为什么通常使用 getView 以及我们如何手动调用该方法。提前谢谢你。

4

2 回答 2

1

在此处输入图像描述

我已经完成了,并将完整的源代码发布在我的 Android 博客上以供参考。

http://amitandroid.blogspot.in/2013/03/android-custon-single-choice-lsitview.html

于 2013-03-07T11:19:32.720 回答
0

你试过RadioGroup吗?它是许多RadioButtons的容器。它甚至有一个名为getCheckedRadioButtonId()的方法。


除此之外,我看到您没有遵循打字约定。对此我非常严格,我为我内心的代码语法纳粹道歉。

如果 bean 是成员变量,则使用 mBean。

bean=arrayListCountry.get(position);

将会:

mBean=arrayListCountry.get(position);

Country_Bean 不是类的正确名称。

Country_Bean

将会:

CountryBean

范围变量名 country_Bean 也是错误的。

country_Bean

将会:

countryBean

此外,如果使用 Eclipse,请尝试使用自动格式,因为它会使代码更漂亮(CTRL + SHIFT + F)。

于 2013-03-07T10:07:24.883 回答