android實現ListView中添加RaidoButton單選

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;

}

}

 

general_recover_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="fill_parent">
<RelativeLayout
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:paddingLeft="5dip"
 android:paddingRight="5dip"
 android:id="@+id/backup_record_item">
<ImageView
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:src="@drawable/icon"
 android:layout_alignParentLeft="true"
 android:id="@+id/backup_record_item_image"/>
 
<LinearLayout
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:orientation="vertical"
 android:layout_toRightOf="@+id/backup_record_item_image"
 android:id="@+id/backup_record_item_txt">
<TextView
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:text="時間"
 android:layout_alignTop="@+id/imageView1"
 android:layout_alignBottom="@+id/imageView1"
 android:id="@+id/backup_record_item_time"/>
<TextView
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:text="備份聯系人 到 "
 android:layout_toRightOf="@+id/imageView1"
 android:layout_alignTop="@+id/imageView1"/>
<TextView
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:text="手機/存儲卡"
 android:layout_toRightOf="@+id/imageView1"
 android:layout_alignTop="@+id/imageView1"
 android:id="@+id/backup_record_item_to" />
</LinearLayout>
<RadioButton
 android:layout_height="wrap_content"
 android:layout_width="wrap_content"
 android:layout_alignParentRight="true"
 android:id="@+id/backup_record_item_btn" />

<CheckBox
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:visibility="gone"
 android:text=""
 android:id="@+id/backup_record_item_check" />
</RelativeLayout>
</LinearLayout>

main.xml
 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical"
 android:id="@+id/recover_items">
 <ListView
 android:id="@+id/recover_list"
 android:cacheColorHint="#00000000"
 android:layout_height="match_parent"
 android:layout_width="fill_parent"/>
 <View
 android:id="@+id/recover_footer"
 android:layout_width="fill_parent"
 android:layout_height="40dip" />
 </LinearLayout>

發佈留言