AngryMan Studios AngryMan Studios - 1 year ago 123
Java Question

Android: ArrayList not updating after using .clear()

I'm new to android development so please bear with me.

I have a custom ArrayAdapter which i want to update with swipe down to refresh.

I understand in order to do this i need to:

  • clear the arraylist that holds my data (with .clear())

  • Update the arraylist with the new data (using the method - getAbsentTeachers())

  • use notifyDataSetChanged() on the arrayadapter

After i clear the ArrayList, call getAbsentTeachers() and notifyDataSetChanged() the ArrayList appears to remain empty even though getAbsentTeachers() is beeing called. I know this because nothing is displayed in my listview.

getAbsentTeachers() populates the arrayList fine when my app is first launched however it doesn't seem to work when i call it again to update the arraylist.

Any ideas as to why the array is not beeing updated?

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

private ArrayList<Absent> absences = new ArrayList<>();
private SwipeRefreshLayout swipeContainer;

protected void onCreate(Bundle savedInstanceState) {

//---------------------INITIALISING PARSE-------------------------------

// Add your initialization code here
Parse.initialize(new Parse.Configuration.Builder(getApplicationContext())
//Information obscured for privacy - tested and working though.

ParseACL defaultACL = new ParseACL();
// Optionally enable public read access.
// defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);


//-----------------------CREATE ADAPTER OBJECT--------------------------------


final ListView lv = (ListView)findViewById(;
final ArrayAdapter<Absent> adapter = new absenceArrayAdapter(this, 0, absences);

swipeContainer = (SwipeRefreshLayout) findViewById(;
// Setup refresh listener which triggers new data loading
swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
public void onRefresh() {
// Code to refresh the list.
// Call swipeContainer.setRefreshing(false) when finished.


// Populates absences ArrayList with absence data
public void getAbsentTeachers() {

ParseQuery<ParseObject> query = ParseQuery.getQuery("Absences");

query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if(e == null){

for(ParseObject object : objects){

String name = String.valueOf(object.get("name"));
Log.i("Name", name);
String desc = String.valueOf(object.get("description"));
Log.i("Description", desc);

absences.add(new Absent(name, desc, "employees"));


} else {

Log.i("Get data from parse", "There was an error getting data!");



//custom ArrayAdapter
class absenceArrayAdapter extends ArrayAdapter<Absent> {

private Context context;
private List<Absent> absencesList;

public absenceArrayAdapter(Context context, int resource, ArrayList<Absent> objects) {
super(context, resource, objects);

this.context = context;
this.absencesList = objects;

public View getView(int position, View convertView, ViewGroup parent) {
//get the property we are displaying
Absent teacher = absencesList.get(position);

//get the inflater and inflate the XML layout for each item
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_listview, null);

TextView name = (TextView) view.findViewById(;
TextView description = (TextView) view.findViewById(;
ImageView teacherImage = (ImageView) view.findViewById(;

String teacherName = teacher.getTeacherName();

int descriptionLength = teacher.getDescription().length();
if(descriptionLength >= 75){
String descriptionTrim = teacher.getDescription().substring(0, 75) + "...";

return view;



Answer Source

Move these two lines to after the if-else within the done of the Parse call.


You currently are not waiting for the Parse call to finish before notifying the adapter.

Another solution is to change

absences.add(new Absent(name, desc, "employees"));


adapter.add(new Absent(name, desc, "employees"));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download