Android ListView适配器不会刷新

时间:2013-05-02 03:41:17

标签: android listview refresh

使用自定义适配器刷新列表视图时遇到了很多麻烦。我一直在网上搜索过去的一小时,我似乎无法找到任何解决方案,使我的列表视图刷新。

我已经尝试过notifyDataSetChanged,还有listView.invalidate,但似乎没有任何工作。任何帮助将不胜感激。我可以看到使用logcat更新数据,但它没有在屏幕上刷新,我不明白为什么。

以下是代码。

ArrayList<Student> students = new ArrayList<Student>();

listview = (ListView) findViewById(R.id.listView);
adapter = new StudentAdapter(this, R.layout.listitemlayout, students);  
listview.setAdapter(adapter);

自定义适配器

public class StudentAdapter extends ArrayAdapter<Student>{

    Context context; 
    int layoutResourceId;    
    ArrayList<Student> data = null;

    public StudentAdapter(Context context, int layoutResourceId, ArrayList<Student> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    public void updateStudentsList(ArrayList<Student> newlist){
        data.clear();
        data = newlist;
        this.notifyDataSetChanged();
    }

    public void updateStudentTime(){
        for (Student s : data) {
            s.updateElapsedTime();          
        }
        this.notifyDataSetChanged();
    }

    @Override
    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);

            TextView title = (TextView)row.findViewById(R.id.txtTitle);
            title.setText(student.getFirstname() + " " + student.getLastname());

            TextView subTitle = (TextView)row.findViewById(R.id.txtSubTitle);
            subTitle.setText(student.getStudentID());       

            TextView duration = (TextView)row.findViewById(R.id.textDuration);
            duration.setText(student.getElapsedTime());
        }
        return row;
    }
}

我经常使用线程更新数据。

Runnable runnable = new Runnable() {
            public void run() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        // Updates how long the student is in the centre.
                        adapter.updateStudentTime();
                        adapter.notifyDataSetChanged();
                        listview.invalidate();
                        Log.i("Debug", "Running on UI Thread");
                    }
                });
                handler.postDelayed(this, 1000);
            }
        };
        handler.postDelayed(runnable, 1000);

2 个答案:

答案 0 :(得分:5)

ListView将回收视图,因此您必须在getView中设置数据。 而f(row == null){表示数据不会刷新,只使用旧数据。

你应该这样做:

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;
}

答案 1 :(得分:0)

认为以下代码阻止您刷新列表。

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

代码表示当列表刷新时。将检查行是否为空(表示是否创建了屏幕中的行显示)。刷新列表时,已创建行。所以它不是NULL,新数据不会设置!

你应该改变这样的事情:

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);
    ...

}