【问题标题】:Passing Hashmap through android intents [duplicate]通过android意图传递Hashmap [重复]
【发布时间】:2016-12-05 17:24:38
【问题描述】:

我的一项活动中有一个 HashMap(String,HashMap(String,Object))。如何通过意图将此 HashMap 发送到另一个活动

如何将此 HashMap 添加到 Intent Extras?

【问题讨论】:

  • 你可以在这里找到答案:stackoverflow.com/a/4992465/4287861
  • 这里的 cmets 和答案向您展示了如何通过 Intent 传递 HashMap,但您可能想借此机会问问自己是否可以以更好的方式设计您的应用程序。每当您“需要”在活动之间传递 Serializable 时,恕我直言,这表明您可能有一个弱“模型”,或者根本没有。重要的应用程序应避免仅将数据从 Activity 传递到 Activity,并实现与 UI 分离的模型,然后在需要时将该模型注入到 Activity 中。另请参阅 MVC 或 MVP 设计模式。

标签: android android-intent hashmap


【解决方案1】:

您好,您可以使用 Parcelable :

这样写类:

public class Person implements Parcelable {
String name;
int age;
Date brithDate;

public Person(String name, int age, Date brithDate) {
    this.name = name;
    this.age = age;
    this.brithDate = brithDate;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.name);
    dest.writeInt(this.age);
    dest.writeLong(brithDate != null ? brithDate.getTime() : -1);
}

protected Person(Parcel in) {
    this.name = in.readString();
    this.age = in.readInt();
    long tmpbrithDate = in.readLong();
    this.brithDate = tmpbrithDate == -1 ? null : new Date(tmpbrithDate);
}

public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {
    public Persona createFromParcel(Parcel source) {
        return new Person(source);
    }

    public Person[] newArray(int size) {
        return new Person[size];
    }
};

添加额外的:

intent.putExtra("person", new Person("Ahmad",34,date));

获取:

Bundle data = getIntent().getExtras();
Person person= (Person) data.getParcelable("person");

或者你可以复制这个站点的类并转换为 Parcelable 类:

http://www.parcelabler.com/

或者你可以使用这个库 hrisey

https://github.com/mg6maciej/hrisey/wiki/Parcelable#details

或者您可以使用 Android Studio 的插件:

  • Android Parcelable code generator(Apache 许可证 2.0)
  • Auto Parcel(麻省理工学院许可证)
  • SerializableParcelable 生成器(MIT 许可证)
  • Parcelable 代码生成器(用于 Kotlin)(Apache 许可证 2.0)

【讨论】:

    【解决方案2】:

    发送HashMap

    HashMap<String, Object> hashmapobject =new HashMap<>();
    HashMap<String, Object> newhashMap=new HashMap<>();
    hashmapobject.put("key",newhashMap);
    
    Intent intent = new Intent(SenderActivity.this, NextActivity.class);
    intent.putExtra("hashMapKey", hashmapobject);
    startActivity(intent);
    

    接收HashMap

    Intent intent = getIntent();    
    HashMap<String, Object> hashMapObject = (HashMap<String, Object>)       intent.getSerializableExtra("hashMapKey");
    HashMap<String, Object> newhashMap=(HashMap<String, Object>)hashMapObject.get("key");
    

    【讨论】:

    【解决方案3】:

    可能有多种方法可以解决您的问题,每种方法都取决于您在 map 中存储的数据类型。最简单的方法是使用 json.org 中的 JSONObject 将其作为字符串传递,并在接收时将其转换回 JSON。 JSONObject 在内部使用 LinkedHashMap,因此您将能够使用HashMap 的功能。

    JSONObject obj=new JSONObject();
    obj.put("key1", "value1");
    obj.put("key2", "value2");
    obj.put("key3", "value3");
    .
    .
    .
    obj.put("keyN", "valueN");
    
    intent.putExtra("map", obj.toString());
    

    接收时

    JSONObject obj=new JSONObject(getIntent().getStringExtra("map"));
    

    对于复杂的数据类型,请尝试考虑使用 GSON 等 JSON 库或使用 Parcelable 接口。

    【讨论】:

    • 您可以直接传递 HashMap 而无需将其解析为 JSON。HashMap 默认实现 Serializable 接口,因此您可以使用:intent.putExtra("map",HashMap) 和阅读:getIntent().getSerializableExtra("map")
    • 我更新了我的答案 Ran
    猜你喜欢
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 2012-07-25
    • 2012-11-22
    • 1970-01-01
    • 2014-10-19
    相关资源
    最近更新 更多