在Android中的asynctask中的doinbackground之后,ProgressDialog仍保持打开状态

时间:2014-05-05 08:05:11

标签: android listview android-asynctask progressdialog

我做了一个活动,因为我使用自定义适配器将数据从webservice绑定到ListView,我想在我点击回复按钮时刷新该数据" doReply" api将被调用。所有进展顺利,但我的progressDialog在ListView加载后仍保持打开状态,任何人都可以告诉我如何刷新列表视图..我的代码如下: 的 main.java

public class ChatHistoryActivity extends Activity {
    private ProgressDialog pDialog;
    JSONArray msgArry;
    JSONObject jsonObj;
    private ChatAdapter chatContent;
    ArrayList<HashMap<String, String>> msgList;
    ListView lv;
    JSONArray msgs = null;
    String pro_id, pro_name, pro_img, grup_id, sender_id, cust_id;
    TextView tv_switch;
    public boolean flag = false;
    Header header;
    Menu menu;
    Intent in;
    Button reply;
    RelativeLayout rl_reply;
    EditText et_reply;
    String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_chat_history);
        lv = (ListView) findViewById(R.id.list);
        tv_switch = (TextView) findViewById(R.id.tv_switch);
        header = (Header) findViewById(R.id.header_msg);
        menu = (Menu) findViewById(R.id.menu_msg);
        reply = (Button) findViewById(R.id.btn_reply);
        rl_reply = (RelativeLayout) findViewById(R.id.rl_reply);
        rl_reply.setVisibility(View.GONE);
        et_reply = (EditText) findViewById(R.id.et_reply);
        menu.setSelectedTab(3);
        header.title.setText("Conversation");
        msgList = new ArrayList<HashMap<String, String>>();

        pro_id = getIntent().getStringExtra(Const.TAG_PRODUCT_ID);
        sender_id = getIntent().getStringExtra(Const.TAG_CUSTOMER_ID);
        grup_id = getIntent().getStringExtra(Const.TAG_GROUP_ID);
        cust_id = Pref.getValue(ChatHistoryActivity.this, Const.PREF_CUSTOMER_ID, "");

        new GetChatHistory().execute();

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // getting values from selected ListItem
                rl_reply.setVisibility(View.VISIBLE);

            }

        });
        // message reply ...!!Chat api(conversation)
        reply.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                new DoReply().execute();
                // new GetChatHistory().execute();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        new GetChatHistory().execute();
    }

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

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ChatHistoryActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            BackendAPIService sh = new BackendAPIService();
            String query = Const.API_CHAT_HISTORY;
            url = "?customer_id=" + cust_id + "&group_id=" + grup_id + "&sender_id=" + sender_id + "&product_id=" + pro_id;
            url = url.replace(" ", "%20");
            url = query + url;
            System.out.println(":::::::::::::My MESSGES URL::::::::::::::" + url);

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, BackendAPIService.GET);

            Log.d("Response: ", "> " + jsonStr);
            try {
                if (jsonStr != null) {
                    msgArry = new JSONArray(jsonStr);
                    if (msgArry != null && msgArry.length() != 0) {
                        // looping through All Contacts

                        System.out.println(":::::::::::FLAG IN SUB:::::::::::" + msgArry.length());
                        for (int i = 0; i < msgArry.length(); i++) {
                            JSONObject c = msgArry.getJSONObject(i);

                            String custID = c.getString(Const.TAG_CUSTOMER_ID);
                            String custName = c.getString(Const.TAG_CUSTOMER_NAME);
                            String proID = c.getString(Const.TAG_PRODUCT_ID);
                            String email = c.getString(Const.TAG_CUSTOMER_EMAIL);
                            String photo = Const.API_HOST + "/" + c.getString(Const.TAG_PHOTO);
                            String msg_body = c.getString(Const.TAG_MESSAGE_BODY);
                            HashMap<String, String> message = new HashMap<String, String>();

                            message.put(Const.TAG_CUSTOMER_ID, custID);
                            message.put(Const.TAG_CUSTOMER_NAME, custName);
                            message.put(Const.TAG_PRODUCT_ID, proID);
                            message.put(Const.TAG_CUSTOMER_EMAIL, email);
                            message.put(Const.TAG_PHOTO, photo);
                            message.put(Const.TAG_MESSAGE_BODY, msg_body);

                            msgList.add(message);

                        }
                    } else {
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Utils.showCustomeAlertValidation(ChatHistoryActivity.this, "No messgaes found", "yehki", "Ok");
                                msgList.clear();
                            }
                        });

                    }

                } else {
                    Log.e("ServiceHandler", "Couldn't get any data from the url");
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();

            chatContent = new ChatAdapter(ChatHistoryActivity.this, msgList);
            chatContent.notifyDataSetChanged();
            lv.setAdapter(chatContent);

        }
    }

    /*
     * GET CONVERSATION LIST.........REPLY
     */
    private class DoReply extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(ChatHistoryActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            BackendAPIService sh = new BackendAPIService();
            String query = Const.API_MESSAGE_REPLY;
            url = "?customer_id=" + cust_id + "&group_id=" + grup_id + "&receiver_id=" + sender_id + "&product_id=" + pro_id + "&message=" + et_reply.getText().toString().trim();
            url = url.replace(" ", "%20");
            url = query + url;
            System.out.println(":::::::::::::My MESSGES URL::::::::::::::" + url);

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, BackendAPIService.GET);

            Log.d("Response: ", "> " + jsonStr);
            try {
                if (jsonStr != null) {
                    jsonObj = new JSONObject(jsonStr);
                    if (jsonObj.getString("status").equals("sucess")) {
                        et_reply.setText("");
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Toast.makeText(ChatHistoryActivity.this, "Message has been sent", Toast.LENGTH_SHORT).show();
                            }
                        });
                    } else {
                        et_reply.setText("");
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Toast.makeText(ChatHistoryActivity.this, "Message has not been sent", Toast.LENGTH_SHORT).show();
                            }
                        });
                    }

                } else {
                    Log.e("ServiceHandler", "Couldn't get any data from the url");
                }
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();

            chatContent = new ChatAdapter(ChatHistoryActivity.this, msgList);
            chatContent.notifyDataSetChanged();
            lv.setAdapter(chatContent);

        }
    }

}

1 个答案:

答案 0 :(得分:0)

您的onCreate()onResume()都会启动设置进度对话框的异步任务,并覆盖活动中的pDialog变量。被解雇的对话框不一定是正在显示的对话框。

  • 仅启动异步任务一次。

  • 将进度对话框引用存储为异步任务对象中的成员而不是活动,因此不会被其他人覆盖。