2017-04-05 75 views
-1

我試圖通過由Hashmap所做的IntentExtra調整「if」基於來自另一活動的值。情況;(Android)通過Hashmap檢查IntentExtra的值


一個Java類

的Hashmap

插入發送意圖的意圖外

將一個值在字符串


B Java類

獲得額外的意向

使用 「如果」 知道哪個值handedover投入額外的字符串

檢查。


問題是最後一個。

B活動的代碼就是這樣。

Intent intent = getIntent(); 
    String mapKey; 
    mapKey = intent.getStringExtra(MAPMARKER); 
    Toast.makeText(getApplicationContext(),mapKey,Toast.LENGTH_SHORT).show(); 

    if(mapKey == "Donhawmun") { 
     Toast.makeText(getApplicationContext(),"SUCCESS",Toast.LENGTH_SHORT).show(); 
     pager = (ViewPager) findViewById(R.id.image_pager); 
     readingpart = (TextView) findViewById(R.id.readingPart); 
     DonImageClass adapter = new DonImageClass(getLayoutInflater()); 
     pager.setAdapter(adapter); 
     readingpart.setText(R.string.Don); 
    }else if(mapKey == "Geumcheongyo"){ 
     Toast.makeText(getApplicationContext(),"SUCCESS",Toast.LENGTH_SHORT).show(); 
     pager = (ViewPager) findViewById(R.id.image_pager); 
     readingpart = (TextView) findViewById(R.id.readingPart); 
     GeumImageClass adapter = new GeumImageClass(getLayoutInflater()); 
     pager.setAdapter(adapter); 
     readingpart.setText(R.string.Geum); 
    } 

通過第一個「吐司」是好的,所以它舉辦了一個信息「Donhawmun」。

但它不符合文字「Donhawmun」。

我不知道它是否有問題與文本直接匹配,我做了另一個字符串「Donhawmun」,並把它放在「如果」句子來比較它。但它沒有奏效。我不確定它已經將mapKey的值顯示爲「Donhawmun」,但爲什麼它與句子中的「Donhawmun」不匹配。請幫助我有任何想法的人。先謝謝你。

+0

不要使用(==)運算符。它由equals()或equalsIgnoreCase()方法完成。你的代碼看起來像if(mapKey.equals(「Donhawmun」)) –

回答

0

您需要使用平等方法來比較字符串。所以使用下面的代碼。

if(mapKey.equals("Donhawmun")) { 
    ... 
}else if(mapKey.equals("Geumcheongyo")){ 
    ... 
} 
0

不匹配文本 「Donhawmun」。

您應該考慮使用equals()代替==

這是爲什麼!

==是參考比較,即兩個對象指向相同的存儲位置。
.equals()評估爲對象中值的比較。

0

嘗試下面的代碼

if(mapKey.equalsIgnoreCase("Donhawmun")) { 
    Toast.makeText(getApplicationContext(),"SUCCESS",Toast.LENGTH_SHORT).show(); 
    pager = (ViewPager) findViewById(R.id.image_pager); 
    readingpart = (TextView) findViewById(R.id.readingPart); 
    DonImageClass adapter = new DonImageClass(getLayoutInflater()); 
    pager.setAdapter(adapter); 
    readingpart.setText(R.string.Don); 
}else if(mapKey.equalsIgnoreCase("Geumcheongyo")){ 
    Toast.makeText(getApplicationContext(),"SUCCESS",Toast.LENGTH_SHORT).show(); 
    pager = (ViewPager) findViewById(R.id.image_pager); 
    readingpart = (TextView) findViewById(R.id.readingPart); 
    GeumImageClass adapter = new GeumImageClass(getLayoutInflater()); 
    pager.setAdapter(adapter); 
    readingpart.setText(R.string.Geum); 
} 

您不能使用比較字符串 「==」。

0

使用的equals()或equalsIgnoreCase()方法

if(mapkey.equalsIgnorCase("Donhawmun")) 
{ 
...... 
} 
else if(mapkey.equalsIgnorCase("Geumcheongyo")) 
{ 
.... 
} 
0

在您的B級嘗試比較兩個字符串這

Intent intent = getIntent(); 
     String mapKey = intent.getStringExtra(MAPMARKER); 

     if(getIntent().getExtras() != null && mapKey.equals("Donhawmun")) { 
      Toast.makeText(getApplicationContext(),"SUCCESS",Toast.LENGTH_SHORT).show(); 
      pager = (ViewPager) findViewById(R.id.image_pager); 
      readingpart = (TextView) findViewById(R.id.readingPart); 
      DonImageClass adapter = new DonImageClass(getLayoutInflater()); 
      pager.setAdapter(adapter); 
      readingpart.setText(R.string.Don); 
     }else if(getIntent().getExtras() != null && mapKey.equals("Geumcheongyo")){ 
      Toast.makeText(getApplicationContext(),"SUCCESS",Toast.LENGTH_SHORT).show(); 
      pager = (ViewPager) findViewById(R.id.image_pager); 
      readingpart = (TextView) findViewById(R.id.readingPart); 
      GeumImageClass adapter = new GeumImageClass(getLayoutInflater()); 
      pager.setAdapter(adapter); 
      readingpart.setText(R.string.Geum); 
     }