2017-10-15 152 views
0
public void bytesToHex(byte[] in) { 
    final StringBuilder builder = new StringBuilder(); 
    int count=0; 
    final int BATCHSIZE=20; 
    sendingData = true; 
    Log.d("byteToHex", "sendingData = true, start sending data."); 
    sendSerial("w"); //write command 
    Log.d("byteToHex", "sending w"); 
    for(byte b : in) { 
     //mBluetoothGatt.setCharacteristicNotification(characteristicRX, enabled); 
     //byte[] a = mBluetoothGatt.readCharacteristic(characteristicRX); 
     while(!newData){ 
      if(resendData == true){//resends previously sent string 
       sendSerial(sendByte); 
       Log.d("byteToHex", "resendData = true, resending: " + sendByte); 
       resendData = false; //reset resendData flag 
      } 
     } //wait for next w from mcu 

     builder.append(String.format("%02x", b)); 

     if(builder.length()== BATCHSIZE){ 
      sendByte= builder.toString(); 


      sendSerial(sendByte); 
      newData = false; 

      Log.d("byteToHex", "newData = false"); 

      count+=BATCHSIZE; 
      Log.d("byteToHex", "Sent " + count/2 + " bytes"); 
      textViewFileProgress.setText(count/2 + "/" + fileLength); //<- THIS SETTEXT DOES NOT WORK 
      builder.setLength(0); //reset the string builder 
     } 



    } //for(byte b : in) 

    //send remaining bytes 
    sendByte= builder.toString(); 
    sendSerial(sendByte); 
    newData = false; 
    Log.d("byteToHex", "newData = false"); 
    count+=builder.length(); 
    Log.d("byteToHex", "Sent " + count/2 + " byte"); 
    textViewFileProgress.setText(count/2 + "/" + fileLength);//<- THIS SETTEXT WORKS 
    builder.setLength(0); //reset the string builder 


    sentTerminator = true; //flag to tell BLE service to check if terminator is received on mcu 
    sendSerial("||"); //terminating command, tell teensy last hex has been sent 
    while(sentTerminator == true){ //while terminator not yet received 
     if(resendTerminator == true){ // 
      sendSerial("||"); 
      Log.d("byteToHex", "resending terminator"); 
      resendTerminator = false; //Resend complete. reset resendTerminator flag. 
     } 
    } 
    sendingData = false; 
    //return builder.toString(); 
}//public void bytesToHex(byte[] in) 

我想设置文本到我的textview显示当前的字节数发送。TextView.setText不工作for循环

不知何故,我在我的函数中有2个完全相同的setText代码。 textViewFileProgress.setText(count/2 + "/" + fileLength);

其中之一是在for循环内,这是行不通的。

另一个是在for循环之外,它工作。

我确定该程序运行该代码,因为我能够在Android监视器中看到它之前的调试消息。

任何想法是什么问题?

+0

'TextView的文本#setText()'只允许在程序的主线程中使用内部的AsyncTask类来设置'TextView'的文本。 – abcOfJavaAndCPP

+0

@abcOfJavaAndCPP这个函数在'extends Activity'类中。其应用主页 – tzj

+0

https://i.imgur.com/JNmuOwF.png 我的byteToHex函数在主线程中 – tzj

回答

0

试试这个。

Log.e("TAG",builder.length()+""); 
if(builder.length()== BATCHSIZE){ 
     sendByte= builder.toString(); 

     sendSerial(sendByte); 
     newData = false; 
     Log.d("byteToHex", "newData = false"); 

     count+=BATCHSIZE; 
     Log.d("byteToHex", "Sent " + count/2 + " bytes"); 
     textViewFileProgress.setText(count/2 + "/" + fileLength); //<- THIS SETTEXT DOES NOT WORK 
     builder.setLength(0); //reset the string builder 
} else { 
     Log.e("TAG","length != 20"); 
} 

而且你可以看到Logcat信息

+0

其按预期工作。 TextView的但是,仍然不更新时在for循环 '/ BluetoothLeService:Q E/TAG:4 E/TAG:长度= 20 E/TAG:6 E/TAG:长度= 20 E/TAG:8 E/TAG:长度= 20 E/TAG:10 E/TAG:长度= 20 E/TAG:12 E/TAG:长度= 20 E/TAG: 14 E/TAG:长度= 20 E/TAG:16 E/TAG:长度= 20 E/TAG:18 E/TAG:长度= 20 E/TAG:20 d/sendSerial :发送:0 07dc00b56890e426200 D/byteToHex:发送460字节' – tzj

+0

由“不更新”我的意思是它停留在0/,并且在它完成发送后变为/ tzj

0

考虑这个简单的代码。你实现你在另一个后台线程循环然后设置使用onPostExecute() 注意,在主线程中的文本onPostExecute()运行在主线程同时doInBackground()运行在后台线程,所以你不能设置在doInBackground()

public void outSideFunction() 
{ 
    //pass a string to doInBackground() 
    new TextSetterTask.execute(""); 


} 
//implement an inner class 
private class TextSetterTask extends AsyncTask<String,Void,String> 
{ 
    @Override 
    protected String doInBackground(String... params) 
    { 
    //loop 
     return //the string you want to set the text 
    //if the background thread is done it will call the onPostExecute 
    } 
    @Override 
    protected void onPostExecute(String result) 
    { 
    //set the text 
    } 



}