如果我在列表视图中滚动得更快,进度对话框不会关闭

Progress dialog does not dismiss if i scroll faster in my listview

如果我滚动得更快,我的 progress dialog 不会关闭并停留在屏幕上以获得无限 time.Any 帮助?尝试了很多东西,但它仍然存在。有什么办法可以消除它。我猜 dismis() 函数没有在 onPostExecute() 中调用。

代码

package com.company.napp;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.widget.AbsListView;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;


public class MainActivity extends Activity {

    // Declare Variables
    ArrayList<HashMap<String, String>> arraylist1;
    int page=1;
    JSONObject jsonobject;
    JSONArray jsonarray;
    ListView listview;
    ListViewAdapter adapter;
    ProgressDialog mProgressDialog;
    ArrayList<HashMap<String, String>> arraylist;
    static String TITLE = "title";
    static String ADDRESS = "address";
    static String COUNTRY = "country";
    static String IMAGE = "image";
    static String SERVICETYPE="serviceType";
    static String PHNUM="phnum";
    static String DESCRIPTION="description";


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_main);
        // Execute DownloadJSON AsyncTask
        new DownloadJSON().execute();

        listview = (ListView) findViewById(R.id.listview);

        listview.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub
                int threshold = 1;

                int count = listview.getCount();
                System.out.println("\nLast visible = "+listview.getLastVisiblePosition()+"\nCount = "+count);
                if (scrollState == SCROLL_STATE_IDLE) {
                    if (listview.getLastVisiblePosition()+1 > count-1) {
                        // Execute LoadMoreDataTask AsyncTask

                        new LoadMoreDataTask().execute();

                    }
                }

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                // TODO Auto-generated method stub

            }

        });

    }


    //DownloadJSON AsyncTask

    private class DownloadJSON extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a progressdialog
            mProgressDialog = new ProgressDialog(MainActivity.this);
            // Set progressdialog title
            mProgressDialog.setTitle("Napp");
            // Set progressdialog message
            mProgressDialog.setMessage("Loading...");
            mProgressDialog.setIndeterminate(false);
            // Show progressdialog
            mProgressDialog.show();
            mProgressDialog.setCanceledOnTouchOutside(false);
            //mProgressDialog.getWindow().setGravity(Gravity.BOTTOM);
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonobject = JSONfunctions.getJSONfromURL("http://54.169.88.65/events/TA/JsonTA.php?page=1");

            try {
                // Locate the array name in JSON
                jsonarray = jsonobject.getJSONArray("Contacts");

                for (int i = 0; i < jsonarray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    jsonobject = jsonarray.getJSONObject(i);
                    // Retrive JSON Objects
                    String t, a, d, s;
                    t = jsonobject.getString("title");
                    t = t.replaceAll("\n", "");
                    //t="\n"+t;
                    a = jsonobject.getString("address");
                    a = a.replaceAll("\n", "");
                    d = jsonobject.getString("description");
                    d = d.replaceAll("\n", "");
                    s = jsonobject.getString("serviceType");
                    s = s.replaceAll("\n", "");
                    //s=s+"\n";

                    map.put("title", t);
                    map.put("address", a);
                    map.put("country", jsonobject.getString("country"));
                    map.put("image", jsonobject.getString("image"));
                    map.put("serviceType", s);
                    map.put("phnum", jsonobject.getString("phnum"));
                    map.put("description", d);
                    // Set the JSON Objects into the array
                    arraylist.add(map);
                }
            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return null;

        }

        @Override
        protected void onPostExecute(Void args) {
            // Locate the listview in listview_main.xml
            listview = (ListView) findViewById(R.id.listview);
            // Pass the results into ListViewAdapter.java
            adapter = new ListViewAdapter(MainActivity.this, arraylist);
            // Set the adapter to the ListView
            listview.setAdapter(adapter);
            // Close the progressdialog
            mProgressDialog.dismiss();


        }
    }
        private class LoadMoreDataTask extends AsyncTask <Void, Void, Void> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                // Create a progressdialog
                mProgressDialog = new ProgressDialog(MainActivity.this);
                // Set progressdialog title
                //mProgressDialog.setTitle("Napp");
                // Set progressdialog message
                mProgressDialog.setMessage("Loading...");
                mProgressDialog.setIndeterminate(false);
                //Show progressdialog
               mProgressDialog.show();
                mProgressDialog.setCanceledOnTouchOutside(false);
                //mProgressDialog.getWindow().setGravity(Gravity.BOTTOM);
            }

            @Override
            protected Void doInBackground(Void... params) {
                // Create an array

                page+=1;
                //arraylist = new ArrayList<HashMap<String, String>>();
                // Retrieve JSON Objects from the given URL address
                jsonobject = JSONfunctions.getJSONfromURL("http://54.169.88.65/events/TA/JsonTA.php?page="+page);

                try {
                    // Locate the array name in JSON
                    jsonarray = jsonobject.getJSONArray("Contacts");
                    arraylist1 = new ArrayList<HashMap<String, String>>();
                    for (int i = 0; i < jsonarray.length(); i++) {
                        HashMap<String, String> map = new HashMap<String, String>();
                        jsonobject = jsonarray.getJSONObject(i);
                        // Retrive JSON Objects
                        String t,a,d,s;
                        t=jsonobject.getString("title");
                        t=t.replaceAll("\n", "");
                        //t="\n"+t;
                        a=jsonobject.getString("address");
                        a=a.replaceAll("\n","");
                        d=jsonobject.getString("description");
                        d=d.replaceAll("\n","");
                        s=jsonobject.getString("serviceType");
                        s=s.replaceAll("\n","");
                        //s=s+"\n";

                        map.put("title",t);
                        map.put("address",a);
                        map.put("country", jsonobject.getString("country"));
                        map.put("image", jsonobject.getString("image"));
                        map.put("serviceType",s);
                        map.put("phnum",jsonobject.getString("phnum"));
                        map.put("description",d);
                        // Set the JSON Objects into the array
                        arraylist1.add(map);

                    }
                } catch (JSONException e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
                return null;

            }

            @Override
            protected void onPostExecute(Void args) {

                // Set the JSON Objects into the array
                arraylist.addAll(arraylist1);
                //locate listview last item
                int position =listview.getLastVisiblePosition();
                adapter = new ListViewAdapter(MainActivity.this, arraylist);


                // Set the adapter to the ListView
                listview.setAdapter(adapter);

                //show the lastest retrieved results on the top
                listview.setSelectionFromTop(position,0);


                adapter.notifyDataSetChanged();


                // Close the progressdialog
                mProgressDialog.dismiss();



            }





        }




    }

看起来在 Asynctask 的第一次调用完成之前,您正在再次调用它。因此,最好的解决方案是不要在每次创建任务时都创建进度对话框,而是检查对话框是否已经 运行 并进行相应处理。创建了多个进度对话框实例,但都没有被销毁