Android ListView adapter wont refresh

拈花ヽ惹草 提交于 2019-12-03 14:26:10

ListView will recycle the view, so you must set data in getView. and if(row == null){ means that data will not refresh, only use the old data.

you should do like this:

Class ViewHolder {
   TextView title;
   TextView subTitle;
   TextView duration;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ViewHolder holder = null;
    if(row == null){
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        Student student = data.get(position);
        holder = new ViewHolder();
        holder.title = (TextView)row.findViewById(R.id.txtTitle);
        holder.subTitle = (TextView)row.findViewById(R.id.txtSubTitle);
        holder.duration = (TextView)row.findViewById(R.id.textDuration);
        row.setTag(holder);
    } else {
        holder = row.getTag();
    }
    holder.title.setText(student.getFirstname() + " " + student.getLastname());
    holder.subTitle.setText(student.getStudentID());   
    holder.duration.setText(student.getElapsedTime());    
    return row;
}

Think the below code prevent you refresh the list.

if(row == null){    
...
}

The code said that when the list refresh. It will be check the row is null (mean the row display in Screen is created or not ). When you refresh the list, the row already created. So it is NOT NULL, the new data will not set !

You should change something like this:

public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;

    if(row == null){
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

    } 

    Student student = data.get(position);
    ...

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!