全部工作完成后进度条运行

Progress bar running after all the work is done

@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.btn_sync:
for ( int i = 0; i<10; i++)   {
    current_status = i;
    new SyncTask().execute(String1,String2,String3);
    new Thread(new Runnable() {
        public void run() {
            while (current_status < 100) {
                current_status += 1;
                //sync_progress.setProgress(current_status);
                //sync_decrip.setText(current_status+" data is uploading from total of "+c_id.size());
                handler.post(new Runnable() {
                    public void run() {
                        sync_progress.setProgress(current_status);
                        sync_decrip.setText(current_status + " data is uploading from total of " + c_id.size());
                    }
                });
                try {
                    // Sleep for 200 milliseconds. 
                    //Just to display the progress slowly
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}
}
}

public class SyncTask extends AsyncTask<String, Void, String> {     
    ProgressDialog progressDialog;
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... arg0) {
        String result=null;
            try{
                // url where the data will be posted                    
                String postReceiverUrl = "http://MyUrl";
                Log.v(TAG, "postURL: " + postReceiverUrl);                   
                HttpClient httpClient = new DefaultHttpClient();   // HttpClient                    
                HttpPost httpPost = new HttpPost(postReceiverUrl);  // post header

                // add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
                //Log.d(uname.getText().toString(), pass.getText().toString());
                nameValuePairs.add(new BasicNameValuePair("customer_id", arg0[0]));
                nameValuePairs.add(new BasicNameValuePair("name", arg0[1]));
                nameValuePairs.add(new BasicNameValuePair("mobile1", arg0[2]));

                // execute HTTP post request
                HttpResponse response = httpClient.execute(httpPost);
                result = EntityUtils.toString(response.getEntity());
        } 
            catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    protected void onPostExecute(String result) {
        // do stuff after posting data
        //progressDialog.dismiss();
        String message=null;  
        try {
            Log.d("Inside Post", "message");
            root = new JSONObject(result);
            validation = root.getInt("response");
            message = root.getString("CustomerName");
            message+=root.getString("MobileNumber");
            up_state=root.getString("MobileNumber");
            Log.d(result, user_id);
            //Toast.makeText(getApplicationContext(), user_id, Toast.LENGTH_LONG).show();
            if(validation==1) {
                Log.e(message, root.getString("Emailid"));
                //Toast.makeText(getApplicationContext(),"Success: "+ message, Toast.LENGTH_LONG).show();
            }
//              else
//                  Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
            }
        catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    }
}

我正在尝试使用进度条将数据从 sqlite 同步到数据库。当我调试这个进度条时,在所有数据上传后显示。在文本框中 current_status 始终是“100 条数据正在上传,总共 10 条数据”。但是数据只有10个。上传一个数据时,如何更改文本框的值。我在 handler.post(new Runnable(){ 之前尝试了这两个与文本框相关的行。但是该应用程序因以下日志而崩溃。

12-25 12:19:45.909: E/AndroidRuntime(21914): FATAL EXCEPTION: Thread-981
12-25 12:19:45.909: E/AndroidRuntime(21914):         android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-25 12:19:45.909: E/AndroidRuntime(21914):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4077)
12-25 12:19:45.909: E/AndroidRuntime(21914):    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:734)
12-25 12:19:45.909: E/AndroidRuntime(21914):    at android.view.View.requestLayout(View.java:12679)
12-25 12:19:45.909: E/AndroidRuntime(21914):    at android.view.View.requestLayout(View.java:12679)
12-25 12:19:45.909: E/AndroidRuntime(21914):    at android.view.View.requestLayout(View.java:12679)
12-25 12:19:45.909: E/AndroidRuntime(21914):    at android.view.View.requestLayout(View.java:12679)
12-25 12:19:45.909: E/AndroidRuntime(21914):    at android.view.View.requestLayout(View.java:12679)
12-25 12:19:45.909: E/AndroidRuntime(21914):    at android.view.View.requestLayout(View.java:12679)
12-25 12:19:45.909: E/AndroidRuntime(21914):    at android.widget.TextView.checkForRelayout(TextView.java:6773)
12-25 12:19:45.909: E/AndroidRuntime(21914):    at android.widget.TextView.setText(TextView.java:3306)
12-25 12:19:45.909: E/AndroidRuntime(21914):    at android.widget.TextView.setText(TextView.java:3162)
12-25 12:19:45.909: E/AndroidRuntime(21914):    at android.widget.TextView.setText(TextView.java:3137)
12-25 12:19:45.909: E/AndroidRuntime(21914):    at ridio.helixtech.SyncClass.run(SyncClass.java:121)
12-25 12:19:45.909: E/AndroidRuntime(21914):    at java.lang.Thread.run(Thread.java:856)

我建议 运行 在 UI 线程 (runOnUiThread ()) 上更新文本字段和 UI 组件。

其次,你的逻辑问题严重。 根据您的初始代码,我假设您想在跟踪状态时执行 10 个 SyncTask。

从我能看到的逻辑问题来看,这里是我推荐的指导重构工作流程(为了你的学习曲线,我不会给出直接的解决方案):

应从 onClick 方法内的 for 循环中删除以下跟踪。

@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.btn_sync:
      for ( int i = 0; i<10; i++)   {
        current_status = i;
        new SyncTask().execute(String1,String2,String3);
      }

      // NOTE: The following should be refactored: 
      // It is better to create another AsyncTask for this tracking and updating the progress
      new Thread(new Runnable() {
        public void run() {
            while (current_status < 100) {
                current_status += 1;
                //sync_progress.setProgress(current_status);
                //sync_decrip.setText(current_status+" data is uploading from total of "+c_id.size());
                handler.post(new Runnable() {
                    public void run() {
                        sync_progress.setProgress(current_status);
                        sync_decrip.setText(current_status + " data is uploading from total of " + c_id.size());
                    }
                });
                try {
                    // Sleep for 200 milliseconds. 
                    //Just to display the progress slowly
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
  }
}

为 String1(customer_id)、String2(name) 和 String3(mobile1) 创建包装器 class(例如:CustomerSyncDetails.class)。

然后将所有 10 个客户详细信息放入一个数组(例如:arrayofCustomerSyncDetails),将包装器对象的数组传递给 SyncTask。

在 SyncTask doInBackground() 方法中执行 for 循环。

new SyncTask().execute(arrayofCustomerSyncDetails);

对于跟踪线程,存在一个重大逻辑缺陷,这是它显示已上传100 条数据的主要原因。

while (current_status < 100) {
  current_status += 1;
  //sync_progress.setProgress(current_status);
  ...

我建议通过在 AsyncTask 和跟踪线程之间共享进度变量来重新考虑解决方案。