-1

im trying to compare a text in android to avoid refreshing texts if it's not necessary (otherwise the texts for different fields are refreshed every 50ms).

With normal texts it is not a problem. Everything works fine. But: If there are numbers in text the text seems not to be equal. Why?

Some examples: "Abschaltpunkt" is equal to "Abschaltpunkt" (OK) / "Gewicht" is equal to "Gewicht" (OK) / "100 kg" is NOT equal to "100 kg" (NOK) / "Abschaltpunkt 2" is NOT equal to "Abschaltpunkt 2" (NOK"

A new question after edit (the comparsion works fine now). As you see I use the UI thread for refreshing the text. This app works in a network and receives arround 300 messages per second with different data (yes, it is necessary). Therefore I need the comparsion otherwise the thread is blocked and the app won't respond on user inputs. Is there another solution? Or is my solution a good one?

This is my code:

 /**
  * Compares the current and new text und updates the text if necessary
  * @param RessourceID given by R.id...
  * @param New text
  */
 private void ChangeText (final int RessourceID, final String sText) { 
  try {
   TextView tv = (TextView) findViewById(RessourceID);
   // Erst prüfen ob Text ersetzt werden muss -> Spart Rechenzeit
   if (tv.getText().toString().equals(sText)) {
    Log.e(this.getClass().getSimpleName(), "Text nicht ersetzen: " + tv.getText() + " != " + sText);
   } else {
    ChangeTextThread(tv, sText);
    Log.e(this.getClass().getSimpleName(), "Text ersetzen: " + tv.getText() + " != " + sText );     
   }
  } catch (Throwable t) {
   Log.e(this.getClass().getSimpleName(), "ChangeText", t);
  }
 }
  
  /**
   * Change the text in UI tread 
   */
  private void ChangeTextThread (final TextView tv, final String sText) {
   this.runOnUiThread(new Runnable() {
      public void run() {
       
       try {
        tv.setText(sText);
       } catch (Throwable t) {
       Log.e(this.getClass().getSimpleName(), "ChangeTextThread", t);
       }
    
      }
    });
  }
Selucky
  • 3
  • 3

2 Answers2

3

try this way

if(tv.getText().toString().equals(sText))

Used .equals() or .equalsIgnoreCase() method for string comparison

M D
  • 47,665
  • 9
  • 93
  • 114
0

Try below code:

if(tv.getText().toString.equalsIgnoreCase(sText)){
     //Do your stuff here
}
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44