android - problems with ProgressDialog,RunOnUiThread and ListAdapter -
ok i've written android program has 3 tabs(meals,drinks , dessert) each in own activity fragment.the problem i'm having retrieving list mysql database , displaying list in each tab.i'de ran , executed custom code displays list of items retrieved mysql database in single list,which worked fine.now bringing code program gives me 3 errors
1)
pdialog = new progressdialog(mealsfragment.this); says progressdialog(android.context,context) in progressdialog cannot applied com.example.tab.tablayout.mealsfragment
2) runonuithread(new runnable() says "cannot resolve method runonuithread "
3)is list adapter.
the initial code used oncreate,but fragment uses oncreateview.i dont know if thats issue.
here code mealsfragment.java
package com.example.tabs.tablayout; 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.app.listactivity; import android.app.progressdialog; import android.content.intent; import android.os.asynctask; import android.os.bundle; import android.support.v4.app.listfragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; import android.widget.textview; /** * created gigabyte on 5/21/2014. */ public class mealsfragment extends listfragment { // progress dialog private progressdialog pdialog; // creating json parser object jsonparser jparser = new jsonparser(); arraylist<hashmap<string, string>> mealslist; // url products list private static string url_all_meals = "http://10.180.79.73/dbase_connect/get_all_meals.php"; // json node names private static final string tag_success = "success"; private static final string tag_meals = "meals"; private static final string tag_mid = "mid"; private static final string tag_name = "name"; // products jsonarray jsonarray meals = null; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_meals, container, false); // hashmap listview mealslist = new arraylist<hashmap<string, string>>(); // loading meals in background thread new loadallmeals().execute(); // listview listview lv = getlistview(); return rootview; } /** * background async task load product making http request * */ class loadallmeals extends asynctask<string, string, string> { /** * before starting background thread show progress dialog * */ protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(mealsfragment.this); pdialog.setmessage("loading meals. please wait..."); pdialog.setindeterminate(false); pdialog.setcancelable(false); pdialog.show(); } /** * getting meals url * */ protected string doinbackground(string... args) { // building parameters list<namevaluepair> params = new arraylist<namevaluepair>(); // getting json string url jsonobject json = jparser.makehttprequest(url_all_meals, "get", params); // check log cat json response log.d("all meals: ", json.tostring()); try { // checking success tag int success = json.getint(tag_success); if (success == 1) { // meals found // getting array of meals meals = json.getjsonarray(tag_meals); // looping through meals (int = 0; < meals.length(); i++) { jsonobject c = meals.getjsonobject(i); // storing each json item in variable string id = c.getstring(tag_mid); string name = c.getstring(tag_name); // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); // adding each child node hashmap key => value map.put(tag_mid, id); map.put(tag_name, name); // adding hashlist arraylist mealslist.add(map); } } /*else { // no products found // launch add new product activity intent = new intent(getapplicationcontext(), newproductactivity.class); // closing previous activities i.addflags(intent.flag_activity_clear_top); startactivity(i); } */ } catch (jsonexception e) { e.printstacktrace(); } return null; } /** * after completing background task dismiss progress dialog * **/ protected void onpostexecute(string file_url) { // dismiss dialog after getting meals pdialog.dismiss(); // updating ui background thread runonuithread(new runnable() { public void run() { /** * updating parsed json data listview * */ listadapter adapter = new simpleadapter( mealsfragment.this, mealslist, r.layout.meals_list_item, new string[] {tag_mid, tag_name}, new int[] { r.id.mid, r.id.name }); // updating listview setlistadapter(adapter); } }); } } }
1) first parameter of progressdialog constructor must context. use
new progressdialog(getactivity());
2) use getactivity().runonuithread(...)
. however, that's not necessary in context. asynctask.onpostexecute()
method running in ui thread. move runnable code body of onpostexecute()
method.
3) same 1, first parameter must context. use new simpleadapter(getactivity(), ...)
.
Comments
Post a Comment