我建立一个 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;
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。