Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid ListView adapter wont refresh
    primarykey
    data
    text
    <p>I'm having a lot of trouble refreshing a list view with a custom adapter. I've been searching online for the past hour and I can't seem to find any solution that will make my list view refresh. </p> <p>I've tried notifyDataSetChanged, and also listView.invalidate, but nothing seems to be working. Any help would be greatly appreciated. I can see the data being updated using logcat, but it is not being refreshed on the screen and I have no idea why.</p> <p>Below is the code.</p> <pre><code>ArrayList&lt;Student&gt; students = new ArrayList&lt;Student&gt;(); listview = (ListView) findViewById(R.id.listView); adapter = new StudentAdapter(this, R.layout.listitemlayout, students); listview.setAdapter(adapter); </code></pre> <p>Custom Adapter</p> <pre><code>public class StudentAdapter extends ArrayAdapter&lt;Student&gt;{ Context context; int layoutResourceId; ArrayList&lt;Student&gt; data = null; public StudentAdapter(Context context, int layoutResourceId, ArrayList&lt;Student&gt; data) { super(context, layoutResourceId, data); this.layoutResourceId = layoutResourceId; this.context = context; this.data = data; } public void updateStudentsList(ArrayList&lt;Student&gt; 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; } } </code></pre> <p>I update the data using a thread every so often.</p> <pre><code>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); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload