使用listview的缓存,将控件缓存之后。如果在if语句里将要设置的控件的值缓存,滑动之后会出现重复加载的问题。
目前在网上看大概有2种解决方案
1、不用缓存影响效率
public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub convertView = LayoutInflater.from(context).inflate( R.layout.menu_finaldish, null); TextView dishname = (TextView) convertView// 显示菜的名称 .findViewById(R.id.finaldish_name); TextView dishprice = (TextView) convertView// 显示菜的价格 .findViewById(R.id.finaldish_price); TextView dishdiscount = (TextView) convertView// 显示菜的折扣 .findViewById(R.id.finaldish_discount); TextView dishnum = (TextView) convertView// 显示菜的份数 .findViewById(R.id.finaldish_num); TextView dishlittlesum = (TextView) convertView//显示菜的小计 .findViewById(R.id.finaldish_littlesum); dishname.setText(finaldishlist.get(position).getName()); dishprice.setText(Double.toString(finaldishlist.get(position) .getPrice())); dishdiscount.setText(Double.toString(finaldishlist.get(position) .getDiscount())); dishnum.setText(Integer.toString(finaldishlist.get(position).getNum())); dishlittlesum.setText(Double.toString(finaldishlist.get(position).getLittlesum())); return convertView; }
2、使用缓存,将赋值写在if语句外面
public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView == null) { convertView = layoutInflater.inflate(R.layout.listitem1, null); viewHolder = new ViewHolder(); viewHolder.textView = (TextView) convertView .findViewById(R.id.textview1); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.textView.setText(lists.get(position).getBuscirclename()); viewHolder.textView.setTextColor(Color.BLACK); return convertView; }