简体   繁体   中英

What is wrong with progress dialog in AsyncTask

Am using Async Task in my application to get response from web service using restful web service. My code

    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_json_page);
    _mContext = this;

    new JSONParserTask().execute();

}

asynctask class

    private class JSONParserTask extends AsyncTask<Void, Void, ListAdapter >{


    ProgressDialog dialog;


    @Override
    protected void onPreExecute() {

    //  dialog = new ProgressDialog(_mContext); 
    //  dialog.setMessage("Loading...");
    //  dialog.show();
        super.onPreExecute();

    }


    @Override
    protected ListAdapter doInBackground(Void... arg0) {
        ListAdapter adapter = null;
        itemsList = new ArrayList<HashMap<String, String>>();

            jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(Constants.JsonURL);

          if(json == null){

              Log.v(TAG, "----------- null");
              return null;
          }


            try {

                // Getting Array of Items
                items = json.getJSONArray(TAG_ITEMS);       
                 // looping through All items
                for(int i = 0; i < items.length(); i++) {
                    JSONObject itemsObj = items.getJSONObject(i);
                    JSONObject products = null;
                    products = itemsObj.getJSONObject(TAG_PRODUCT);
                    Log.d(TAG,"product array "+products.toString());

                    JSONArray images = products.getJSONArray(TAG_IMAGES);
                    JSONObject imagesObj = images.getJSONObject(0);
                    Log.d(TAG, "......."+ imagesObj.getString(TAG_LINK));

                    String imageUrl = imagesObj.getString(TAG_LINK);


                    // Storing each json item in variable
                    String kind = itemsObj.getString(TAG_KIND);
                    String id = itemsObj.getString(TAG_KID);
                            String selfLink = itemsObj.getString(TAG_SELFLINK);

                            HashMap<String, String> map = new HashMap<String, String>();

                         // adding each child node to HashMap key => value
                        map.put(TAG_KIND, kind);
                        map.put(TAG_KID, id);
                        map.put(TAG_SELFLINK, selfLink);

                            // adding HashList to ArrayList
                                itemsList.add(map);

                }
              /**
                 * Updating parsed JSON data into ListView
                 * */
                 adapter = new SimpleAdapter(_mContext, itemsList,
                        R.layout.list_item_row,
                        new String[] { TAG_KIND,  TAG_SELFLINK }, new int[] {
                                R.id.name, R.id.mobile });



            } catch(JSONException e){
                e.printStackTrace();
            }       
            return adapter;
    }

    @Override
    protected void onPostExecute(ListAdapter adapter) {


            lv.setAdapter(adapter);    

            // Launching new screen on Selecting Single ListItem
            lv.setOnItemClickListener(new OnItemClickListener() {

               public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // some action
                }
            });

        //dialog.dismiss();
    }      
   }

with this code every thing working fine without using progress dialog. If u found, the code related to progress dialog is commented in above class. If i uncomment progress dialog code, am not getting any response from server. I have tried with debugging also but never get any idea to remove this error. Can some one tell what wrong am doing here.

ok the reason for that is you are updating you are adapter in your doInBackground() method

        adapter = new SimpleAdapter(_mContext, itemsList,
                    R.layout.list_item_row,
                    new String[] { TAG_KIND,  TAG_SELFLINK }, new int[] {
                            R.id.name, R.id.mobile });

This code is related to the MAIN THREAD and shouldn't be called here in the background thread, remove it from here, and add it to the onPostExecute() , just pass an array list from the Background thread and do other UI related stuff in the onPostExecute()

Try this

ProgressDialog mProgressDialog;

@Override
        protected void onPreExecute() {
            mProgressDialog= ProgressDialog.show(getApplicationContext(),"", getString(R.string.dialog_wait_message));
            super.onPreExecute();
        }




   protected void onPostExecute(Void result) {
                if(mProgressDialog!=null){
                    mProgressDialog.dismiss();
           }
}

Try this one, rather

    ProgressDialog mProgressDialog;

    @Override
    protected void onPreExecute() {
        mProgressDialog= ProgressDialog.show(getApplicationContext(),"", getString(R.string.dialog_wait_message));
        super.onPreExecute();
    }

    @Override
    protected ListAdapter doInBackground(Void... arg0){
      //do your stuff here
    }

    @Override
    protected void onPostExecute(Void result) {
            if(mProgressDialog!=null && mProgressDialog.isShowing()){
                mProgressDialog.dismiss();
       }
}

Try this

          @Override
    protected void onPreExecute() {
        pd=new ProgressDialog(m_context);
        pd.setTitle("Authenticating");
        pd.show();

    }
  @Override
    protected Void doInBackground(Void... args) {
            //your stuff
           }
        @Override
    protected void onPostExecute(Void result) {

        pd.dismiss();
           }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM