2017-02-22 5 views
-2

Ich habe ein Problem Ich möchte meine JSON vergleichen, aber ist nicht arbeiten, ich verstehe nicht warum. Ich versuche, die Funktion compareTo() zu verwenden, funktioniert aber auch nicht. Ich möchte, dassWie kann ich einen JSON mit < or > vergleichen

if (Myjson < 5 oder Myjson> 5)

{(etwas tun)}

zeige ich meinen Code

Acceuil.java

public class Accueil extends AppCompatActivity { 
String json_string; 
JSONObject jObj = null; 
private TextView Mpx,Al,Ar,Rds,Pilots,Frequence,Rf; 
private String value= String.valueOf(5); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_accueil); 
    json_string = getIntent().getExtras().getString("json_data"); 

    Mpx = (TextView) findViewById(R.id.mpx); 
    Ar=(TextView)findViewById(R.id.ar); 
    Al=(TextView)findViewById(R.id.al); 
    Rds=(TextView)findViewById(R.id.rds); 
    Pilots=(TextView)findViewById(R.id.pilots); 
    Frequence=(TextView)findViewById(R.id.fréquence); 
    Rf=(TextView)findViewById(R.id.rf); 

      try { 
       // Json Object {} 
       /******************************************************************************/ 

       String mpx, rds, al, ar, frequence, pilots, id, id_SIGFOX, timestamps, rf; 
       jObj = new JSONObject(json_string); 
       mpx = jObj.getString("MPX"); 
       rds = jObj.getString("RDS"); 
       rf = jObj.getString("RF"); 
       frequence = jObj.getString("Frequence"); 
       timestamps = jObj.getString("timestamp"); 
       id = jObj.getString("id"); 
       id_SIGFOX = jObj.getString("id_SIGFOX"); 
       pilots = jObj.getString("PILOT"); 
       al = jObj.getString("a_l"); 
       ar = jObj.getString("a_r"); 
       Valeur valeurs = new Valeur(mpx, rds, al, ar, frequence, pilots, id, timestamps, id_SIGFOX, rf); 
       /******************************************************************************/ 
       if(mpx.compareTo(String.valueOf(5))) { 
       Mpx.setText(valeurs.getMpx()) 
       Mpx.setText(valeurs.getMpx()); 
       Mpx.setTextColor(Color.parseColor("#00000")) 

       }else{ 
       Mpx.setText(valeurs.getMpx()) 
       Mpx.setTextColor(Color.parseColor("#DF0101"))};//red 

      } catch (JSONException e) { 

       e.printStackTrace(); 
      } 

     } 

public void popUp(View view){ 
    Intent intent=new Intent(this,popUp.class); 
    startActivity(intent); 

} 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.warning: 
      Intent intent = new Intent(this, popUp.class); 
      startActivity(intent); 
      return true; 
     case R.id.localiser: 
      return true; 
     case R.id.station: 
      return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    //ajoute les entrées de menu_test à l'ActionBar 
    getMenuInflater().inflate(R.menu.menu, menu); 
    return true; 
} 
} 

Valeur .java

public class Valeur { 
    private String mpx,rds,al,ar,pilots,frequence,id,timestamps,id_SIGFOX,rf; 
    public Valeur(String mpx, String rds, String al, String ar, String pilots, String frequence, String id, String timestamps, String id_SIGFOX, String rf) 

    { 
     this.setMpx(mpx); 
     this.setRds(rds); 
     this.setAl(al); 
     this.setAr(ar); 
     this.setPilots(pilots); 
     this.setFrequence(frequence); 
     this.setId(id); 
     this.setTimestamps(timestamps); 
     this.setId_SIGFOX(id_SIGFOX); 
     this.setRf(rf); 

    } 




    public String getMpx() { 
    return mpx; 
} 

    public void setMpx(String mpx) { 
    this.mpx = mpx; 
} 

    public String getRds() { 
    return rds; 
} 

    public void setRds(String rds) { 
    this.rds = rds; 
} 

    public String getAl() { 
    return al; 
} 

    public void setAl(String al) { 
    this.al = al; 
} 

    public String getAr() { 
    return ar; 
} 

    public void setAr(String ar) { 
    this.ar = ar; 
} 

    public String getPilots() { 
    return pilots; 
} 

    public void setPilots(String pilots) { 
    this.pilots = pilots; 
} 

    public String getFrequence() { 
    return frequence; 
} 

    public void setFrequence(String frequence) { 
    this.frequence = frequence; 
} 

    public String getTimestamps() { 
    return timestamps; 
} 

    public void setTimestamps(String timestamps) { 
    this.timestamps = timestamps; 
} 

    public String getId() { 
    return id; 
} 

    public void setId(String id) { 
    this.id = id; 
} 

    public String getId_SIGFOX() { 
    return id_SIGFOX; 
} 

    public void setId_SIGFOX(String id_SIGFOX) { 
    this.id_SIGFOX = id_SIGFOX; 
} 

    public String getRf() { 
    return rf; 
} 

    public void setRf(String rf) { 
    this.rf = rf; 
} 
} 

mein Fluss json

{"id":"1","timestamp":"2017-01-31 10:59:11","id_SIGFOX":"ABER","MPX":"1","RDS":"2","RF":"79","PILOT":"8","a_l":"-5","a_r":"-39","Frequence":"1034"} 
+0

Also versuchen Sie zu vergleichen '' String'' Werte mit ''> '' und '' < ''? Was ist dann das Ergebnis von '' "Hallo" <"World" ''? Ist das "richtig" oder "falsch"? – f1sh

+0

HI? Ich möchte meinen Wert mpx vergleichen (wer ist 1) bei 5, so will ich, dass wenn ich (mpx (1) <5) bearbeiten werde für meine Fluss json –

+0

Sie den String-Wert von JSON zu Integer umwandeln zu konvertieren vergleiche – Blip

Antwort

0

Sie wie folgt vor:

int mpxInt = Integer.parseInt(mpx); 
if(mpxInt > 5) { 
    // mpx is > 5 
} else { 
    // mpx is < 5 
} 

Dies sollte funktioniert

+0

thx es funktioniert jetzt –

0

ich glaube, das Problem ist, dass Sie versuchen, den String in mpx mit der ganzen Zahl 5.

mpx = jObj.getString("MPX"); 
... 
if(mpx.compareTo(String.valueOf(5))) { 

Um dies richtig zu vergleichen, müssen Sie mpx konvertieren in int. Verwenden Sie entweder etwas anderes als jObj.getString oder verwenden Sie Integer.parseInt, um die Zeichenfolge in ein int zu konvertieren.

+0

thx es funktioniert jetzt –

Verwandte Themen