我陷入了一種情況,我需要獲取選中的Radiobutton的ID。我知道問題出在哪裏,但我無法解決問題,請在我的代碼基礎上給我建議。自定義列表視圖中的Radiobutton - 一次只能選擇一個
@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正常使用,我們如何手動調用該方法。先進的謝謝你。
我已經使用它,但不是與listview,所以我使用單個單選按鈕和使用RadioGroup可能不是很好的地方只需要單個單選按鈕。是的,它可能是簡單的方法,但更多的項目將在我的列表視圖中,更多的radiogroups將被創建。使用radioGroup將是我完成此操作的最後一招。謝謝。 – IamExpo 2013-03-07 10:15:27
好的,先生,下次我會介意它。我感謝你的建議。謝謝。 – IamExpo 2013-03-07 10:18:31
不客氣,希望您找到高效的解決方案! :) – 2013-03-07 10:19:27