基本信息
源码名称:android 多选菜单 例子 附完整源码
源码大小:0.67M
文件格式:.rar
开发语言:Java
更新时间:2013-07-05
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

          


row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@android:color/white" >

    <TextView
        android:id="@ id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_margin="5dp"
        android:layout_toLeftOf="@ id/checkbox"
        android:text="title"
        android:textColor="@android:color/black"
        android:textSize="20dp"
        android:textStyle="bold" >
    </TextView>

    <CheckBox
        android:id="@ id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dip"
        android:background="@drawable/chk"
        android:button="@null"
        android:focusable="false"
        android:focusableInTouchMode="false" >
    </CheckBox>
</RelativeLayout>

code:

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class NamesAdapter extends ArrayAdapter<NameBean> {

  private List<NameBean> list;
 private LayoutInflater inflator;

  public NamesAdapter(Activity context, List<NameBean> list) {
  super(context, R.layout.row, list);
  this.list = list;
  inflator = context.getLayoutInflater();
 }

  @Override
 public View getView(int position, View convertView, ViewGroup parent) {

   ViewHolder holder = null;
  if (convertView == null) {
   convertView = inflator.inflate(R.layout.row, null);
   holder = new ViewHolder();
   holder.title = (TextView) convertView.findViewById(R.id.title);
   holder.chk = (CheckBox) convertView.findViewById(R.id.checkbox);
   holder.chk
     .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

       @Override
      public void onCheckedChanged(CompoundButton view,
        boolean isChecked) {
       int getPosition = (Integer) view.getTag();
       list.get(getPosition).setSelected(view.isChecked());

       }
     });
   convertView.setTag(holder);
   convertView.setTag(R.id.title, holder.title);
   convertView.setTag(R.id.checkbox, holder.chk);
  } else {
   holder = (ViewHolder) convertView.getTag();
  }
  holder.chk.setTag(position);

   holder.title.setText(list.get(position).getName());
  holder.chk.setChecked(list.get(position).isSelected());

   return convertView;
 }

  static class ViewHolder {
  protected TextView title;
  protected CheckBox chk;
 }
}


When your listView is bind with data you can see checkbox in row with name and now you can get Selected name using NameBean Object and you can retrieve it from list.


                // Retrive Data from list Using for-each loop
                StringBuffer sb = new StringBuffer();
  for (NameBean bean : items) {

    if (bean.isSelected()) {
    sb.append(bean.getName());
    sb.append(",");
   }
  }