我是Android的新手,我设法制作以下代码,用于从网站上将信息下载到我的手机应用程序中:
public class Ws_Download_KJ extends AsyncTask<Object, Boolean, String> {
Adapter_Descarca callerActivity;
private Activity activity;
private ProgressDialog dialog;
private Context context;
private DataBase_MeniuDreapta sursa_sql;
public Ws_Download_KJ(Activity activity){
this.activity = activity;
this.context = activity;
this.dialog = new ProgressDialog(activity);
this.dialog.setTitle("Downloading");
this.dialog.setMessage("Please wait...");
this.dialog.setCancelable(true);
if(!this.dialog.isShowing()){
this.dialog.show();
}
}
@Override
protected String doInBackground(Object... params) {
String serviceUrl = (String) params[0];
callerActivity = (Adapter_Descarca) params[1];
List<NameValuePair> nameValuePairs = (List<NameValuePair>) params[2];
BasicWebService webService = new BasicWebService(serviceUrl);
webService.nameValuePairs=nameValuePairs;
return webService.webGet();
}
@Override
protected void onPostExecute(String response) {
JSONArray jArray;
sursa_sql = new DataBase_MeniuDreapta(activity);
sursa_sql.open();
sursa_sql.sterge_kj();
try
{
jArray = new JSONArray(response);
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);
sursa_sql.insert_kj(json_data.getString("salvare"));
}
sursa_sql.finalizare_kj();
sursa_sql.close();
dialog.dismiss();
}
catch(Exception e)
{
Log.d("Error: ", " "+e.getMessage());
}
super.onPostExecute(response);
}
}
我的应用程序正在连接到一个网站,抛出这个课程,并回忆起json的共鸣。 json响应包含sql insert的语句,如:
insert into table values (....),(....),(....) ....
insert into table values (....),(....),(....) ....
我的问题,希望你能帮助我: 如何将我的对话框从我所拥有的转换为进度条。要添加或修改的内容。
谢谢,
答案 0 :(得分:2)
要向用户显示进度,您可以使用ProgressDialogs方法 以下是我的某个应用中的一些示例代码:
ProgressDialog mProgressDialog = new ProgressDialog(mContext);
mProgressDialog.setMessage("Downloading...");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.show();
mProgressDialog.setIndeterminate(false);
//set the max value for the dialog
mProgressDialog.setMax(fileTableCursor.getCount());
for(int i = 0; i < fileTableCursor.getCount(); i++) {
//set the progress
mProgressDialog.setProgress(i+1);
}
//dismiss ProgressDialog after work is done
mProgressDialog.dismiss();
我在Activity中使用ProgressDialog。您可以使用AsyncTasks onProgressUpdate
设置进度,因为您无法从doinBackground
主题访问用户界面。
希望这会有所帮助:)