0

I am trying to display a list fragment in the middle of my activity. The list fragment adapter is a custom adapter (extended from BaseAdapter) with the typical ViewHolder pattern. It is implemented correctly.

I have the adapter set up with greenrobot Eventbus to receive a new List object from an asynctask which does the query in the background (as not to slow down the UI Main Thread).

The problem is the list fragment doesn't have the results of the database query initially so it defaults to empty (and displays the textview in my xml for the main activity which has the id 'empty').

In the end, my adapter, and listviewfragment don't get instaniated at all because it defaults to empty.

Is there a better method to doing this? How can I get my listview fragment to wait for the data recieved from the asyncTask?

I am going to just do a fragment with a listview in it, instead of a listview fragment, and I'll see if that will help.

AnnonAshera
  • 399
  • 1
  • 4
  • 12

1 Answers1

0

As I understood correctly, you did everything okay so far. What you have to do is to add an "update" method to your adapter, and call this whenever your asynctask finished it's job and got the results for you. This update method should look something like

public void updateAdapterData(List<String> newList) {
    mItems.clear(); // say mItems is the global list you have in the adpater
    mItems.addAll(newList);

    notifyDataSetChanged(); // This will cause the adpater to re-draw it's rows, so now data should be visible in your list
}

And in your fragment you do somehting like:

 adpater.updateAdapterData(newList);

Does this help?

Mike
  • 4,550
  • 4
  • 33
  • 47
  • Thanks mike. I have this already, where it gets onEvent method from the async task and loads the List into the adapter. The problem is that the fragment itself is not being created, or the adapter. Since the data is initially empty the ListFragment default behavior when there is no data is to show the view resource with the id empty. – AnnonAshera Oct 17 '14 at 19:33
  • Which I don't really understand how it can do that since it isn't being created in the first place (my logs aren't going off in onattach or oncreateview for instance so I can tell). I'm looking into the ListFragment source trying to figure it out. – AnnonAshera Oct 17 '14 at 19:35
  • If you're still stuck, please provide some code so I can help you out! – Mike Oct 17 '14 at 19:35
  • Pasted in the code. Apologies there is a lot of it. I tried to remove anything irrelevant to the problem. – AnnonAshera Oct 17 '14 at 20:15
  • I'm also a bit confused that it says to add fragments in xml to activites with the Fragment tag and the android:name attribute. However, for the ListFragment in the documentation it says to have a listview in your xml called with android list as the id. Which one is it? Any I missing something obvious here? – AnnonAshera Oct 17 '14 at 20:18
  • Adding fragment directly from xml should be done only when you're pretty much sure you won't play around too much with fragments. IF you know, you need a static fragment on an activity, you may add it directly from xml. By the way for having a listview in a fragment, you don't necessarily need to use ListFragment. you may as well use a simple Framgent and have a ListView in it. It might be more simple for you. – Mike Oct 17 '14 at 20:21
  • Good idea. I'll try that, although perhaps I should just switch to adding them programmatically as I will probobly have a horizontal layout that is different. Thanks for help, I think I'll be fine. – AnnonAshera Oct 17 '14 at 21:55
  • Yeah I'll just do a regular fragment and have a listview in it. That makes more sense. – AnnonAshera Oct 17 '14 at 21:55
  • And also, since you need that data only in the fragment, start your AsyncTask in your fragment instead of the activity. This way, you can even have your asyncTask as an inner class in your fragment so you can easily reach your ListView on the onPostExecute method. – Mike Oct 17 '14 at 21:56