写这个只是为了自己记住,每次写都得想一会。
adapter 里面:
package com.gdkj.gddoctor.robotdoctor.adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.TextView; import com.gdkj.gddoctor.library.views.CircularImage; import com.gdkj.gddoctor.library.views.sortlist.SortModel; import com.gdkj.gddoctor.robotdoctor.R; import java.util.ArrayList; import java.util.HashMap; import java.util.List; /** * Created by 时间辉 on 2017/8/28. */ public class ChooseAdapter extends BaseAdapter { private List<SortModel> mList = null; private Context mContext; private LayoutInflater mInflater; private static HashMap<Integer,Boolean> isSelected; public ChooseAdapter(Context mContext) { this.mContext = mContext; this.mInflater = LayoutInflater.from(mContext); this.isSelected = new HashMap<Integer, Boolean>(); } /** * 当ListView数据发生变化时,调用此方法来更新ListView * @param list */ public void updateListView(List<SortModel> list){ this.mList = list; initDate(); notifyDataSetChanged(); } // 初始化isSelected的数据 private void initDate(){ for(int i=0; i<mList.size();i++) { getIsSelected().put(i,false); } } @Override public int getCount() { if (mList == null) { mList = new ArrayList<>(); } return mList.size(); } @Override public Object getItem(int position) { return mList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View view, ViewGroup parent) { ViewHolder viewHolder = null; final SortModel mContent = mList.get(position); if (view == null) { viewHolder = new ViewHolder(); view = mInflater.from(mContext).inflate(R.layout.item_choose, null); viewHolder.mName = (TextView) view.findViewById(R.id.item_choose_name); viewHolder.mAge = (TextView) view.findViewById(R.id.item_choose_age); viewHolder.mPhone = (TextView) view.findViewById(R.id.item_choose_phone); viewHolder.mLetter = (TextView) view.findViewById(R.id.catalog); viewHolder.mHead = (CircularImage) view.findViewById(R.id.item_choose_head); viewHolder.mState = (CheckBox) view.findViewById(R.id.item_choose_state); view.setTag(viewHolder); } else { viewHolder = (ViewHolder) view.getTag(); } //根据position获取分类的首字母的Char ascii值 int section = getSectionForPosition(position); //如果当前位置等于该分类首字母的Char的位置 ,则认为是第一次出现 if(position == getPositionForSection(section)){ viewHolder.mLetter.setVisibility(View.VISIBLE); viewHolder.mLetter.setText(mContent.getSortLetters()); }else{ viewHolder.mLetter.setVisibility(View.GONE); } viewHolder.mName.setText(this.mList.get(position).getName()); viewHolder.mState.setChecked(getIsSelected().get(position)); viewHolder.mState.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { isSelected.put(position,isChecked); } }); return view; } public static class ViewHolder { private TextView mLetter; private TextView mName; private TextView mAge; private TextView mPhone; private TextView mAddress; private CircularImage mHead; public CheckBox mState; } /** * 根据ListView的当前位置获取分类的首字母的Char ascii值 */ public int getSectionForPosition(int position) { return mList.get(position).getSortLetters().charAt(0); } /** * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置 */ public int getPositionForSection(int section) { for (int i = 0; i < getCount(); i++) { String sortStr = mList.get(i).getSortLetters(); char firstChar = sortStr.toUpperCase().charAt(0); if (firstChar == section) { return i; } } return -1; } /** * 提取英文的首字母,非英文字母用#代替。 * * @param str * @return */ private String getAlpha(String str) { String sortStr = str.trim().substring(0, 1).toUpperCase(); // 正则表达式,判断首字母是否是英文字母 if (sortStr.matches("[A-Z]")) { return sortStr; } else { return "#"; } } public Object[] getSections() { return null; } public static HashMap<Integer,Boolean> getIsSelected() { return isSelected; } public static void setIsSelected(HashMap<Integer,Boolean> isSelected) { ChooseAdapter.isSelected = isSelected; } }
获取选中的值:
private List<String> listItemID = new ArrayList<String>();
listItemID.clear(); for (int i = 0; i < adapter.getIsSelected().size(); i++) { if (adapter.getIsSelected().get(i)) { listItemID.add(SourceDateList.get(i).getName()); } } GLog.e(listItemID.toString());