/**
* 模拟数据
*/
private void initPersonData(){
GoodsMainInformation goodsMainInformation;
for(int i=1;i<=goodslist.size();i++){
goodsMainInformation = new GoodsMainInformation();
goodsMainInformation.setGoodname(goodsName[i-1]+" "+goodCode[i-1]);
goodsMainInformation.setGoodPrice( Float.toString(goodsPrice[i-1])+"RMB");
goodsInformation.add(goodsMainInformation);
}
}
//自定义ListView适配器
class MyListAdapter extends BaseAdapter{
List<Boolean> mChecked;
List<GoodsMainInformation> listGoods;
HashMap<Integer,View> map = new HashMap<Integer,View>();
public MyListAdapter(List<GoodsMainInformation> list){
listGoods = new ArrayList<GoodsMainInformation>();
listGoods = list;
mChecked = new ArrayList<Boolean>();
for(int i=0;i<list.size();i++){
mChecked.add(false);
}
}
@Override
public int getCount() {
return listGoods.size();
}
@Override
public Object getItem(int position) {
return listGoods.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder holder = null;
if (map.get(position) == null) {
Log.e("MainActivity","position1 = "+position);
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = mInflater.inflate(R.layout.listitem, null);
holder = new ViewHolder();
holder.selected = (CheckBox)view.findViewById(R.id.list_select);
holder.name = (TextView)view.findViewById(R.id.list_name);
holder.price = (TextView)view.findViewById(R.id.list_price);
final int p = position;
map.put(position, view);
holder.selected.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox)v;
mChecked.set(p, cb.isChecked());
}
});
view.setTag(holder);
}else{
Log.e("MainActivity","position2 = "+position);
view = map.get(position);
holder = (ViewHolder)view.getTag();
}
holder.selected.setChecked(mChecked.get(position));
holder.name.setText(listGoods.get(position).getGoodname());
holder.price.setText(listGoods.get(position).getGoodPrice());
return view;
}
}
static class ViewHolder{
CheckBox selected;
TextView name;
TextView price;
}
ListView
最新推荐文章于 2023-09-22 14:56:31 发布