package com.example.radiobuttonlisttest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class RadioButtonList extends Activity {
private ListView radioButtonList;
private RadioAdapter adapter;
// 模拟几个数据,作为List的条目
private String[] authors = { "芥川龙之介", "三岛由纪夫", "川端康成", "村上春树", "东野圭吾",
"张爱玲", "金庸", "钱钟书", "老舍", "梁实秋", "亨利米勒", "海明威", "菲兹杰拉德", "凯鲁亚克",
"杰克伦敦", "小仲马", "杜拉斯", "福楼拜", "雨果", "巴尔扎克", "莎士比亚", "劳伦斯", "毛姆",
"柯南道尔", "笛福" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_radio_button_list);
radioButtonList = (ListView) findViewById(R.id.list);
adapter = new RadioAdapter(this, authors);
radioButtonList.setAdapter(adapter);
}
}
package com.example.radiobuttonlisttest;
import android.content.Context;
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.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class RadioAdapter extends BaseAdapter {
private LayoutInflater inflater;
private String[] authors;
private viewHolder holder;
// 标记用户当前选择的那一个作家
private int index = -1;
private Context c;
public RadioAdapter(Context c, String[] authors) {
super();
this.c = c;
this.authors = authors;
inflater = LayoutInflater.from(c);
}
@Override
public int getCount() {
return authors.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
holder = new viewHolder();
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_list, null);
holder.nameTxt = (TextView) convertView.findViewById(R.id.author);
holder.selectBtn = (RadioButton) convertView
.findViewById(R.id.radio);
convertView.setTag(holder);
} else {
holder = (viewHolder) convertView.getTag();
}
holder.nameTxt.setText(authors[position]);
holder.selectBtn
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Toast.makeText(c, "您选择的作家是:" + authors[position],
Toast.LENGTH_LONG).show();
index = position;
notifyDataSetChanged();
}
}
});
if (index == position) {// 选中的条目和当前的条目是否相等
holder.selectBtn.setChecked(true);
} else {
holder.selectBtn.setChecked(false);
}
return convertView;
}
public class viewHolder {
public TextView nameTxt;
public RadioButton selectBtn;
}
}