我在android.it上有tab + swipe app工作正常。我使用asyntask方法在第二页上使用json从url加载数据,它也可以正常工作。但是当它加载数据时,我会显示进度条。问题是在第一页上看到进度对话框。我想只看到第二页。我怎么能这样做?
谢谢,
修改
这是我的代码
public class web_get_thread扩展了AsyncTask {
private ProgressDialog progressDialog = new ProgressDialog(context);
InputStream inputStream = null;
String result = "";
protected void onPreExecute() {
progressDialog.setMessage("Loading data...");
progressDialog.show();
progressDialog.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface arg0) {
web_api_get_thread.this.cancel(true);
}
});
}
@Override
protected String doInBackground(String... params) {
StringBuilder builder = new StringBuilder();
try {
// Set up HTTP post
// HttpClient is more then less deprecated. Need to change to URLConnection
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(params[0]);
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
// Log.e(ParseJSON.class.toString(), "Failed to download file");
}
} catch (UnsupportedEncodingException e1) {
Log.e("UnsupportedEncodingException", e1.toString());
e1.printStackTrace();
} catch (ClientProtocolException e2) {
Log.e("ClientProtocolException", e2.toString());
e2.printStackTrace();
} catch (IllegalStateException e3) {
Log.e("IllegalStateException", e3.toString());
e3.printStackTrace();
} catch (IOException e4) {
Log.e("IOException", e4.toString());
e4.printStackTrace();
}
// Convert response to string using String Builder
return builder.toString();
} // protected Void doInBackground(String... params)
protected void onPostExecute(String string) {
//parse JSON data
try{
JSONArray jArray = new JSONArray(string);
this.progressDialog.dismiss();
} catch (JSONException e) {
Log.e("JSONException", "Error: " + e.toString());
} // catch (JSONException e)
} // protected void onPostExecute(Void v)
}
答案 0 :(得分:0)
您可以在开始加载网址时显示ProgressBar,并在加载完成后使其不可见。为此,您只需实施WebViewClient
并扩展onPageStarted
和onPageFinished()
,如下所示:
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap facIcon) {
mProgressBar.setVisibility(View.VISIBLE);
}
@Override
public void onPageFinished(WebView view, String url) {
mProgressBar.setVisibility(View.GONE);
}
});