Android - ListView getting populated twice
I am using the following piece of code to populate my ListView -
setListAdapter(new NotifListAdapter(this));
I call this in the onCreate() method of my activity, and the first time it is created, it populates fine. But when I go back and return to this activity it shows twice as many rows in the List! I have checked my db and its definitely not a backend issue. Also, when I force close the activity and restart it again, it populates fine. Could anybody provide an insight into the issue?
where this is the list adapter -
public class NotifListAdapter extends BaseAdapter {
public NotifListAdapter(Context context) {
mContext = context;
}
public int getCount() {
return rnList.size();
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
Notif开发者_Go百科View nv;
RichNotification rn = rnList.get(position);
if (convertView == null) {
nv = new NotifView(mContext, rn.getSubject(),
rn.getContent(), rn.getDate());
} else {
nv = (NotifView) convertView;
nv.setTitle(rn.getSubject());
nv.setDialogue(rn.getContent());
nv.setDate(rn.getDate());
}
return nv;
}
/**
* Remember our context so we can use it when constructing views.
*/
private Context mContext;
@Override
public Object getItem(int position) {
return null;
}
}
Your adapter is displaying data from rnList
: you are probably adding new objects to rnList
without clearing it first, which is why you are getting twice as many rows when you return to the Activity. Before you add data to rnList
call rnList.clear();
and that should probably fix your problem.
精彩评论