一年前做的这个,现在突然有人来问我这个,这里贴出关键代码,adapter里面,完全可以解决问题的。留着自己以后用
package com.stgmobile.cyt.adapter;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
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.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import com.stgmobile.cyt.activity.R;
import com.stgmobile.cyt.activity.financing.ReportListActivity;
import com.stgmobile.cyt.utils.img.ImageLoader;
public class MyListAdapter extends BaseAdapter {
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
private LayoutInflater inflater;
ArrayList<HashMap<String, Object>> list;
Context c;
Activity act;
public MyListAdapter(Activity context,
ArrayList<HashMap<String, Object>> list2) {
inflater = LayoutInflater.from(context);
this.c = context;
this.list = list2;
act = context;
}
public void setList(ArrayList<HashMap<String, Object>> list) {
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private int temp = -1;
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder myHolder;
if (convertView == null) {
myHolder = new Holder();
convertView = inflater.inflate(R.layout.reportlist_item, null);
myHolder.img = (ImageView) convertView
.findViewById(R.id.report_item_img);
myHolder.title = (TextView) convertView
.findViewById(R.id.report_item_name);
myHolder.money = (TextView) convertView
.findViewById(R.id.report_item_money);
myHolder.mailway = (TextView) convertView
.findViewById(R.id.report_item_type);
myHolder.content = (TextView) convertView
.findViewById(R.id.report_item_content);
myHolder.btn_rdo = (RadioButton) convertView
.findViewById(R.id.report_item_check);
myHolder.btn_rdo.setChecked(false);
convertView.setTag(myHolder);
} else {
myHolder = (Holder) convertView.getTag();
}
ImageLoader loader = new ImageLoader(c);
loader.DisplayImage(list.get(position).get("img").toString(),
myHolder.img);
myHolder.title.setText(list.get(position).get("name").toString());
myHolder.money.setText(list.get(position).get("money").toString());
myHolder.mailway.setText(list.get(position).get("mailway").toString());
myHolder.content
.setText(!"".equals(list.get(position).get("repay_content")
.toString())
&& list.get(position).get("repay_content").toString()
.length() > 140 ? list.get(position)
.get("repay_content").toString().substring(0, 140)
.concat("...") : list.get(position)
.get("repay_content").toString());
myHolder.btn_rdo.setId(position);
myHolder.btn_rdo
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
if (temp != -1) {
RadioButton tempButton = (RadioButton) act
.findViewById(temp);
if (tempButton != null) {
tempButton.setChecked(false);
}
}
temp = buttonView.getId();
ReportListActivity.checked_map=list.get(position);
}
}
});
if (position == temp) {
myHolder.btn_rdo.setChecked(true);
} else {
myHolder.btn_rdo.setChecked(false);
}
return convertView;
}
class Holder {
ImageView img;
RadioButton btn_rdo;
TextView title;
TextView money;
TextView mailway, content;
}
}
实现listview里radiobutton单选,布布扣,bubuko.com
原文:http://blog.csdn.net/enkezhang/article/details/22857487