ListView 数据混乱处理
该继承自BaseAdapter的类,以下的重写方法,实现如下。<span style="font-family: Arial, Helvetica, sans-serif;"> //以下是使用数据模型,重新构建视图,在ListView中存在重用View的问题,故而,在滚动刷新一个新view的数据就是根据数据中得数据重新设置即可</span>
<pre name="code" class="java">@Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder viewHolder; if(view == null){ view = View.inflate(context, R.layout.stop_item_activity,null); viewHolder = new ViewHolder(); viewHolder.stopTitle = (TextView) view.findViewById(R.id.stop_title_textview); viewHolder.sortTextView = (TextView) view.findViewById(R.id.stop_seq_state_shape); viewHolder.liveIconImageView = (ImageView) view.findViewById(R.id.live_icon_imageview); viewHolder.stopDetailTextView = (TextView) view.findViewById(R.id.stop_detail_textview); view.setTag(viewHolder); }else{ viewHolder = (ViewHolder)view.getTag(); } //begin 实时公交页面渲染 // if(stopList.get(i).getHasBus()){ viewHolder.sortTextView.setVisibility(View.GONE); viewHolder.liveIconImageView.setVisibility(View.VISIBLE); viewHolder.stopDetailTextView.setVisibility(View.VISIBLE); viewHolder.stopTitle.setTextColor(Color.RED); }else{//此分支必须要,重置viewHolder中view中得值 viewHolder.sortTextView.setVisibility(View.VISIBLE); viewHolder.liveIconImageView.setVisibility(View.GONE); viewHolder.stopDetailTextView.setVisibility(View.GONE); viewHolder.stopTitle.setTextColor(Color.GRAY); } String tipText = "车辆"+stopList.get(i).getBusselfid()+"于"+stopList.get(i).getActdatetime()+"到达此站"; viewHolder.stopDetailTextView.setText(tipText); //end viewHolder.stopTitle.setText(stopList.get(i).getStop_name()); viewHolder.sortTextView.setText(i+1+ ""); viewHolder.line_code = stopList.get(i).getLine_code(); viewHolder.line_seq = stopList.get(i).getStop_seq(); return view; } public class ViewHolder{ public TextView stopTitle; public String line_code; public String line_seq; public TextView sortTextView;//序号,1,2,3 public boolean hasBus; public String busSelfId; public String actDatetime; public ImageView liveIconImageView; public TextView stopDetailTextView; }
版权声明:本文为博主原创文章,未经博主允许不得转载。