我使用listView使用自定义Radiobutton,我想先检查 radiobutton默认但不能这样做。当我正在实施 对于最后一个radiobutton,可以检查默认值。
我想要什么: 当我点击第二个radiobutton时,应取消选择第一个radiobutton(默认选中的按钮)。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
@SuppressLint(“ViewHolder”) View returnedView =inflater.inflate(listItemViewId, null);
final TextView itemName = (TextView) returnedView.findViewById(R.id.ic_colorname);
itemName.setText(colorArrayList.get(position));
final RadioButton radioButton= (RadioButton) returnedView.findViewById(R.id.ic_radioButton);
//by default last radio button selected
if (position == getCount()-1) {
if (selected == null) {
radioButton.setChecked(true);
selected =radioButton;
}
}
radioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(“Id”, String.valueOf(radioButton.getId()));
colorResult=colorArrayList.get(position);
Log.d(“Button”,colorResult);
if (selected != null ) {
selected.setChecked(false);
}
radioButton.setChecked(true);
selected = radioButton;
//for passing value to activity
Intent intent=new Intent(“passingColor”);
intent.putExtra(“color”,colorResult);
LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);
}
});
return returnedView;
}