简体   繁体   English

在AsyncTask Android应用的PostExecute方法中显示Toast消息

[英]Displaying Toast message in PostExecute method of an AsyncTask Android App

Ok, so I have an AsyncTask that is adding items to a ListView . 好的,所以我有一个AsyncTask ,它将项目添加到ListView On PreExecute, I am displaying a message "Starting" and on PostExecute I am displaying "Done". 在PreExecute上,我显示一条消息“正在启动”,在PostExecute上,我显示一条“完成”。 The "Starting" message is displayed but for some reason, when the listing is done, the "Done" message is NOT displayed. 显示“正在启动”消息,但是由于某种原因,列出完成后,不显示“完成”消息。 Does anyone have any ideas on why the message is not displaying in the PostExecute section of this Task? 是否有人对为什么此任务的PostExecute部分中未显示消息有任何想法? Thank you 谢谢

Code: 码:

class FillLocations extends AsyncTask<Integer, Void, Void> {

    public FillLocations() {

    }

    protected void onPreExecute() {
        Toast.makeText(getApplicationContext(), "Starting",
                Toast.LENGTH_LONG).show();
    }

    // Decode image in background.
    @Override
    protected Void doInBackground(Integer... params) {

        String result = "";
        InputStream isr = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://myscript/"); // YOUR
                                                                                // PHP
                                                                                // SCRIPT
                                                                                // ADDRESS
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            isr = entity.getContent();
            // resultView.setText("connected");
        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }
        // convert response to string
        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(isr, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            isr.close();

            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error  converting result " + e.toString());
        }

        // parse json data
        try {
            JSONArray jArray = new JSONArray(result);

            for (int i = 0; i < jArray.length(); i++) {
                final JSONObject json = jArray.getJSONObject(i);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        try {

                            BitmapWorkerTask myTask = new BitmapWorkerTask(
                                    json.getInt("ID"), json);
                            myTask.execute();

                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }
                });

            }

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("log_tag", "Error Parsing Data " + e.toString());
        }
        return null;

    }

    protected void onPostExecute() {
        Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_LONG)
                .show();

    }
}

You need to appropriately implement onPostExecute 您需要适当地实现onPostExecute

@Override
protected void onPostExecute(Void void){
      // Do what you want to do, for example, your toast:
      Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_LONG)
            .show();
}

-Notes on General Practice-

Your AsyncTask doesn't need a constructor, so you can remove 您的AsyncTask不需要构造函数,因此您可以删除

public FillLocations(){
}

And you should declare your class visibility (unless you want it set to the default visibility which is the package), so make it something like 并且您应该声明您的类可见性(除非您希望将其设置为默认的可见性即包),因此应使其类似于

private class FillLocations extends AsyncTask<Integer, Void, Void>{
    // Your Stuff
}

onPostExecute takes a parameter as shown here . onPostExecute需要一个参数如图所示这里

Personally, I would do something like this, to tell the user that something went wrong, in case some exception is thrown. 就个人而言,我会做这样的事情,以告诉用户出了点问题,以防万一引发异常。 I never liked letting errors go to nirvana. 我从来不喜欢让错误成为必杀技。

protected String doInBackground(Integer... params) {
...
}

and

protected void onPostExecute(String msg) {
    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG)
            .show();

}

(Also, in the documentation, no @Override is used.) (此外,在文档中,不使用@Override 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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