I have been learning about Android Loaders and been trying to implement one with AsyncTaskLoader.
I have been very successful, thus making this even more annoying :P
My onLoadFinished function is as follows:
@Override
public void onLoadFinished(Loader<JSONArray> arg0, JSONArray data) {
// TODO Auto-generated method stub
setProgressBarIndeterminateVisibility(false);
mLoadedData = data;
//Log.d("here","dosomething");
//Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_LONG).show();
//Log.d("json",mLoadedData.toString());
Fragment f = getSupportFragmentManager().findFragmentByTag("pub_list");
((BP_Fragment) f).set_result(mLoadedData);
}
The set_result function in my fragment is as follows:
public void set_result(JSONArray data) {
// TODO Auto-generated method stub
//Toast.makeText(getSherlockActivity(), "set result is called..", Toast.LENGTH_SHORT).show();
Log.d("pubs",data.toString());
for (int i = 0; i <data.length(); i++){
Log.d("string","here");
JSONObject c;
try {
c = data.getJSONObject(i);
String id = c.getString("pid");
String name = c.getString("name");
String town = c.getString("town");
String county = c.getString("county");
//Double score=c.getDouble(criteria);
//Log.d("score",score);
//HashMap<String, String> map = new HashMap<String, String>();
pubs.add(new PubListDetails(id,name,town,county,1.0,"pub"));
//map.put(TAG_PID, id);
//map.put(TAG_NAME, name);
//productsList.add(map);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
PubListAdapter adapter = new PubListAdapter(getSherlockActivity(), pubs);
// Toast.makeText(getSherlockActivity(), "This is called", Toast.LENGTH_SHORT).show();
setListAdapter(adapter);
}
Ive used the LoaderManager debugger to check the loader is being retained.. it is. I have used the console Log to output my data from within the set_result function - the data is there each orientation change.. I.E everything is being called, and everything is being passed as suggested YET set_adapter only sets the data to the list the first time, no when it is called as a result of a retained loader.
Why? :P I am flummoxed.
THanks
So I resolved this.
In my loader Activity I was creating a fragment within onCreate in which was contained my list and my set_result function.
Although the loader was being retained successfully when I was recreating my activity on orientation change it was creating another fragment with the same tag instead of using the retained one.
Thus when I called my set_result function, I assume it was finding my initial fragment and setting the list on that.
I am not sure 100% how fragments work when you create two with the same tag - I can only assume that the recreated version was placed on top of my initial one thus hiding it.