vendredi 9 mai 2014

Android - chaîne comparant à chaîne ne fonctionne pas lorsque la chaîne sont venus SQLiteDatabase - Stack Overflow


I'm retrieving string data from my sqlitedatabase and compare in to my local variable. But its not working. It goes like this codes below.


String hole1 = " ";
Cursor cc = save.rawQuery("Select *from " +data + " where Mobile = '"+ number +"'", null);
cc.moveToFirst();
hole1 = cc.getString(cc.getColumnIndex("Hole1"));
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole1,"1", "2", "3", "4","5","6","7","8","9" }); adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
((Spinner) findViewById(R.id.soloscorehole1front9)).setAdapter(adapter1);

if( ((Spinner) findViewById(R.id.soloscorehole1front9)).getSelectedItem().toString() == hole1){
Toast.makeText(Activity.this,"True!", Toast.LENGTH_LONG).show();
}

Can anyone point what is wrong with this simple codes?




You shouldn't use == to compare strings. Use string1.equals(string2) instead. Using == compares if the reference to each string is equal whereas .equals() compares the values of the strings




String comp like this would be better


().getSelectedItem().toString().equals(hole);


I'm retrieving string data from my sqlitedatabase and compare in to my local variable. But its not working. It goes like this codes below.


String hole1 = " ";
Cursor cc = save.rawQuery("Select *from " +data + " where Mobile = '"+ number +"'", null);
cc.moveToFirst();
hole1 = cc.getString(cc.getColumnIndex("Hole1"));
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole1,"1", "2", "3", "4","5","6","7","8","9" }); adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
((Spinner) findViewById(R.id.soloscorehole1front9)).setAdapter(adapter1);

if( ((Spinner) findViewById(R.id.soloscorehole1front9)).getSelectedItem().toString() == hole1){
Toast.makeText(Activity.this,"True!", Toast.LENGTH_LONG).show();
}

Can anyone point what is wrong with this simple codes?



You shouldn't use == to compare strings. Use string1.equals(string2) instead. Using == compares if the reference to each string is equal whereas .equals() compares the values of the strings



String comp like this would be better


().getSelectedItem().toString().equals(hole);

0 commentaires:

Enregistrer un commentaire