Android ListView адаптер не обновится
У меня много проблем с обновлением представления списка с помощью специального адаптера. Я искал в Интернете в течение последнего часа, и я не могу найти какое-либо решение, которое бы обновило мой список.
Я попытался 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);