ListView Android adapter not updating

I am having trouble updating the list using the adapter. I searched online for the past hour, and I cannot find any solution that will force me to update the list.

I tried notifyDataSetChanged as well as listView.invalidate but nothing works. Any help would be greatly appreciated. I can see that the data is being updated using logcat, but it is not being updated on the screen, and I have no idea why.

Below is the code.

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

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

I update data using a stream so often.

 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); 
+6
source share
2 answers

ListView will rework the view, so you must set the data in getView. and I f(row == null){ means that the data will not be updated, use only old data.

you need to do the following:

 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; } 
+5
source

Think that the code below does not allow you to update the list.

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

The code says that when updating the list. It will check that the string is NULL (the average value of displaying the string on the screen is created or not). When you update the list, a row is already created. So this is NOT NULL, new data will not be installed!

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); ... } 
0
source

Source: https://habr.com/ru/post/944090/


All Articles