一.列表中每项的布局文件
<?xml version= "1.0" encoding ="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:orientation= "vertical" >
<TextView
android:id="@+id/catalog"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#E0E0E0"
android:layout_weight="1.0"
android:paddingLeft="5dip"
android:paddingTop="5dip"
android:paddingBottom="5dip"
android:textSize="13dp"
android:textColor="@color/index_font"
android:text="A"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dip"
android:background="@color/white"
>
<com.shjy.jingin.widget.HeaderView
android:id="@+id/select_staff_photo"
android:layout_width="46dip"
android:layout_height="46dip"
android:scaleType="centerCrop"
android:layout_centerVertical="true"
android:background="@drawable/head3x"
android:layout_marginLeft="10dip" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_toRightOf="@+id/select_staff_photo"
android:layout_marginLeft="10dip" >
<TextView
android:id="@+id/select_staff_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dip"
android:textSize="16dp"
android:textColor="@color/main_font" />
<TextView
android:id="@+id/select_staff_dept_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:textSize="13dp"
android:textColor="@color/main_font_color2" />
</LinearLayout>
<RelativeLayout
android:id="@+id/ry_phone_staff"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginRight="15dip" >
<CheckBox
android:id="@+id/select_staff_checkbox"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
style= "@style/DeptCheckboxTheme"
android:layout_marginRight="18dp"
/>
</RelativeLayout>
</RelativeLayout >
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#d1d1d1" />
</LinearLayout>
对复选框修饰的样式文件 @style/DeptCheckboxTheme
style.xml 中加入
<style name="DeptCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox" >
<item name= "android:button">@drawable/dept_checkbox_style </item>
</style >
资源文件dept_checkbox_style.xml
<?xml version= "1.0" encoding ="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable= "@drawable/work_diary_checkbok_pre" android:state_checked="true" />
<item android:drawable= "@drawable/work_diary_checkbok" android:state_checked="false" />
<item android:drawable= "@drawable/work_diary_checkbok" />
</selector>
二.适配器编写
1.在适配器中用一个map变量存储选择的值
2.在复选框组件上注册OnCheckedChangeListener事件 在该事件中将选择的值保存到map中
package com.shjy.jingin.model;
import java.util.HashMap;
import java.util.List;
import com.baidu.location.b.m;
import com.shidian.SDK.imageloader.core.DisplayImageOptions;
import com.shidian.SDK.imageloader.core.ImageLoader;
import com.shidian.SDK.imageloader.core.display.RoundedBitmapDisplayer2;
import com.shjy.jingin.R;
import com.shjy.jingin.activity.EditWorkDiaryActivity.SelectContext ;
import com.shjy.jingin.bean.SortModel;
import com.shjy.jingin.bean.Staff;
import com.shjy.jingin.context.JYBoxApplication;
import com.shjy.jingin.tools.Utils;
import com.shjy.jingin.util.RandomColor;
import com.shjy.jingin.widget.HeaderView;
import android.content.Context;
import android.graphics.Bitmap;
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.SectionIndexer;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class SelectStaffAdapter extends BaseAdapter implements SectionIndexer{
private List<SortModel> list = null;
private Context mContext ;
//由于存储选择的数据
private static HashMap<Long, Boolean> isSelected = null;
private ImageLoader loader ;
private DisplayImageOptions options ;
private JYBoxApplication jyBoxApplication ;
public SelectStaffAdapter(Context mContext, List<SortModel> list) {
this.mContext = mContext;
this.list = list;
isSelected = new HashMap<Long, Boolean>();
jyBoxApplication = JYBoxApplication.getInstance();
loader = ImageLoader. getInstance();// 图片的异步加载器
// 创建默认的显示的图片
options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable. head3x)
.showImageOnFail(R.drawable. head3x)// 设置图标加载或者解析错误时候的照片
.cacheInMemory( true).cacheOnDisc(true)// 设置下载的图片放在缓存中
.displayer( new RoundedBitmapDisplayer2(0)).build();// 设置图片圆角
}
public void updateListView(List<SortModel> list){
this.list = list;
notifyDataSetChanged();
}
public int getCount() {
return this .list .size();
}
public Object getItem(int position) {
return list .get(position);
}
public long getItemId(int position) {
return position;
}
public static HashMap<Long, Boolean> getIsSelected() {
return isSelected ;
}
public static void setIsSelected(HashMap<Long, Boolean> isSelected) {
SelectStaffAdapter. isSelected = isSelected;
}
public View getView(final int position, View view, ViewGroup arg2) {
ViewHolder viewHolder = null;
SortModel mContent = this.list .get(position);
final Staff staff = this.list.get(position).getStaff();
if (view == null) {
viewHolder = new ViewHolder();
view = LayoutInflater.from(mContext).inflate(R.layout. item_select_staff_list, null );
viewHolder. headerView = (HeaderView)view.findViewById(R.id.select_staff_photo );
// viewHolder.nameTextView = (TextView) view.findViewById(R.id.select_staff_name);
viewHolder. deptNameTextView = (TextView) view.findViewById(R.id.select_staff_dept_name );
viewHolder. tvLetter = (TextView) view.findViewById(R.id.catalog );
viewHolder. checkBox = (CheckBox)view.findViewById(R.id.select_staff_checkbox );
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
//将选择的值记录在 isSelected
final CheckBox checkBox = viewHolder.checkBox ;
checkBox.setOnCheckedChangeListener( new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isSelected.put(staff.getId(), isChecked);
}
});
Boolean checked = isSelected.get(staff.getId());
viewHolder. checkBox.setChecked(checked== null?false :checked);
int section = getSectionForPosition(position);
if(position == getPositionForSection(section)){
viewHolder. tvLetter.setVisibility(View. VISIBLE);
viewHolder. tvLetter.setText(mContent.getSortLetters());
} else{
viewHolder. tvLetter.setVisibility(View. GONE);
}
String serviceAddress = jyBoxApplication.getServiceAddress();
String path = "http://" + serviceAddress + "/" + staff.getPath();
loader.clearDiscCache();
loader.clearMemoryCache();
loader.displayImage(path, viewHolder. headerView, options );
// viewHolder.nameTextView.setText(staff.getName());
viewHolder. deptNameTextView.setText(staff.getDeptName());
return view;
}
public static class ViewHolder {
public HeaderView headerView ;
public TextView photoText ;
// public TextView nameTextView;
public TextView deptNameTextView ;
public TextView tvLetter ;
public CheckBox checkBox ;
}
public int getSectionForPosition(int position) {
return list .get(position).getSortLetters().charAt(0);
}
public int getPositionForSection(int section) {
for (int i = 0; i < getCount(); i++) {
String sortStr = list.get(i).getSortLetters();
char firstChar = sortStr.toUpperCase().charAt(0);
if (firstChar == section) {
return i;
}
}
return -1;
}
private String getAlpha(String str) {
String sortStr = str.trim().substring(0, 1). toUpperCase();
if (sortStr.matches("[A-Z]" )) {
return sortStr;
} else {
return "#" ;
}
}
@Override
public Object[] getSections() {
return null ;
}
}
三.将数据库中选择的数据进行回显
selectStaffAdapter = new SelectStaffAdapter(this, sortModels);
if(SelectContext.selectStaffs!=null&&SelectContext. selectStaffs.size()>0){
HashMap<Long, Boolean> selectMap = new HashMap<Long, Boolean>();
for(Staff staff:SelectContext.selectStaffs){
selectMap.put(staff.getId(), true);
}
selectStaffAdapter.setIsSelected(selectMap);
}
在数据适配器getView中
Boolean checked = isSelected .get(staff.getId());
viewHolder. checkBox.setChecked( checked==null? false:checked);
----
spring mvc+tomcat源码分析视频(链接复制在浏览器打开)
https://study.163.com/course/courseMain.htm?share=2&shareId=480000001919582&courseId=1209399899&_trace_c_p_k2_=6d81bc445e9c462ab8d6345e40f6b0bf