I read a lot about AsyncTask while trying to make it work with my ListFragment. Now I came across a few articles saying it must be static for technical reasons. Other articles say it doesn't matter as for my case it I couldn't make the right syntax for static so I made my class non-static. If it is recommended to make it static please helping me changing it.
my class :
private class MyAsyncTask extends AsyncTask<List<String>, Void, List<String>>
{
// List of messages of the rss feed
private List<Message> messages;
private WeakReference<NieuwsSectionFragment> fragmentWeakRef;
private MyAsyncTask(NieuwsSectionFragment fragment)
{
this.fragmentWeakRef = new WeakReference<NieuwsSectionFragment>(fragment);
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
// progress.setVisibility(View.VISIBLE); //<< set here
}
@Override
protected List<String> doInBackground(List<String>... urls)
{
FeedParser parser = FeedParserFactory.getParser();
messages = parser.parse();
List<String> titles = new ArrayList<String>(messages.size());
for (Message msg : messages)
{
titles.add(msg.getTitle());
// Log.w("doInBackground", msg.getTitle());
}
return titles;
}
@Override
protected void onPostExecute(List<String> result)
{
super.onPostExecute(result);
if (result != null)
{
PostData data = null;
listData = new PostData[result.size()];
for (int i = 0; i < result.size(); i++)
{
data = new PostData();
data.postTitle = result.get(i);
listData[i] = data;
Log.w("onPostExecute", "" + listData[i].postTitle);
}
Log.w("onPostExecute", "" + adapter);
adapter = new PostItemAdapter (getActivity(), android.R.layout.simple_list_item_1, listData);
setListAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
}
When I make it static it gives errors on the adapter. It says something about setListAdapter and getActivity().