I have a private subclass in my public class. This private class extends AsyncTask. In onPostExecute() , i have to send my List of objects through custom adapter. But the problem is when i get the "context" of main public class(parent of private subclass), it gives an error on @Override (OnPostExecute()).
public class MainListActivity extends ListActivity {
protected String[] mBlogPost;
public static final int NUMBER_OF_POST = 20;
public static final String TAG= MainListActivity.class.getSimpleName();
public void loadPage() {
Log.d(TAG,"loadpage me a gae nh");
// if((sPref.equals(ANY)) && (wifiConnected || mobileConnected)) {
new DownloadXmlTask().execute(URL);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
loadPage();
}
private class DownloadXmlTask extends AsyncTask<String, Void, Object> {
@Override
protected Object doInBackground(String... urls) {
try {
return loadXmlFromNetwork(urls[0]);
} catch (IOException e) {
return "I/O exception ae hy";
} catch (XmlPullParserException e) {
return "XML pull parser ke exception ae hy";
}
}
@Override
protected void onPostExecute(List<StackOverflowXmlParser.Entry> results) {
String title,link,summary;
ArrayList<HashMap<String,String>> blogPosts = new ArrayList<HashMap<String,String>>();
for(StackOverflowXmlParser.Entry result : results){
title = result.title;
link = result.link;
summary = result.summary;
HashMap<String,String> blogPost = new HashMap<String,String>();
blogPost.put("link",link);
blogPost.put("title",title);
//blogPost.put("summary",summary);
blogPosts.add(blogPost);
}
String[] keys = {"title","link"};
int[] ids = {android.R.id.text1,android.R.id.text2};
SimpleAdapter adapter =
new SimpleAdapter(MainListActivity.this,blogPosts,android.R.layout.simple_list_item_2,keys,ids);
setListAdapter(adapter);
}
It is giving an error on @Override. I have to get the context of parent class of private subclass(this extends asyncTask and have onPostExecute())
I also used getApplicationContext(). Moreover I also made a new public class for context and getting context from there.
private Object loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException {
InputStream stream = null;
// Instantiate the parser
StackOverflowXmlParser stackOverflowXmlParser = new StackOverflowXmlParser();
List<StackOverflowXmlParser.Entry> entries = null;
String title = null;
String url = null;
String summary = null;
Calendar rightNow = Calendar.getInstance();
DateFormat formatter = new SimpleDateFormat("MMM dd h:mmaa");
StringBuilder htmlString = new StringBuilder();
try {
stream = downloadUrl(urlString);
entries = stackOverflowXmlParser.parse(stream);
// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (stream != null) {
stream.close();
}
}
for (StackOverflowXmlParser.Entry entry : entries) {
htmlString.append("<p><a href='");
htmlString.append(entry.link);
htmlString.append("'>" + entry.title + "</a></p>");
htmlString.append(entry.summary);
}
for (StackOverflowXmlParser.Entry entry : entries)
{
Log.d(TAG, entry.link + " /" + entry.title);
}
//return htmlString.toString();
return entries;
}