2017-10-15 155 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 
    } 



}