맞춤 어댑터로 목록보기를 새로 고치는 데 많은 문제가 있습니다. 나는 지난 1 시간 동안 온라인으로 검색을 해왔고 목록보기를 새로 고칠 수있는 솔루션을 찾을 수없는 것 같습니다.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);
업데이트 UI 스레드 :
당신이 뭔가를 변경해야합니다. adapter.updateStudentTime();을 호출하십시오. runonuithread를 사용하십시오 – Raghunandan