0

This is the part of code which i am trying to do please any one help me I am new to android develpment

This is the exact error which I am getting: java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131165289, class android.widget.ListView) with Adapter(class android.widget.ArrayAdapter)]

ListView requestListView;

ArrayList<String> requests = new ArrayList<String>();

ArrayAdapter arrayAdapter;

LocationManager locationManager;

LocationListener locationListener;

ArrayList<Double> requestLatitude = new ArrayList<Double>();

ArrayList<Double> requestLongitude = new ArrayList<Double>();

ArrayList<String> UserNames = new ArrayList<String>();

Button refresh;

public void updateListView(Location location){

    if(location!=null) {

        requests.clear();

        ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Request");

        final ParseGeoPoint geoPointLocation = new ParseGeoPoint(location.getLatitude(), location.getLongitude());

        query.whereNear("location",geoPointLocation);

        query.whereDoesNotExist("driverUserName");

        query.setLimit(10);

        query.findInBackground(new FindCallback<ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {

                if(e==null){
                    if(objects.size()>0){

                        requests.clear();

                        requestLongitude.clear();

                        requestLatitude.clear();

                        for (ParseObject object:objects){

                            ParseGeoPoint requestLocation = (ParseGeoPoint) object.get("location");

                            if(requestLocation!=null) {

                                Double distanceinMiles = geoPointLocation.distanceInKilometersTo(requestLocation);

                                Double DistanceOneDP = (double) Math.round(distanceinMiles * 10) / 10;

                                requests.add(DistanceOneDP.toString() + "Km");

                                arrayAdapter.notifyDataSetChanged();

                                requestLatitude.add(requestLocation.getLatitude());

                                requestLongitude.add(requestLocation.getLongitude());

                                UserNames.add(object.getString("username"));


                            }
                        }
                    }else{

                        requests.clear();

                        requests.add("No Near By request");
                    }

                    arrayAdapter.notifyDataSetChanged();

                }
            }
        });
    }

}
Community
  • 1
  • 1
Prashanth
  • 77
  • 2
  • 8
  • 1
    Possible duplicate of [Android, ListView IllegalStateException: "The content of the adapter has changed but ListView did not receive a notification"](https://stackoverflow.com/questions/3132021/android-listview-illegalstateexception-the-content-of-the-adapter-has-changed) – Rumit Patel Nov 22 '18 at 12:10

2 Answers2

0

Try this way first to set adapter into listview.

requestListView.setAdapter(adapter);

after that called arrayAdapter.notifyDataSetChanged();

0

Set adapter to Listview

requestListView.setAdapter(arrayAdapter); then

arrayAdapter.notifyDataSetChanged();