我建立一個 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;
}
}
看你數據源的ArrayList,是不是靜態變量,而且每次初始化的時候都沒有clear。