2017-04-05 77 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); 
     }