Populating ListView inside Fragment class extending ListFragment - android

I have a fragment class extending ListFragment and I'm having trouble populating the list.
I run an asynctask to retreive and parse data from a webservice which is all working ok but now I'm stuck on where I should set the list adapter to populate my list.
I am currently executing my asynctask in the onCreate() method and trying to populate the list in onActivity created which gives a NullPointerException. If i remove the setListAdapter everything runs fine.
According to the android developer docs, a fragment extending ListFragment returns a ListView from onCreateView() by default so I should have access to it.
Where is the correct place to set my list adapter to populate the list?
Thanks for your help.

You are going to want to set the list adapter in the onPostExecute() in the AsyncTask. You can pass a reference to the ListFragment into the constructor for AsyncTask and call setListAdapter() on that reference.

Related

ListView in the Activity and getting data from server in listview and from adapter refresh the listview by calling same url

I have a listview in Activity and getting data from server to generate the listview and now from adapter i have to call the same url to refresh the listview. No data are storing in device. But unable to do it from adapter.how can i do it?
What's your problem ? Can you tell it more in detail? If you declare the adapter as the inner class of the activity, simply add a method in the activity and call it. Otherwise add a context or activity parameter to the constructor of the adapter and use it. Use handler or AsyncTask or other way to load the data, after getting data from server , set data to the adapter and call adapter.notifyDataSetChanged to refresh data.Hope this help.:)

Refresh Listadapter with custom listview in Listfragment android

i used list adapter for setting values to listview.The values are loaded from webservice api.I need how to refresh the customized listview with list adapter in android.
There are two ways to do that:
Create a new adapter and set it to the ListView with setAdapter every time you reload your data.
Reuse the adapter (via setDataor similar methods) and invoking notifyDataSetChanged after that, so the view is refreshed.
You need to use notifyDataSetChanged() from the adapter, in order to do that your adapter needs extend BaseAdapter, so every time you change some data call the ,
instanceOfAdaper.notifyDataSetChanged()
I guess you must be using AsyncTask method to fetch data from WebService,Just override this method,and call notifyDataSetChanged method in onPostExecute like this.
protected void onPostExecute(String result) {
adapter.notifyDataSetChanged();
}

updating a nested adapter does not update UI

I have a Fragment that contains a ListView that uses a custom adapter that extends ArrayAdapter. Each item in this list view, contains another ListView that also extends ArrayAdapter. Think of it as a list of news stories and each news story having multiple images.
I now want to add a new image to a news story. What I do is:
ImageAdapter imageAdapter =
newsAdapter.getImageAdapterForNewsItem(id);
imageAdapter.add(imageId);
imageAdapter.notifyDataSetChanged();
This all happens in SelectImagesFragment, where as the news items are listed in the NewsItemsFragment. However, the SelectImagesFragment contains a reference to the NewsItemsFragment. The above code executes in an AsyncTask, and before it executes, I move the user back to the NewsItemsFragment.
I was expecting the news list to get updated, once the AsyncTask completes and the UI to get refreshed. However, this is not happening. Since the ImageAdapter is inside the NewsAdapter, do I need to somehow tell the NewsAdapter that there has been a data change as well? If so, how do I do this? Calling 'notifyDataSetChanged()' won't work, since I didn't modify the underlying list data in NewsAdapter using add().

ListFragment and asynctask issues android

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.
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?

Android async class with setListAdapter

I have an Activity Class with an inner protected class that extends AsyncTask. I am trying to have the AsyncTask load data in background when I click a button and then display a list based on that data.
here is my code for the setListAdapter:
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_row,
R.id.rowtextview, testArr.toArray(test2)));
the problem is that I can't just stick setListAdapter into the AsyncTask class because it needs a context for the outer class, and the only place it can go is inside the onCreate() in the outer Activity class. If its inside the onCreate, it can only be used once and isnt dynamic. I am not sure how I would go about making the list re-load every time I click a button and search for something new.
Your custom inner AsyncTask is not marked as static right? That means that it actually holds reference to the parent class, which is you Activity. So you can use:
setListAdapter(new ArrayAdapter<String>(YourActivity.this, R.layout.list_row,
R.id.rowtextview, testArr.toArray(test2)));
Easy, make the adapter an instance variable and extend it (trivial) to add your custom updateData(newData) method. In this method you update your data AND call notifyDatasetChanged(). And you're done! That way you only need to set the adapter once and just call updateData() when the AsyncTask comes back.

Categories

Resources