android-------listview 实现radiobutton 单选
?
?
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
?
public class RecoverListAdapter extends BaseAdapter {
?
? ? private LayoutInflater inflater;
?
? ? int[] backup_record_item_image;
?
? ? String[] backup_record_item_time;
?
? ? String[] backup_record_item_to;
?
? ? Activity activity;
?
? ? private int temp = -1;
?
? ? public RecoverListAdapter(Activity context, int[] backup_record_item_image,
? ? ? ? ? ? String[] backup_record_item_time, String[] backup_record_item_to) {
? ? ? ? this.inflater = LayoutInflater.from(context);
? ? ? ? this.backup_record_item_image = backup_record_item_image;
? ? ? ? this.backup_record_item_time = backup_record_item_time;
? ? ? ? this.backup_record_item_to = backup_record_item_to;
? ? ? ? this.activity = context;
? ? }
?
? ? @Override
? ? public int getCount() {
? ? ? ? return backup_record_item_time.length;
? ? }
?
? ? @Override
? ? public Object getItem(int position) {
? ? ? ? return position;
? ? }
?
? ? @Override
? ? public long getItemId(int position) {
? ? ? ? return position;
? ? }
?
? ? @Override
? ? public View getView(int position, View convertView, ViewGroup parent) {
? ? ? ? ViewHolder holder;
? ? ? ? if (convertView == null) {
? ? ? ? ? ? holder = new ViewHolder();
? ? ? ? ? ? convertView = this.inflater.inflate(R.layout.general_recover_list_item, null);
? ? ? ? ? ? holder.iv = (ImageView) convertView.findViewById(R.id.backup_record_item_image);
? ? ? ? ? ? holder.tv_time = (TextView) convertView.findViewById(R.id.backup_record_item_time);
? ? ? ? ? ? holder.tv_to = (TextView) convertView.findViewById(R.id.backup_record_item_to);
? ? ? ? ? ? holder.radioButton = (RadioButton) convertView
? ? ? ? ? ? ? ? ? ? .findViewById(R.id.backup_record_item_btn);
? ? ? ? ? ? holder.radioButton.setChecked(false);
? ? ? ? ? ? convertView.setTag(holder);
? ? ? ? } else {
? ? ? ? ? ? holder = (ViewHolder) convertView.getTag();
? ? ? ? }
?
? ? ? ? holder.iv.setImageResource(backup_record_item_image[position]);
? ? ? ? holder.tv_time.setText(backup_record_item_time[position]);
? ? ? ? holder.tv_to.setText(backup_record_item_to[position]);
?
? ? ? //黑体部分为实现单选功能部分
?
? ? ? ? holder.radioButton.setId(position);
? ? ? ? holder.radioButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
? ? ? ? ? ? ? ? if (isChecked) {
? ? ? ? ? ? ? ? ? ? if (temp != -1) {
? ? ? ? ? ? ? ? ? ? ? ? RadioButton tempButton = (RadioButton) activity.findViewById(temp);
? ? ? ? ? ? ? ? ? ? ? ? if (tempButton != null) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? tempButton.setChecked(false);
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? temp = buttonView.getId();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
?
? ? ? ? if (position == temp) {
? ? ? ? ? ? holder.radioButton.setChecked(true);
? ? ? ? } else {
? ? ? ? ? ? holder.radioButton.setChecked(false);
? ? ? ? }
? ? ? ? return convertView;
? ? }
?
? ? private class ViewHolder {
? ? ? ? ImageView iv;
?
? ? ? ? TextView tv_time;
?
? ? ? ? TextView tv_to;
?
? ? ? ? RadioButton radioButton;
?
? ? }
}