【问题标题】:Issue with Bundle in androidAndroid中的Bundle问题
【发布时间】:2010-11-25 12:13:20
【问题描述】:

我在 Bundle 中放置了一个字符串值(用户在我的搜索活动中输入的 Edittext)。然后我得到了 Bundle (Result-Activity) 来检查用户是否在没有任何价值的情况下离开了 Edittext。但它不起作用。如果语句有问题。好像什么都没检查。我调试并看到第一个“If”被执行,然后是“Else”。有人可以帮我吗?

搜索活动:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        mDbHelper = new SBooksDbAdapter(this);
        mDbHelper.open();
        setContentView(R.layout.sbooks_search); 

        mTextSearch = (EditText)findViewById(R.id.text_search);     
        searchButton = (Button)findViewById(R.id.btn_search);

        mTextSearch.setFocusableInTouchMode(true);
        mTextSearch.setHint(R.string.button_hint);

        searchButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){        
                    startSearch();                                  
            }
        });
    }
    // Start Activity Search Result
    public void startSearch(){                  
        Bundle bundle = new Bundle();       
        bundle.putString(SBooksDbAdapter.KEY_TITLE_RAW, mTextSearch.getText().toString());

        Intent intentSearch = new Intent(this, SBooksSearchResult.class);
        intentSearch.putExtras(bundle);     
        this.startActivity(intentSearch);           
    }

结果-活动:

Bundle bundle = getIntent().getExtras();        
    String searchText = bundle.getString(SBooksDbAdapter.KEY_TITLE_RAW);        

    if(searchText != "")
    {
        // Do nothing           
    }
    else {  
        searchText = "Hello" ;
    }

【问题讨论】:

  • 1.您可以在 Intent 上调用 getStringExtra() —— 您无需调用 getExtras() 之后再调用 getString()。 2. 缺少的 Bundle 值从 getString() 返回 null,而不是 ""。 3. 您可能希望在调用 putExtra() 的位置发布代码以将值存储在 Intent 中。如果没有它,也许还有其他代码,将很难为您提供帮助。
  • 嗨,我在您评论时发布代码。还有一件事,我调试(太多次),发现searchText值是“”,而不是null。希望你能帮上忙!

标签: android database listview if-statement


【解决方案1】:

代替:

if (searchText != "")

尝试:

if (!searchText.equals(""))

【讨论】:

    猜你喜欢
    • 2014-09-15
    • 1970-01-01
    • 2021-06-02
    • 2020-04-22
    • 1970-01-01
    • 2011-10-29
    • 2011-05-21
    • 2018-10-19
    • 2017-06-11
    相关资源
    最近更新 更多