android - Where do you put an AsyncTask that would load data on a ListFragment? -
android - Where do you put an AsyncTask that would load data on a ListFragment? -
so question states, set asynctask load info onto listfragment?
my mainactivity have 3 fragments. 1 listfragment contain categories. middle 1 fragment contain info retrieved selecting categories. , lastly 1 listfragment show ones user have selected.
each of 3 fragments have own xml files , own .java files. main activity cml file define 3 fragments through fragment tags. works fine when not loading info on first list fragment. when start loading info remote server through http request fails. using asynctask accomplish it.
here java file
menucategory.java
package com.thesis.menubook; import java.util.arraylist; import java.util.hashmap; import java.util.list; import org.apache.http.namevaluepair;import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.annotation.targetapi; import android.app.listfragment; import android.app.progressdialog; import android.os.asynctask; import android.os.build; import android.os.bundle; import android.util.log; import android.widget.arrayadapter; @targetapi(build.version_codes.honeycomb) public class menucategory extends listfragment { jsonparser jsonparser = new jsonparser(); arraylist<hashmap<string, string>> categorylist; private progressdialog pdialog; @override public void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); // load category onto list new getcategories().execute(); } class getcategories extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(getactivity().getapplicationcontext()); pdialog.setmessage("loading categories. please wait..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } /** * getting product details in background thread * */ protected string doinbackground(string... param) { bundle b = getactivity().getintent().getextras(); string ipaddress = b.getstring("ipaddress"); list<namevaluepair> params = new arraylist<namevaluepair>(); log.d("ip address", ipaddress +" "); jsonobject json = jsonparser.makehttprequest("http://"+ipaddress+"/menubook/selectcategories.php", "get", params); // check log cat json reponse log.d("all categories: ", json.tostring() + " "); seek { // checking success tag int success = json.getint("success"); if (success == 1) { // products found // getting array of products jsonarray category_list = json.getjsonarray("category_list"); // looping through products (int j = 0; j < category_list.length(); j++) { jsonobject c = category_list.getjsonobject(j); // storing each json item in variable string category = c.getstring("category"); // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); // adding each kid node hashmap key => value map.put("category", category); int num = 1; log.d("category #"+num+"", category + ""); num++; // adding hashlist arraylist if(categorylist.contains(map) != true) { categorylist.add(map); } } } } grab (jsonexception e) { e.printstacktrace(); } arrayadapter<arraylist<hashmap<string, string>>> arrayadapter = new arrayadapter<arraylist<hashmap<string, string>>>(getactivity(). getapplicationcontext(), r.layout.activity_menu_category); arrayadapter.add(categorylist); setlistadapter(arrayadapter); homecoming null; } /** * after completing background task dismiss progress dialog * **/ @override protected void onpostexecute(string result) { pdialog.dismiss(); } } }
am doing correctly? or should set on main activity instead?
becuase currenlty trying accessing ui elements doinbackground method of asynctask. need move ui update related code doinbackground in onpostexecute :
class getcategories extends asynctask<string, string, arraylist<hashmap<string, string>>> { string ipaddress=""; @override protected void onpreexecute() { //your code here... bundle b = getactivity().getintent().getextras(); ipaddress = b.getstring("ipaddress"); } protected arraylist<hashmap<string, string>> doinbackground(string... param) { // code here... homecoming categorylist; } @override protected void onpostexecute(string result) { // update listview here arrayadapter<arraylist<hashmap<string, string>>> arrayadapter = new arrayadapter<arraylist<hashmap<string, string>>> (getactivity(). getapplicationcontext(), r.layout.activity_menu_category); arrayadapter.add(result); your_listactivity.this.setlistadapter(arrayadapter); pdialog.dismiss(); } }
android xmlhttprequest
Comments
Post a Comment