0

我似乎无法弄清楚这一点。我是 android 新手,我试图在从我的网站查询数据库时显示一个对话框。除了在 doInBackground() 完成之前 ProgressDialog 不显示这一事实之外,代码正常运行,这不应该是这种情况。

这是代码

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

    private ProgressDialog mProgressDialog;
    private Context context;
    private String taskResult;

    public MyAsyncTask(Activity activity) {
        context = activity;
        mProgressDialog = new ProgressDialog(context);
    }

    @Override
    protected void onPreExecute() {
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setMessage("Performing Query...");
        mProgressDialog.show();
        Log.d("log_tag", "Showed dialog");
    }

    @Override
    protected void onPostExecute(Void result) {
        if (!error) {
            Log.d("log_tag", "No ERROR - " + error);
            JSONArray jArray;
            List<Map<String, String>> lValues = new ArrayList<Map<String, String>>();
            try{
                jArray = new JSONArray(taskResult);
                JSONObject json_data=null;
                //Log.d("log_tag", "for loop to read data");
                for(int i=0;i<jArray.length();i++){
                    json_data = jArray.getJSONObject(i);
                    Map<String, String> datum = new HashMap<String, String>(2);
                    datum.put("artist", json_data.getString("artist"));
                    datum.put("title", json_data.getString("title"));
                    lValues.add(datum);
                }
            } catch(JSONException e1){
                Message("No Results Found");
            } catch (ParseException e1) {
                e1.printStackTrace();
            }
            lstResults.setAdapter(new SimpleAdapter(context, lValues, 
                        android.R.layout.simple_expandable_list_item_2,
                        new String[] {"artist", "title"}, 
                        new int[] {android.R.id.text1, android.R.id.text2}));
        } else {
            Message("ERROR: " + errString);
            error = false;
            errString = "";
        }

        if (mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
            Log.d("log_tag", "Closed dialog");
        }

    }

    @Override
    protected Void doInBackground(Void... params) {
        StringBuilder sb;
        InputStream is = null;
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("c_name", generateSearchCriteria()));
        Log.d("log_tag", nameValuePairs.get(0).toString());
        try{
            Log.d("log_tag", "Create Object");
             HttpClient httpclient = new DefaultHttpClient();
             HttpPost httppost = new HttpPost(RHINOGOLD_URL);
             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
             HttpResponse response = httpclient.execute(httppost);
             HttpEntity entity = response.getEntity();
             is = entity.getContent();
        }catch(Exception e){
            errString = "Connection Error";
            error = true;
            Log.e("log_tag", "Error in http connection - " + error + " - " + e.toString());
        }

        //convert response to string
        if (!error) {
            Log.d("log_tag", "BufferedReader - " + error);
            try{
                  BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                   sb = new StringBuilder();
                   Log.d("log_tag", "append");
                   sb.append(reader.readLine() + "\n");

                   String line="0";
                   while ((line = reader.readLine()) != null) {
                                  sb.append(line + "\n");
                   }
                   Log.d("log_tag", "close input stream");
                   is.close();
                   if (sb.toString() == null) {
                       taskResult = "No values returned";
                   } else {
                       taskResult=sb.toString();
                   }
            } catch(Exception e) {
                errString = "The application encountered an error";
                error = true;
                Log.e("log_tag", "Error converting result - " + error + " - " + e.toString());
            }
        }
        return null;
    }
}

我敢肯定这里有很多编程没有,但正如我所说,我是新手,所以我仍在弄清楚所有这些是如何工作的。无论如何,如您所见,我在这里有一些日志,我观察 LogCat 输出以查看对话框何时弹出,这就是我得到的。

Showed dialog
c_name=artist LIKE 'journey' OR title LIKE 'journey'
Create Object
... (long pause)
BufferedReader - false
append
close input stream
No ERROR - false
Closed dialog

直到...(长时间停顿)之后我才看到对话框,它只显示一瞬间。暂停后的行发生得如此之快,我不确定对话框开始显示的确切位置,但据我所知,它应该在长时间暂停之前显示,特别是因为我看到日志“显示对话框”

有人可以帮我弄清楚为什么我的对话框会在最后一刻出现吗?

4

2 回答 2

1

试试这个.....创建你的private ProgressDialog mProgressDialog;

在您的活动中。

onCreate(.....) {

    ...........
    ...........
    mProgressDialog = new ProgressDialog(this);
}

现在剩余的过程与您在异步任务中所做的相同。就像@PearsonArtPhoto 提到的那样做

 private class MyAsyncTask extends AsyncTask<Null , Int, Null>
 { ...
      doInBackground(...)
      {
           ....
           publishProgress(Progress_metric ...);
      }
      ......
      ......
 }
于 2013-04-14T07:45:30.167 回答
1

您需要以某种方式发布您的进度doInBackground()

 private class MyAsyncTask extends AsyncTask<Null , Int, Null>
{ ...
doInBackground(...)
{
     ....
     publishProgress(Progress_metric ...)
}
于 2012-11-15T18:45:04.593 回答