Android—滑动ListView保存选中状态

通常,我们使用listview,滑动后,view会被销毁,重新滑动,item的checkbox选中状态会回到默认状态,这里我使用一种方法,保存状态,重新显示是,把原来的状态转到view上面。

首先,写一个实体类,保存数据,用于显示

package com.example.info;

public class Goods {

	public int checkimage;
	public String goodsname;
	public String text;
	public boolean ischeck;

	public boolean isIscheck() {
		return ischeck;
	}

	public void setIscheck(boolean ischeck) {
		this.ischeck = ischeck;
	}
	public int getCheckimage() {
		return checkimage;
	}

	public void setCheckimage(int checkimage) {
		this.checkimage = checkimage;
	}



	public String getGoodsname() {
		return goodsname;
	}

	public void setGoodsname(String goodsname) {
		this.goodsname = goodsname;
	}

	public Goods(int checkimage, String goodsname,boolean ischeck) {
		super();
		this.ischeck=ischeck;
		this.checkimage = checkimage;
		this.goodsname = goodsname;
	}

	public Goods() {
		super();
	}

	@Override
	public String toString() {
		return super.toString();
	}
}

再写一个adapter

package com.example.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import com.example.info.Goods;
import com.example.listviewselectitem.R;

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.ImageView;
import android.widget.TextView;

public class AppointmentAdapter extends BaseAdapter {
	// 填充数据的list
	private ArrayList foodlist;
	// 上下文
	private Context context;
	// 用来导入布局
	private LayoutInflater inflater = null;

	// 构造器
	public AppointmentAdapter(ArrayList list, Context context) {
		this.context = context;
		this.foodlist = list;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return foodlist.size();
	}

	@Override
	public Object getItem(int position) {
		return foodlist.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			// 获得ViewHolder对象
			holder = new ViewHolder();
			// 导入布局并赋值给convertview
			convertView = inflater.inflate(R.layout.item, null);
			holder.imageView = (ImageView) convertView
					.findViewById(R.id.food_imager);
			holder.txt2 = (TextView) convertView.findViewById(R.id.price);
			holder.cb = (CheckBox) convertView.findViewById(R.id.check_box);
			// 为view设置标签
			convertView.setTag(holder);
		} else {
			// 取出holder
			holder = (ViewHolder) convertView.getTag();
		}
		// 获取数据
		Goods goods = foodlist.get(position);

		// 将数据填充到当前convertView的对应控件中

		holder.imageView.setImageResource(goods.checkimage);
		holder.txt2.setText(goods.goodsname);
		// 设置list中TextView的显示
		return convertView;
	}

	public static class ViewHolder {
		public TextView txt2;
		public ImageView imageView;
		public CheckBox cb;
	}
}

activity文件

package com.example.listviewselectitem;

import java.util.ArrayList;
import com.example.adapter.AppointmentAdapter;
import com.example.adapter.AppointmentAdapter.ViewHolder;
import com.example.info.Goods;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity implements OnClickListener,
		OnItemClickListener {

	private ListView listView;
	private ArrayList goods = new ArrayList();
	private AppointmentAdapter adapter;
    private String[] good={"龙井","绿茶","营养快线","怡宝","范姐炒蛋","牛腩粉","广州早擦","柳州螺蛳粉","桂林米粉","过桥米线","康师傅","牛肉面","麻辣烫"};
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();// 初始化控件
		initData();// 初始化虚拟数据
		adapter = new AppointmentAdapter(goods, getApplicationContext());
		listView.setAdapter(adapter);
	}

	/**
	 * 初始化控件
	 * */
	public void initView() {
		listView = (ListView) findViewById(R.id.drink_list);// listview列表控件
		listView.setOnItemClickListener(this);
	}

	/**
	 * 初始化数据
	 * */
	public void initData() {
			
		Class cls = R.drawable.class;
		try {
			for(int i=0;i parent, View view, int position,
			long id) {
		// 取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例的步骤
		ViewHolder holder = (ViewHolder) view.getTag();
		// 改变CheckBox的状态
		holder.cb.toggle();
		
		boolean ischeck=goods.get(position).isIscheck(); 	
		String  goodsname=goods.get(position).goodsname;
		Class cls = R.drawable.class;
		if(ischeck){
		try {
			goods.set(position, new Goods(cls.getDeclaredField("circularcheckbox_normal").getInt(null), //如果已经选中,改为未选中状态,保存在															//goods里面
					goodsname,false));
		} catch (Exception e){
			// TODO Auto-generated catch block
			e.printStackTrace();
		  }
		}else{
			try {
				goods.set(position, new Goods(cls.getDeclaredField("circularcheckbox_press").getInt(null), //如果未选中,改为选中保存
						goodsname,true));
			} catch (Exception e){
				// TODO Auto-generated catch block
				e.printStackTrace();
			  }
		}
		adapter = new AppointmentAdapter(goods, getApplicationContext());  //再一次刷新adapter和listview
		listView.setAdapter(adapter);
	}
}



工程源码:http://download.csdn.net/detail/weiguishan/8408475

你可能感兴趣的:(android)