-4

ok.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(mode.getText().toString()=="Default"){
                Toast.makeText(getApplicationContext(), "Cannot Delete Default", Toast.LENGTH_SHORT).show();
            }


            //else{

            /*  db = openOrCreateDatabase("MY_App_Data", MODE_PRIVATE, null);

            db.execSQL(DATABASE_TABLE_CREATE);
            ContentValues cvInsert=new ContentValues();
            cvInsert.put("mode",mode.getText().toString());
            cvInsert.put("msg",msg.getText().toString());
            db.insert("SSP_Data", null, cvInsert);
            Toast.makeText(getApplicationContext(), "Mode Added", Toast.LENGTH_SHORT).show();
            list.add(mode.getText().toString());*/
            //}
        }
    });

hey guys in the above code my if statement is not working please help me out there iz no syntax error then why its nt executing please say smthing abt it

yogs
  • 3
  • 3
  • You should read about java before getting into android, mode.getText().toString()=="Default" is looking if strings are pointing at same string object, mode.getText().toString().equals("Default") is what you might need... – Martin Cazares Nov 20 '13 at 20:05

4 Answers4

2

You can't compare Strings this way in Java. Use .equals()

if("Default".equals(mode.getText().toString()){

Using == checks if they are the same Object but not if they have the same values.

Also, putting the String in front of the variable this way protects against a NPE. Your mode.getText().toString() could return null but your literal String will not.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

Use .equals or .equalsIgnoreCase to compare strings

if(mode.getText().toString().equals("Default")){
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

You have to use equals to compare strings, like this:

if(mode.getText().toString().equals("Default")){
                Toast.makeText(getApplicationContext(), "Cannot Delete Default", Toast.LENGTH_SHORT).show();
            }
Xaver Kapeller
  • 49,491
  • 11
  • 98
  • 86
0

You didn't say how it didn't work, but you almost certainly wanted this:

if (mode.getText().toString().equals("Default"))

rather than this:

if (mode.getText().toString() == "Default")

The first tests to see whether the two strings have the same content. The second tests to see if they're the same string.

David Given
  • 13,277
  • 9
  • 76
  • 123