I’ve been bashing my head against a wall on this for about a day now. I hope someone can help.
My program loads and parses some Json in to an array of objects in an async task. This async task is called from my ListFragment class and the data is sent back in the onPostExecute. No errors are thrown and the list loading animation disappears as it should. But no matter what I do the list is never populated.
I’m definitely getting data returned and parsed correctly and I’ve tried using a simple array and adapter in the onPostExecute to make sure it’s not my adapter class and it still doesn’t work.
I’ve simplified my code below to just an example. The code below waits for 1 second then removes the loading animation. But doesn’t populate the list. If you remove the sleep from the doInBackground method it works perfectly.
public class MyFragment extends ListFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
AsyncTask<Void, Void, String> task = new AsyncTask<Void,Void,String>()
{
@Override
protected String doInBackground(Void... params) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, values);
setListAdapter(adapter);
}
};
task.execute();
}
}
As I say if you remove the Thread.sleep(1000) which i’m using to simulate the json loading it works fine.
I’m using the fragment support library for 2.3 if that makes any difference.
Thanks
Steve
Try
MyFragment.this.setListAdapter(adapter);
EDIT:
Hi,
I have tried your code, even loaded the view by
It works fine for me, The only possible cause of the trouble should be that: the View is not visible while loading the fragment, or some similar issue.
You might want to check the layouts where you intent to load the fragment.