listview 与checkbox结合记录点击数据

1记录listview数据重写Adapter在getView中重写判断记录数据与全部数据作对比,从中判断checkbox选择情况
public class HanbanAdapter extends BaseAdapter {

		private List items;
		private LayoutInflater inflater;
		public HanbanAdapter(List items, Context context) {
			super();
			this.items = items;
			this.inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		@Override
		public int getCount() {
			return items.size();
		}
		@Override
		public Object getItem(int position) {
			return items.get(position);
		}
		@Override
		public long getItemId(int position) {
			return position;
		}
		@Override
		public View getView(int position, View contentView, ViewGroup arg2) {
			ViewHolder holder = null;
			if (contentView == null) {
				holder = new ViewHolder();
				contentView = inflater.inflate(R.layout.gjdz_gxfw_item, null);
				holder.checkBox1 = (CheckBox) contentView.findViewById(R.id.checkBox1);
				contentView.setTag(holder);
			} else {
				holder = (ViewHolder) contentView.getTag();
			}
			for(Fuwu s:savelist1){
				if(s.getId().equals(items.get(position).getId())){
					holder.checkBox1.setChecked(true);
				}
			}
			holder.checkBox1.setText("" + items.get(position).getCon());
			return contentView;
		}
		class lvButtonListener implements OnClickListener {

			/** The position. */
			private int position;

			lvButtonListener(int pos) {
				position = pos;
			}

			@Override
			public void onClick(final View v) {
			}
		}

		
		public void addItem(Fuwu item) {
			items.add(item);
		}
		public final class ViewHolder {
			public CheckBox checkBox1;
		}
	}

其中savelist为记录的历史数据,我是采用静态变量的方法存储,注意释放

其中totalHeight为listview与scrollview嵌套的一种解决方法

	adapter1=new HanbanAdapter(list1, Gjdz_gxfw_Activity.this);
					listView1.setAdapter(adapter1);
					
					 int totalHeight = 0;  
				        for (int i = 0; i < adapter1.getCount(); i++) {  
				            View listItem = adapter1.getView(i, null, listView1);  
				            listItem.measure(0, 0);  
				            totalHeight += listItem.getMeasuredHeight();  
				        }  
						android.widget.RelativeLayout.LayoutParams params1 = new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,totalHeight);

					
//					android.widget.RelativeLayout.LayoutParams params1 = new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,list1.size()*(getResources().getDimensionPixelOffset(R.dimen.items_hight)));
					params1.addRule(RelativeLayout.BELOW,R.id.l1);
					listView1.setLayoutParams(params1);
					listView1.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView arg0, View arg1,
							int arg2, long arg3) {
							// TODO Auto-generated method stub
							ViewHolder holder=(ViewHolder) arg1.getTag();
						if(holder.checkBox1.isChecked()){
							holder.checkBox1.setChecked(false);
							savelist1.remove(list1.get(arg2));
						}else{
							holder.checkBox1.setChecked(true);
							savelist1.add(list1.get(arg2));
						}
					}
					});

以上方法有时候可以实现,但是也可以不能实现的情况存在

因此引入第二种方式

在引入的listview中的activity中添加

private HashMap isSelected=new HashMap(); 
public HashMap getIsSelected() { 
        return isSelected; 
    } 

public void setIsSelected(HashMap isSelected) { 
        this.isSelected = isSelected; 
    } 

方法,然后进行

void initDate(List list) {
for(int i = 0; i < list.size(); i++){
getIsSelected().put(i, false);
for (Party p:allitems) {
if(list.get(i).getId().equals(p.getId())){
getIsSelected().put(i, true);
break;
}
}
}
}

进行填写数据

在getview中重写

@Override
public View getView(int position, View contentView, ViewGroup arg2) {
ViewHolder holder = null;
if (contentView == null) {
holder = new ViewHolder();
contentView = inflater.inflate(R.layout.order_part_choose_item, null);
holder.checkbox1 = (CheckBox) contentView.findViewById(R.id.checkBox1);
contentView.setTag(holder);
} else {
holder = (ViewHolder) contentView.getTag();
}
holder.checkbox1.setText(items.get(position).getFullname());】
holder.checkbox1.setChecked(getIsSelected().get(position));

return contentView;
}

这样就可以针对各种复杂的数据结构和复杂的laout布局中的选择情况进行记录了


其中checkbox,button和listview结合要注意listview的点解事件xml配置文件重写itemview时候:

            android:id="@+id/checkBox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="@dimen/main_padding"
            android:layout_marginRight="@dimen/main_padding"
            android:layout_marginTop="@dimen/list_padding"
            android:button="@anim/checkbox_chengshi"
            android:clickable="false"
            android:focusable="false"
            android:focusableInTouchMode="false" />

主要 android:clickable="false" android:focusable="false"  android:focusableInTouchMode="false" 在起作用



你可能感兴趣的:(listview 与checkbox结合记录点击数据)