Setting up async task for loading Json into a listview Setting up async task for loading Json into a listview json json

Setting up async task for loading Json into a listview


The onPreExecute , onPostExecute of AsyncTask will run in the UI thread, the doInBackground will run in another thread, so below code should just fine for you

public class YourActivity extends Activiy{   public void getJson(String selection, String url) {            new LoadJsonTask().execute( selection, url);   }   private class LoadJsonTask extends AsyncTask<String, Void, ArrayList<HashMap<String, String>> > {       ProgressDialog dialog ;       protected void onPreExecute (){            dialog = ProgressDialog.show(YourActivity.this ,"title","message");       }       protected ArrayList<HashMap<String, String>> doInBackground (String... params){           return doGetJson(params[0],params[1]);       }       protected void onPostExecute(ArrayList<HashMap<String, String>> mylist){            ListAdapter adapter = new JsonAdapter(YourActivity.this, mylist, R.layout.list,              new String[] { "name", "text", "ts"}, new int[] { R.id.item_title,                R.id.item_subtitle, R.id.timestamp});            setListAdapter(adapter);            dialog.dismiss();       }    } public ArrayList<HashMap<String, String>> doGetJson(String selection, String url) {     JSONObject json = null;     String formatedcat = selection.toLowerCase();     ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();     json = JSONfunctions        .getJSONfromURL(url);     try {    //the array title that you parse    JSONArray category = json.getJSONArray(formatedcat);    for (int i = 0; i < category.length(); i++) {                       HashMap<String, String> map = new HashMap<String, String>();        JSONObject c = category.getJSONObject(i);        map.put("id", String.valueOf(i));        map.put("name",                c.getString("title"));        //map.put("text",c.getString("title"));        map.put("ts",c.getString("run_date") );        map.put("image","http:"+c.getString("url"));        mylist.add(map);    }} catch (JSONException e) {}   return mylist;  ....   }


Here's what you're looking for: progressDialog in AsyncTask check out the answer with most upvotes, should give you an idea on how to do async with progress dialog. Also, if you're totally unfamiliar with AsyncTask, check out this


Here is a simple thread that will display an indeterminate ProgressDialog while you fetch your data and then dismiss itself once the thread is finished executing.

...final ProgressDialog pd = ProgessDialog.show(this, "some title", "some message", true);Thread t = new Thread(new Runnable(){    @Override    public void run(){       //your code       ....       pd.dimiss();    } }); t.start(); ...