1

Please Help,

whay the result if(Title.toString().trim() == "camera") is false?
my code:

mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                //Get item at position
                GridMenuItem item = (GridMenuItem) parent.getItemAtPosition(position);
                String Title =item.getTitle();
                if(Title.toString().trim() == "camera"){

                    String strPhoneNumber;
                    Bundle b = getIntent().getExtras();
                    strPhoneNumber = b.getString("phonenumber");

                    Intent myIntent = new Intent(v.getContext(), UploadActivity.class);
                    Bundle bs = new Bundle();
                    bs.putString("phonenumber", strPhoneNumber); //Your id
                    myIntent.putExtras(b); //Put your id to your next Intent
                    startActivityForResult(myIntent, 0);
                    finish();
                }
                if(Title.toString() =="history"){
                    Intent myIntent = new Intent(v.getContext(), HistoryActivity.class);
                    startActivityForResult(myIntent, 0);
                }
                if(Title.toString() =="setting"){
                    Intent myIntent = new Intent(v.getContext(), HistoryActivity.class);
                    startActivityForResult(myIntent, 0);
                }

            }
        });

debug

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
asgar t3a
  • 33
  • 3
  • 2
    Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Henry Feb 18 '17 at 08:32
  • @Henry use `String.equals(String params)` method. http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equals(java.lang.Object) – Distjoy Feb 18 '17 at 08:36
  • @Henry, many thank , it's Work – asgar t3a Feb 18 '17 at 09:27

3 Answers3

1

Change the code like this:

mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                //Get item at position
                GridMenuItem item = (GridMenuItem) parent.getItemAtPosition(position);
                String Title =item.getTitle().toString().trim();
                Log.d("testString",Title);
                if(Title.equals("camera")){

                    String strPhoneNumber;
                    Bundle b = getIntent().getExtras();
                    strPhoneNumber = b.getString("phonenumber");

                    Intent myIntent = new Intent(v.getContext(), UploadActivity.class);
                    Bundle bs = new Bundle();
                    bs.putString("phonenumber", strPhoneNumber); //Your id
                    myIntent.putExtras(b); //Put your id to your next Intent
                    startActivityForResult(myIntent, 0);
                    finish();
                }
                if(Title.equals("history")){
                    Intent myIntent = new Intent(v.getContext(), HistoryActivity.class);
                    startActivityForResult(myIntent, 0);
                }
                if(Title.equals("setting")){
                    Intent myIntent = new Intent(v.getContext(), HistoryActivity.class);
                    startActivityForResult(myIntent, 0);
                }

            }
        });

Please post the value of log "testString" in the comment below. Look if it shows "Camera" instead of "camera" then change the if statement accordingly.

Pulkit
  • 1,020
  • 3
  • 12
  • 26
0

Try this

Title.equals ("camera")

Since title is a string there is no need for using toString and trim

Abilash
  • 313
  • 5
  • 15
0

if(Title.toString().equal("camera"))

Because in java, String is an Object Type(Reference) so you must use equal instead of ==.

"==" operator is using for primitive types only (like int, long ...)

phapli
  • 627
  • 6
  • 16