重写一个自己的adapter,实现带checkbox的列表(这里用到了ViewHolder,好控制,且运行的次数也少提高了效率)
public class QW_push_listAdapter extends BaseAdapter{
Context context;
List<HashMap<String,Object>> datas;
public static HashMap<Integer,Boolean> isSelected;
private LayoutInflater inflater = null;
public QW_push_listAdapter(Context context,List<HashMap<String,Object>> datas) {
//System.out.println("---in adapter----");
this.context = context;
this.datas = datas;
inflater = LayoutInflater.from(context);
isSelected = new HashMap<Integer, Boolean>();
//给初始状态
for(int i = 0;i<datas.size();i++)
{
//System.out.println("states:"+(Boolean)datas.get(i).get("state"));
isSelected.put(i, (Boolean)datas.get(i).get("state"));
}
}
@Override
public int getCount() {
return datas.size();
}
@Override
public Object getItem(int position) {
return datas.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
//System.out.println("---getview ---: position == :"+position);
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.qw_push_item, null);
holder.tv = (TextView) convertView.findViewById(R.id.qw_push_item_text);
holder.iv = (ImageView) convertView.findViewById(R.id.qw_push_item_image);
holder.cb = (CheckBox) convertView.findViewById(R.id.qw_push_item_checkBox);
//为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.tv.setText((String)datas.get(position).get("textview"));
//System.out.println("image:"+Integer.parseInt( datas.get(position).get("imageview").toString()));
holder.iv.setBackgroundResource(Integer.parseInt(datas.get(position).get("imageview").toString()));
//System.out.println("isselected"+isSelected.get(position));
//System.out.println("---set states:---"+isSelected.get(position));
if(isSelected.get(position)){
holder.cb.setChecked(true);
}else{
holder.cb.setChecked(false);
}
holder.cb.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked) {
//System.out.println("--in in checked : ischecked:"+position);
isSelected.put(position, isChecked);
} else {
isSelected.put(position,isChecked);
}
//buttonView.setChecked((isSelected.get(position) == null ? false : true));
buttonView.setChecked(isSelected.get(position));
}
});
return convertView;
}
public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
QW_push_listAdapter.isSelected = isSelected;
}
}
写一个自己的viewHolder类
public class ViewHolder {
CheckBox cb;
TextView tv;
ImageView iv;
RadioButton rb;
}
Activity中的oncreat()方法中实现checkbox多选
Boolean states[]={true,false,false,false,false,false,false,false,false,false,false,false,false,
false,false,false,false,false,false,true};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.qw_push);
//设置标题
System.out.println("-----on push xml---");
title_top_tv = (TextView)findViewById(R.id.title_center_title);
title_top_tv.setText("推送设置");
title_top_back = (Button)findViewById(R.id.qw_title_left_back);
title_top_back.setBackgroundResource(R.drawable.qw_left_back);
title_top_save = (Button)findViewById(R.id.qw_title_right_button);
title_top_save.setBackgroundResource(R.drawable.qw_right_save);
title_top_save.setVisibility(0);
layout = (LinearLayout)findViewById(R.id.qw_push_layout);
push_listView = (ListView)findViewById(R.id.qw_push_list);
//设置手势滑动
myGestureDetector = new GestureDetector(this);
layout.setOnTouchListener(this);
layout.setLongClickable(true);
//点击“返回”时退出当前的页
title_top_back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
QW_PushActivity.this.finish();
}
});
//点击“保存”事件
title_top_save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
savefile();
Toast.makeText(QW_PushActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
}
});
//数据源、适配器,对列表进行操作
String temp_text[] ={"内部消息","个人资料","日程管理","周程","工作日报"};
int temp_image[] = {R.drawable.qw_neibuxiaoxi,R.drawable.qw_gerenzhiliao,
R.drawable.qw_richengguanli,R.drawable.qw_zhoubaoguanli,R.drawable.qw_gongzuoribao,
};
List<HashMap<String,Object>> datas = new ArrayList<HashMap<String,Object>>();
for(int i = 0;i<temp_text.length;i++){
HashMap<String,Object> map = new HashMap<String, Object>();
map.put("textview", temp_text[i]);
map.put("imageview", temp_image[i]);
map.put("state", states[i]);
datas.add(map);
}
list_adapter = new QW_push_listAdapter(this,datas);
push_listView.setAdapter(list_adapter);
}//end oncreat
private HashMap<Integer, Boolean> state;
//保存
public void savefile(){
state =list_adapter.isSelected;
String options="选择的项是:";
for(int j=0;j<list_adapter.getCount();j++){
//System.out.println("state.get("+j+")=="+state.get(j));
if(state.get(j)!=null && state.get(j)== true ){
@SuppressWarnings("unchecked")
HashMap<String, Object> map=(HashMap<String, Object>) list_adapter.getItem(j);
String username=map.get("textview").toString(); //得到更改后的值
//String id=map.get("friend_id").toString();
options+="\n"+"."+username;
System.out.println("----save-----"+options);
}
Toast.makeText(QW_PushActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
QW_PushActivity.this.finish();
}
}
item的xml文件:
<CheckBox
android:id="@+id/qw_push_item_checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:focusable="false" //不可获得焦点
android:clickable="false" //不可点击
android:focusableInTouchMode="false"//通过touch来获得焦点
/>