Search code examples
androidlistviewandroid-arrayadapter

notifyDataSetChanged method is not refreshing listview using updated data?


I am using an ArrayAdapter with filterable interface to implement search functionality in a listview.My code for adapter is following:

public class FlightsAdapter extends ArrayAdapter<Flight> implements Filterable {

    List<Flight> flightLists = new ArrayList<Flight>();
    private List<Flight> origFlightList;
    private Context context;
    Flight flight = null;

    public FlightsAdapter(Context context, int textViewResourceId,
            List<Flight> fLists) {
        super(context, textViewResourceId, fLists);
        this.context = context;
        this.flightLists = fLists;
    }

    public void updateFlightList(List<Flight> newData){
        this.flightLists.clear();
        this.flightLists = newData;

    }
public Filter getFilter() {
        return new Filter() {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                final FilterResults oReturn = new FilterResults();
                final List<Flight> results = new ArrayList<Flight>();
                if (origFlightList == null)
                    origFlightList = flightLists;
                if (constraint != null) {
                    if (origFlightList != null && origFlightList.size() > 0) {
                        for (final Flight f : origFlightList) {
                            if (f.getPlace().toLowerCase()
                                    .contains(constraint.toString()))
                                results.add(f);
                        }
                    }
                    oReturn.values = results;
                    oReturn.count = results.size();
                }
                return oReturn;
            }

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint,
                                          FilterResults results) {
                flightLists = (ArrayList<Flight>) results.values;
                notifyDataSetChanged();
            }
        };
    }

}

getFilter() and publishResults() methods get triggered properly on searching and flightLists get populated with new data but the listview remain same with no items change, I don't know what I am doing wrong in above code, Please help me figure out this problem


Solution

  • Here is the problem

     flightLists = (ArrayList<Flight>) results.values;
    

    Replace this with

    flightLists.clear();
    flightLists.addAll((ArrayList<Flight>) results.values);
    

    See if that fixes your issue.

    Later edit (explanation):

    When you created your adapter, you gave it a List object. The adapter thus has a reference, a pointer, to that List, from where it takes the data to display.

    When you have replaced that reference with a whole other block of memory

    flightLists = (ArrayList<Flight>) results.values;
    

    the adapter didn't updated the orignial data set (from where it actually was taking data to display). Thus, notifyDataSetChanged() didn't work.

    When updating the data in an adapter, you have to keep the original data structure reference intact. Clearing and populating with another data set is an option. Reinstantiating it (in any way) isn't.

    This is basic java.