listview中的radiobutton如何实现单选并在avtivity中取到他的值

来源:互联网 发布:javascript公开课 编辑:程序博客网 时间:2024/06/05 10:09

写一个自己的适配器:

public class QW_HomePage_listAdapter extends BaseAdapter{ Context context; List<HashMap<String,Object>> datas; public static HashMap<String,Boolean> isSelected = new HashMap<String, Boolean>(); private LayoutInflater inflater = null;  boolean res; static int isFirst =0;public QW_HomePage_listAdapter(Context context,List<HashMap<String,Object>> datas) {//System.out.println("---in adapter----");this.context = context;this.datas = datas;inflater = LayoutInflater.from(context);for(int i = 0;i<datas.size();i++){//System.out.println("states:"+(Boolean)datas.get(i).get("state"));isSelected.put(String.valueOf(i), (Boolean)datas.get(i).get("state"));}}@Overridepublic int getCount() {return datas.size();}@Overridepublic Object getItem(int position) {return datas.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(final int position, View convertView, ViewGroup parent) {ViewHolder holder = null;if(convertView == null){//System.out.println("position == :"+position);holder = new ViewHolder();//获得layoutconvertView = inflater.inflate(R.layout.qw_homepage_item, null);holder.rb = (RadioButton) convertView.findViewById(R.id.qw_homepage_rb);//为view设置标签convertView.setTag(holder);}else{holder = (ViewHolder)convertView.getTag();}//System.out.println("(String)datas.get(position).get('textview')"+(String)datas.get(position).get("textview"));holder.rb.setText((String)datas.get(position).get("text"));//System.out.println("---states---"+(Boolean)datas.get(position).get("state"));holder.rb.setChecked((Boolean)datas.get(position).get("state"));//点击事件holder.rb.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View paramView) {//System.out.println("选中的是第"+position+"条记录");//重置,确保最多只有一项被选中for(String key:isSelected.keySet()){//System.out.println("key:"+key);isSelected.put(key,false);}isSelected.put(String.valueOf(position),true);QW_HomePage_listAdapter.this.notifyDataSetChanged();}});boolean res=false;        if(isSelected.get(String.valueOf(position)) == null || isSelected.get(String.valueOf(position))== false){        //System.out.println("ont  if");        res=false;            isSelected.put(String.valueOf(position), false);        }        else{            res=true;        }        holder.rb.setChecked(res);        return convertView;}

activity:取到radioButton更改后的值

boolean states[]={false,true,false,false};@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.qw_default_homepage);listview = (ListView)findViewById(R.id.qw_homepage_listview);layout = (LinearLayout)findViewById(R.id.qw_default_homepage);myGestureDetector = new GestureDetector(this);layout.setOnTouchListener(this);layout.setLongClickable(true);//设置title并获取控件back_btn = (Button)findViewById(R.id.qw_title_left_back);back_btn.setBackgroundResource(R.drawable.qw_left_back);back_btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View paramView) {// TODO Auto-generated method stubQW_DefaultHomePageActivity.this.finish();}});save_btn = (Button)findViewById(R.id.qw_title_right_button);save_btn.setVisibility(0);save_btn.setBackgroundResource(R.drawable.qw_right_save);title_top_tv = (TextView)findViewById(R.id.title_center_title);title_top_tv.setText("默认首页设置");String temps[]={"咨询","通信录","通知","办公"};//boolean states[]={false,true,false,false};for(int i = 0;i<temps.length;i++){HashMap<String, Object> map = new HashMap<String, Object>();map.put("text",temps[i]);map.put("state",states[i]);datas.add(map);}adapter = new QW_HomePage_listAdapter(QW_DefaultHomePageActivity.this, datas);listview.setAdapter(adapter);save_btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubsaveFile();Toast.makeText(QW_DefaultHomePageActivity.this, "保存成功", Toast.LENGTH_SHORT).show();}});}//end oncreatprivate HashMap<String, Boolean> state;//保存更改后的信息public void saveFile(){state=adapter.isSelected;for(int j=0;j<adapter.getCount();j++){System.out.println("state.get("+j+")=="+state.get(String.valueOf(j)));if(state.get(String.valueOf(j))!=null && state.get(String.valueOf(j))== true ){@SuppressWarnings("unchecked")HashMap<String, Object> map=(HashMap<String, Object>) adapter.getItem(j);String username=map.get("text").toString();
//取到更改后的RadioButton的值System.out.println("--username---"+username);}}Toast.makeText(QW_DefaultHomePageActivity.this, "保存成功", Toast.LENGTH_SHORT).show();  QW_DefaultHomePageActivity.this.finish();}
我的viewholder:

public class ViewHolder {public CheckBox  cb;public TextView  tv;public ImageView iv;public RadioButton rb;}

我的xml文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/qw_default_homepage"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#ffffff"    android:orientation="vertical" >    <include layout="@layout/qw_setting_title_top" /><RelativeLayout     android:id="@+id/RelativeLayout1"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ListView        android:id="@+id/qw_homepage_listview"        android:layout_width="fill_parent"        android:layout_height="fill_parent" >    </ListView></RelativeLayout></LinearLayout>

item的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:background="#ffffff"    android:orientation="horizontal" >    <RadioButton        android:id="@+id/qw_homepage_rb"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:textColor="#000000"        android:layout_marginLeft="10dp"        android:textSize="20sp"        android:focusable="false"        android:text="RadioButton" /></LinearLayout>


原创粉丝点击