【问题标题】:Pass HashMap(String,Boolean) to nextActivity using Serializable method-Android使用 Serializable 方法将 HashMap(String,Boolean) 传递给 nextActivity-Android
【发布时间】:2018-11-20 18:49:37
【问题描述】:

我有一个 Hashmap,我想将它传递给下一个 Activity,所有答案都显示此方法-:

Intent intent=new Intent(MainActivity.this,NextActivity.class);
Bundle extras = new Bundle();
extras.putSerializable("YourHashMap",hashMap);
intent.putExtras(extras);
startActivity(intent);

但它显示错误的第二个参数,它需要 Serializable 而不是 Map String,Boolean>

我什至给了这个基本的尝试-:

intent.putExtra("myMap",myMap);

但它说它无法解析方法

【问题讨论】:

  • 我建议你传递一个对象而不是 HashMap。您可能会考虑创建一个内部带有HashMap 的对象。制作对象Parcelable,然后通过intent传递它。

标签: android android-intent serialization hashmap


【解决方案1】:

试试这个

 // pass HashMap from one Activity
                Intent intent = new Intent(this, AboutActivity.class);

                HashMap<String, Boolean> map = new HashMap<>();
                map.put("var",true);
                Bundle bun = new Bundle();
                bun.putSerializable("map", map);
                intent.putExtra("bundle",bun);

                startActivity(intent);
                overridePendingTransition(R.anim.slide_from_right, R.anim.nothing);


                // get HashMap from another activity
                Bundle  bundle = getIntent().getBundleExtra("bundle");
                if(bundle != null){
                    HashMap<String,Boolean> map = (HashMap<String, Boolean>) bundle.getSerializable("map");
                    if(map != null){
                        Log.e("bundle",map.get("var")+"");
                    }

                }

【讨论】:

    【解决方案2】:

    这是我尝试过的并且有效-: //添加数据

    Intent ActivityIntent = new Intent(MainActivity.this, NewActivity.class);
                    Bundle bundles = new Bundle();
                    bundles.putSerializable("myMap", (Serializable) myMap);
                    ActivityIntent.putExtra("bundle",bundles);
    

    //提取数据

     Bundle  bundle = getIntent().getBundleExtra("bundle");
            HashMap<String, Boolean> myMap = (HashMap<String, Boolean>) bundle.getSerializable("myMap");
            if(myMap.get(StringOfItemWhichIsTrue)) {
                Toast.makeText(this, "Works", Toast.LENGTH_SHORT).show();
            }
    

    【讨论】:

    • 我会尽快回复你
    【解决方案3】:

    试试这个来传输你的值,对象必须是可序列化的,根据你的要求更改参数。

    HashMap<String, Boolean> hashMap = new HashMap<String, Boolean>();
    hashMap.put("key", "value");
    Intent intent = new Intent(this, NextActivity.class);
    intent.putExtra("map", (Serializable)hashMap);
    startActivity(intent);
    

    在你的NextActivity 中获取数据试试这个

    Intent intent = getIntent();
        HashMap<String, Boolean> hashMap = (HashMap<String, Boolean>)intent.getSerializableExtra("map");
        Log.v("HashMapTest", hashMap.get("key"));
    

    检查是否正确

    Boolean b= hashMap.get("key");
    
        if(b==true){
        //Your Toast Message
        }
    

    试试这个,让我知道它是否适合你。

    【讨论】:

    • 我上面已经指定了,这个基本的方式说明这个方法是未解决的。
    • 试一试,然后说出哪一行表明你无法再爱
    • 我又试了一次,它说“无法解析方法putExtra(String,Map&lt;String,Boolean&gt;);
    • 检查我添加的更新答案(Serializable)
    • 我的错!...我没有在 Hashmap 之前添加“(Serializable)”..它不再给出错误..一旦它起作用就会接受你的答案..谢谢!
    【解决方案4】:

    尝试使用单例类通过setter方法存储hashmap,并使用getter方法在其他活动上获取它。

    【讨论】:

      猜你喜欢
      • 2011-04-07
      • 1970-01-01
      • 1970-01-01
      • 2011-11-24
      • 1970-01-01
      • 2018-10-02
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      相关资源
      最近更新 更多