Ram Koti
Ram Koti

Reputation: 2211

NotifyDataSetChanged is not working in my code

adapter.notifyDataSetChanged(); is not working in android i am retrieving the values in for loop but the values are not displaying on the screen. My java file:

private static final String TAG_NAME="Notification";
String[] resultsAsString = {""};
ListView listView;
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, resultsAsString);
    ParseQuery<ParseObject> parseObjectParseQuery=new ParseQuery<ParseObject>("PushNotifications");
    parseObjectParseQuery.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> objects, ParseException e) {
            if (e==null){
                resultsAsString = new String[objects.size()];
                Log.d(TAG_NAME,"Objects size in MyService is:"+objects.size());

                if(objects.size()>0){
                    Log.d(TAG_NAME,"Object size is "+objects.size());
                    for(int index = 0; index < objects.size(); index++){
                        resultsAsString[index] = objects.get(index).getString("Message");
                        Log.d(TAG_NAME,"result value is:"+resultsAsString[index]);
                    }
                    adapter.notifyDataSetChanged();
                }else {
                    Log.d(TAG_NAME,"Object size is "+objects.size());
                }
            }
        }
    });
listView = (ListView) findViewById(R.id.notificatio_listView);
listView.setAdapter(adapter);

my logcat display: Objects size in MyService is:3 Object size is 3 First Nottification second message Third message

Upvotes: 0

Views: 431

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191728

You created a brand new array and it is not attached to the adapter.

resultsAsString = new String[objects.size()];

The notify does work, but you no longer have the reference to that dataset in the adapter in order to update it. The adapter is still holding onto the initial, empty String array

I'd recommend you use an Arraylist and simply adapter.add to fill in data. With that, there's no need to notify

And also use adapter.clear when the Parse result is complete unless you want duplicate data


Alternatively, just make a new Adapter and set it on the ListView again

Upvotes: 7

Related Questions