变量string = value(string)但结果为false

时间:2017-02-18 08:30:42

标签: android string android-studio

请帮助,

结果if(Title.toString().trim() == "camera")false吗? 我的代码:

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

3 个答案:

答案 0 :(得分:1)

更改代码如下:

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);
                }

            }
        });

请发布log&#34; testString&#34;的值在下面的评论中。 看它是否显示&#34;相机&#34;而不是&#34;相机&#34;然后相应地更改if语句。

答案 1 :(得分:0)

试试这个

Title.equals ("camera")

由于title是一个字符串,因此不需要使用toString和trim

答案 2 :(得分:0)

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

因为在java中,String是一个Object Type(Reference),所以你必须使用equal而不是==。

“==”运算符仅用于基本类型(如int,long ...)