Android 从网站加载数据时向我的应用程序添加进度条
我有一个简单的类,它像这样扩展了ActivyAndroid 从网站加载数据时向我的应用程序添加进度条,android,android-asynctask,Android,Android Asynctask,我有一个简单的类,它像这样扩展了Activy public class About extends Activity { private ProgressDialog pdia; private TextView tvAbout; private WebView vwAbout; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setCo
public class About extends Activity
{
private ProgressDialog pdia;
private TextView tvAbout;
private WebView vwAbout;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
vwAbout = (WebView)findViewById(R.id.vwViewAbout);
String content = new String();
try {
URL url = new URL("http://xxx.prodevAbout.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null)
{
content+=str + "\n";
vwAbout.loadData(content, "text/html", null);
}
in.close();
} catch (Exception e)
{
vwAbout.loadData("error message", "text/html", null);
}
}
}
我想做的是在从网站加载内容时添加一个progressbar,我发现了这段代码
private ProgressDialog pdia;
@Override
protected void onPreExecute(){
super.onPreExecute();
pdia = new ProgressDialog(yourContext);
pdia.setMessage("Loading...");
pdia.show();
}
@Override
protected void onPostExecute(String result){
super.onPostExecute(result);
pdia.dismiss();
}
并添加了此代码,但onPreExecute方法无法解决我该怎么办
谢谢,
添加了此代码,但无法解析onPreExecute方法应该解决什么问题
是吗
onPreExecute
未解析,因为这是来自AsyncTask
的方法,因此需要扩展AsyncTask
类以使用onPreExecute将当前代码使用AsyncTask更改为:
public class About extends Activity
{
[...your code here...]
public void onCreate(Bundle savedInstanceState)
{
[...your code here...]
// execute AsyncTask as
new LongOperation(About.this).execute("");
}
[...your code here...]
public class WebapiOperation extends AsyncTask<String, Void, String> {
Context context;
WebapiOperation(Context context){
this.context=context;
}
@Override
protected void onPreExecute() {
// show ProgressDialog here
pdia = new ProgressDialog(context);
}
@Override
protected String doInBackground(String... params) {
// make web service access task here
return null;
}
@Override
protected void onPostExecute(String result) {
// ProgressDialog here
}
}
public类关于扩展活动
{
[…您的代码在此…]
创建时的公共void(Bundle savedInstanceState)
{
[…您的代码在此…]
//按以下方式执行异步任务:
新的长操作(关于.this)。执行(“”);
}
[…您的代码在此…]
公共类WebAPI操作扩展了AsyncTask{
语境;
WebAPI操作(上下文){
this.context=context;
}
@凌驾
受保护的void onPreExecute(){
//在此处显示进度对话框
pdia=新建进度对话框(上下文);
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
//在此处创建web服务访问任务
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//在这里进行对话
}
}
我在应用程序中添加进度对话框的方式(到目前为止我从未遇到过任何问题)如下:
Runnable runable = new Runnable() {
@Override
public void run() {
SofiaLiveCafeActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB){
progressDialog = new ProgressDialog(SofiaLiveCafeActivity.this, ProgressDialog.THEME_HOLO_LIGHT);
} else {
progressDialog = new ProgressDialog(SofiaLiveCafeActivity.this);
}
progressDialog.setMessage("Loading! Pleasе wait...");
progressDialog.setIndeterminate(true);
progressDialog.show();
}
});
// CONNECT TO SERVER AND DOWNLOAD DATA
SofiaLiveCafeActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.dismiss();
}
});
}
}
};
new Thread(runable).start();
据我所知,我已经扩展了Activity,但我不能同时扩展Activity和async task???@albatross:只需为Activity创建async task内部类即可