0

I am programming on an android app, and I have a program with a qr scanner. When the scanner detects a scan something happens. My problem is that I have more than one qr code, and I need the program to recognize which qr code is being scanned. In my code I have a string "contents" that I set to be equal "SCAN_RESULT" which is different from each qr code. I see the result by printing in logCat and I have also made a textView to be the same as contents. In both cases the "SCAN_RESULT" is "Scanextra1" for the first qr code. I then say: if(contents == "Scanextra1"){ something should happen}. It doesn't though. Can anyone help me please?

The code where I am having a problem looks like this:

       if (resultCode1 == RESULT_OK) {
         contents = intent.getStringExtra("SCAN_RESULT");
         String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

         int QR = 0;
         Log.v(contents, "QR value" + QR);

         proeve.setText(contents);   
         //String contents2 = proeve.getText().toString(); 

        if (contents == "Scanextra1"){...

The entire code for the activity looks like this:

  package your.choko.namespace;

  import android.app.Activity;
  import android.content.Intent;
  import android.content.SharedPreferences;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  import android.util.Log;

  public class ScanExtra extends Activity {

    String contents;

    public static final String PREFS_NAME = "MyPrefsFile";

boolean extra1, extra2;

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.scanextra);

     SharedPreferences settings = getSharedPreferences("MyPrefsFile", 0);
           extra1 = settings.getBoolean("fact1", false);
       extra2 = settings.getBoolean("fact2", false);

Button scan = (Button) findViewById(R.id.buttonscanextra);
 scan.setOnClickListener(new OnClickListener() {

 static final int QR_ACTIVITY = 0; 
 public void onClick(View v) {
 Intent intent = new Intent("com.google.zxing.client.android.SCAN");
 intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
 startActivityForResult(intent, QR_ACTIVITY);
 } 
});
 } 

/** Called when the activity is first created. */
 @Override
 public void onPause() {
 super.onPause();

 } 

@Override
 public void onStop() {
 super.onStop();
 } 

public void onActivityResult(int requestCode, int resultCode1, Intent intent) {

    TextView proeve = (TextView) findViewById(R.id.textView1);

    if (requestCode == 0) {

       if (resultCode1 == RESULT_OK) {
         contents = intent.getStringExtra("SCAN_RESULT");
         String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

         int QR = 0;
         Log.v(contents, "QR value" + QR);

         proeve.setText(contents);   

        if (contents == "Scanextra1"){

      //WHAT HAPPENS ON SCAN 1          
        SharedPreferences settings = getSharedPreferences("MyPrefsFile",0);
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("fact1", true);           
            editor.commit();

        Toast toast = Toast.makeText(ScanExtra.this, contents, 9000);
        toast.show();
      startActivity(new Intent(ScanExtra.this, ScanExtraResultat.class));   
     }
    }
    }
    }

}

Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118
user1199422
  • 1
  • 1
  • 6

1 Answers1

0

You shouldn't use == for string comparison. Use equals() instead:

if (contents.equals("Scanextra1")) {
    ...
}

When you use ==, you check whether the two things are exactly the same object. When you use equals(), you check whether the contents of the two strings are the same.

See also:

Community
  • 1
  • 1
Sebastian Paaske Tørholm
  • 49,493
  • 11
  • 100
  • 118