Android Asynctask, Prevent Ui From Freezing
Solution 1:
try following code:
privateclassLoadMoreListViewextendsAsyncTask<Void, Void, ArrayList<HashMap<String, String>>> {
@OverrideprotectedvoidonPreExecute() {
// Showing progress dialog before sending http request
pDialog = newProgressDialog(
AndroidListViewWithLoadMoreButtonActivity.this);
pDialog.setMessage("Please wait..");
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.show();
}
protected ArrayList<HashMap<String, String>> doInBackground(Void... unused) {
ArrayList<HashMap<String, String>> menuItems = newArrayList<HashMap<String, String>>();
//MOTHERLAJME MULTIDIMENSIONAlfinal List<HashMap<String, String>> MotherContainer= newArrayList<HashMap<String, String>>();
try{
XMLParserparser=newXMLParser();
Stringxml= parser.getXmlFromUrl(catUrl); // getting XMLDocumentdoc= parser.getDomElement(xml); // getting DOM element//ALLNodeListforecastW= doc.getElementsByTagName("newsitem");
for (intj=0; j < forecastW.getLength(); j++)
{
HashMap<String, String> map = newHashMap<String, String>();
Nodenodeday= forecastW.item(j);
ElementdayElmnt= (Element) nodeday;
map.put("title", (parser.getValue(dayElmnt, "title")) );
map.put("intro", (parser.getValue(dayElmnt, "intro")).toString());
map.put("story_id", ""+j );
// adding HashList to ArrayList
menuItems.add(map);
//MULTI DIMENSIONAL ARRAY
HashMap<String, String> TheStory= newHashMap<String, String>();
TheStory .put("title", (parser.getValue(dayElmnt, "title")));
TheStory .put("story_date", parser.getValue(dayElmnt, "datetime"));
MotherContainer.add(j, TheStory);
/////////////////////////
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
return menuItems;
}
protectedvoidonPostExecute(ArrayList<HashMap<String, String>> unused) {
// closing progress dialog
pDialog.dismiss();
// Adding menuItems to ListViewListAdapteradapter=newSimpleAdapter(YourActivity.this, unused,
R.layout.week_day_item,
newString[] { "title", "intro", "story_id"}, newint[] {
R.id.title_list,
R.id.intro_list,
R.id.story_id_list});
setListAdapter(adapter);
}
In AsyncTask
1st param means the type you can pass to execute. Void means you can pass nothing
The class names in Java should start with upper-calse letter. Please rename it for better readability by others.
So proper call would be
new LoadMoreListView().execute();
2nd param is a type of data you can publish calling publishProgress()
from doInBackground()
. You don't use publishProgress
, so nothing to mention in this case.
3rd param mean s type that will be passed to onPostExecute()
. To pass the menuItems to onPostExecute you must return it from doInBackground. so you need declare your class with
AsyncTask<Void, Void, ArrayList<HashMap<String, String>>>
doInBackground runs on new thread so you don't need following code:
new Runnable() {
publicvoidrun()
if you want work with UI thread you can use following method:
onPreExecute
onPostExecute
onPreExecute
usually used for showing please wait dialog or something like that and onPostExecute
used for showing data after downloading and other thing
Post a Comment for "Android Asynctask, Prevent Ui From Freezing"