开发者社区> 问答> 正文

Android ListView重复加载的问题

我建立一个 ListViewView,并重写了自己的 adapter(继承BaseAdapter),数据源是一个ArrayList.
,ArrayList里的数据是 1,2,3。当我初始化 ListView时,显示的是 1,2,3。
以上看起来非常正常,但当我按 后退键,然后重新 进入 ListView的时候,诡异的事情就发生了。。
当我第二次进入 ListView的时候,屏幕上显示的 数据 成了 1,2,3, 1,2,3 .
然后我按下 后退键,再进入 ListView, 屏幕上的 数据 成了 1,2,3, 1,2,3 ,1,2,3

如此循环往复。。总之,每次进入ListView,都得重新读取ArrayList的数据。请问是什么原因导致的呢?
以下是adapter源码

 public class chatListAdapter extends BaseAdapter{
private Context context;                        //运行上下文   
private List<Map<String, Object>> listItems;    //联系人信息集合   
private LayoutInflater listContainer;           //视图容器   
private boolean[] hasChecked;                   //记录联系人选中状态   
public final class ListItemView{                //自定义控件集合     


        public TextView RemoteAudio;
        public TextView LocalAudio;
        public TextView RemoteId;     
        public TextView LocalId;
        public TextView MessageType;

        public ImageView RemoteImage;
        public ImageView LocalImage;
        public TextView RemoteSay;
        public TextView LocalSay;

 }     
public chatListAdapter(Context context, List<Map<String, Object>> listItems) {   
    Log.v("construtor", "constructor11");
    this.context = context;            
    listContainer = LayoutInflater.from(context);   //创建视图容器并设置上下文   
    this.listItems = listItems;   
    hasChecked = new boolean[getCount()];   
}   



@Override
public int getCount() {
    // TODO Auto-generated method stub
    return listItems.size();


}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {


    // TODO Auto-generated method stub
    Log.v("getview","getview11");
    final int selectID = position;
    ListItemView  listItemView = null;  
     listItemView = new ListItemView();  
    if (convertView == null) {   

        //获取list_item布局文件的视图   
        convertView = listContainer.inflate(R.layout.chat_list_item, null);   
        //获取控件对象   
        listItemView.RemoteAudio = (TextView)convertView.findViewById(R.id.RemoteAudio); 
        listItemView.LocalAudio = (TextView)convertView.findViewById(R.id.LocalAudio);   
        listItemView.RemoteId = (TextView)convertView.findViewById(R.id.RemoteId); 
        listItemView.LocalId = (TextView)convertView.findViewById(R.id.LocalId);
        listItemView.MessageType= (TextView)convertView.findViewById(R.id.MessageType);   
        listItemView.RemoteImage = (ImageView)convertView.findViewById(R.id.RemoteImage);
        listItemView.LocalImage=(ImageView)convertView.findViewById(R.id.RemoteImage);
        listItemView.RemoteSay=(TextView)convertView.findViewById(R.id.RemoteSay);
        listItemView.LocalSay=(TextView)convertView.findViewById(R.id.LocalSay);
        //设置控件集到convertView   
        convertView.setTag(listItemView);  
    }else{listItemView=(ListItemView)convertView.getTag();}

        //set rma,lca,mst,rid,lid into invisible
        //put data from list into view
        String strPosition=""+position;

        int RR=0;
        RR++;
        Log.v("11aa21","RR"+RR);

        listItemView.RemoteId.setText((String)listItems.get(position).get("RemoteId"));


// String idd=Regist.generateId();
 // listItemView.RemoteId.setText(" "+idd+" ");
 Log.v("11aa21","PositionI"+strPosition);
 Log.v("RemoteID","Remote+"+(String)listItems.get(position).get("RemoteId"));
        listItemView.LocalId.setText((String) listItems.get(position).get("LocalId"));
        Log.v("11aa21","PositionI"+strPosition);
        Log.v("RemoteID","LocalID+"+(String)listItems.get(position).get("LocalId"));


        ////
        listItemView.RemoteAudio.setText((String)listItems.get(position).get("RemoteAudio"));
        listItemView.LocalAudio.setText((String)listItems.get(position).get("LocalAudio"));
        ////
        listItemView.MessageType.setText((String)listItems.get(position).get("MessageType"));
        ///
        listItemView.RemoteImage.setBackgroundResource(R.drawable.ic_launcher);
        listItemView.LocalImage.setBackgroundResource(R.drawable.ic_launcher);
        ///
        listItemView.RemoteSay.setText((String)listItems.get(position).get("RemoteSay"));
        listItemView.LocalSay.setText((String)listItems.get(position).get("LocalSay"));


// String idd=Regist.generateId(); 
 // listItemView.LocalSay.setText(idd);
 // listItemView.RemoteSay.setText("rms"+position);
        Log.i("tttt","ttttt"+listItemView.LocalSay.getText().toString());
        //decide wether Ls,LI,RS,RI invisible
        //if msgtype=??,then set..invisible,set XX onclick method=??
        String MsgType=listItemView.MessageType.getText().toString();
        Log.i("msgt",MsgType);

        if (MsgType.equals("9d")){
            Log.v("MsgType", "Text");
        listItemView.LocalSay.setVisibility(View.INVISIBLE);
        listItemView.LocalImage.setVisibility(View.INVISIBLE);
        listItemView.RemoteSay.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.v("1ab",((TextView) v).getText().toString());
                Toast.makeText(null, "111",Toast.LENGTH_LONG).show();
            }
        });
        }

    return convertView;
}


} 

展开
收起
爵霸 2016-06-16 10:26:57 2899 0
1 条回答
写回答
取消 提交回答
  • 看你数据源的ArrayList,是不是静态变量,而且每次初始化的时候都没有clear。

    2019-07-17 19:40:25
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载