2015-12-25 44 views
0
@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之前,这两个文本框,相关线(新的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$1.run(SyncClass.java:121) 
12-25 12:19:45.909: E/AndroidRuntime(21914): at java.lang.Thread.run(Thread.java:856) 
+0

你可以请发布整个代码?我不清楚你想要做什么。 –

+1

你不能在线程中处理视图(文本视图),你需要使用ui线程。 – Harry

回答

0

我会建议运行在UI线程上(runOnUiThread())对于

其次更新文本字段和UI组件,你有重大的逻辑问题。 从您最初的代码,我假设你想要做的10 SyncTask而做的状态跟踪。

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

以下跟踪应该从取出您的onClick方法内循环。

@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(); 
    } 
} 

创建包装类(例如:CustomerSyncDetails.class)为字符串1(CUSTOMER_ID),字符串2(名称)和STRING3(mobile1)。

然后把所有10个客户的详细信息到一个数组(例如:arrayofCustomerSyncDetails),通过你的包装对象,以SyncTask的阵列。

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

new SyncTask().execute(arrayofCustomerSyncDetails); 

对于跟踪线程,有一个主要的逻辑缺陷,这就是它显示100个上传数据的主要原因。

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

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