8

I'm trying to display a list of items fetched from a url but I only want to fetch 20 of them at a time... so I've implemented an OnScrollListener to fetch the items when the users is on the last item of the listview.

The items are fetched but my only problem is that the listview is not updated: here's my code so far:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    parentView = inflater.inflate(R.layout.activity_event_speakers_alphabetically, container,false);
    listView = (ListView) parentView
            .findViewById(R.id.speakersAlphabeticallyActivityList);
    nameOrderedMembers.addDataBatch(communityMembers);
    nameOrderedAdapter = DelegateViewStateAdapterFactory
            .makeUserListAdapter(getActivity(),
                    nameOrderedMembers.getSortedData(), DelegateViewStateAdapterFactory.OrderType.NAME);
    listView.setAdapter(nameOrderedAdapter);
    AQuery aQuery = new AQuery(listView);
    aQuery.id(R.id.speakersAlphabeticallyActivityList).scrolled(new EndlessScrollListener());
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
            if (nameOrderedAdapter.getItem(arg2) instanceof User) {
                User u = (User) nameOrderedAdapter.getItem(arg2);
                Intent i = new Intent(getActivity(),
                        UserProfileFragmentActivity.class);
                startActivity(i);
            }
        }
    });

    return parentView;
}

Now - in the AsynkTask in the onPostExecute method - i notify the adapter like this:

communityMembers.addAll(newMembers);
getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
    nameOrderedAdapter.addAll(communityMembers);
    nameOrderedAdapter.notifyDataSetChanged();
    }
  });

but the listview doesn't refresh - only if I go back and reopen the activity - the new data is shown.

So how can I notify the adapter that there's new data to display?

Alin
  • 1,044
  • 6
  • 20
  • 42
  • It seems fine. Try `listView.invalidateViews();` – Rethinavel Dec 10 '13 at 12:27
  • Nope - I've tried to put listView.invalidateViews() after notifyDataSetChanged() - but the listview stays the same! – Alin Dec 10 '13 at 12:33
  • I am also getting same problem and not getting solution. Listview is not refreshing ind if i go back and reopen the activity it is getting updated. I too have tried with listView.invalidateViews(); but same as Alin not listview is not updating... – InnocentKiller Dec 10 '13 at 12:34
  • nameOrderedAdapter.addAll(communityMembers); nameOrderedAdapter.notifyDataSetChanged(); listView.invalidateViews(); – Rethinavel Dec 10 '13 at 12:34
  • Does addAll adds data to the same Collection object in the Adapter? I've had an issue when I used to create new Collection instance. – Yaroslav Mytkalyk Dec 10 '13 at 12:35
  • @DoctororDrive from what I understand addAll adds the specified Collection at the end of the array – Alin Dec 10 '13 at 12:37
  • @RethinavelPillai I've tried this - it's not working – Alin Dec 10 '13 at 12:37
  • @Alin at the end of the array or at the end of Collection? Appending at the end of array requires creation of new array. – Yaroslav Mytkalyk Dec 10 '13 at 12:40
  • @DoctororDrive this is what the documentation for addAll method says. - I've only accesed this method from the adapter - and passed the new list of objects - I guess addAll creates a new array with the old and the new data – Alin Dec 10 '13 at 12:43
  • @Alin I thought addAll is a method you created in BaseAdapter. Now I see you are using ArrayAdapter I've never used an ArrayAdapter. Try setNotifyOnChange(true) after adapter is created and do not call notifyDataSetChanged yourself. If that doesn't work try to create your own BaseAdapter implementation. – Yaroslav Mytkalyk Dec 10 '13 at 12:46
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42868/discussion-between-alin-and-doctoror-drive) – Alin Dec 10 '13 at 12:49

3 Answers3

6

call listView.invalidateViews() after notifyDataSetChanged()

Autocrab
  • 3,474
  • 1
  • 15
  • 15
  • 3
    No luck - I've tried it but the listview does not refresh with the new items! – Alin Dec 10 '13 at 12:34
  • you don't have to call runOnUiThread in method OnPostExecute, because this method is already running in UI thread. – Autocrab Dec 10 '13 at 12:36
  • if it doesn't refresh data, then check if data is changed – Autocrab Dec 10 '13 at 12:36
  • from what I can see - there are new items to show -because the members list increases in size every time the asynktask is running – Alin Dec 10 '13 at 12:44
3

add below line after nameOrderedAdapter.notifyDataSetChanged(); :

listView.setAdapter(nameOrderedAdapter);
Ahmad Vatani
  • 1,630
  • 4
  • 21
  • 34
  • I am using `RecyclerView` and this is the ONLY solution worked for me after hours of trials and search! – Yahya Mar 21 '18 at 12:45
0

use

notifyDataSetInvalidated()

method if you are using any custom ListView or Custom GridView like / HorizontalListView

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43