将Hashmap通过Android Intent传递

4
在我的某个活动中有一个HashMap(String,HashMap(String,Object))。我该如何通过意图(Intent)将此HashMap发送到另一个活动中呢?
如何将此HashMap添加到Intent额外数据中?

1
你可以在这里找到答案:https://dev59.com/7m445IYBdhLWcg3wM3Xx#4992465 - Ran Hassid
这里的评论和答案向您展示了如何通过Intent传递HashMap,但您可能想借此机会问问自己是否可以更好地设计您的应用程序。在任何时候,“需要”在活动之间传递Serializable时,我认为这表明您可能拥有一个薄弱的“模型”,或者根本没有。非平凡的应用程序应避免仅从Activity到Activity传递数据,并实现一个与UI分离的模型,然后将该模型注入到需要的Activity中。另请参见MVC或MVP设计模式。 - GreyBeardedGeek
3个回答

3
发送 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");

2
你需要使用putSerializable()而不是putExtra() - https://dev59.com/Umgu5IYBdhLWcg3wRlBh - Vaiden

0

对于您的问题可能有多种方法,每种方法都取决于您在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"));

对于复杂的数据类型,可以考虑使用 JSON 库,如 GSON,或使用 Parcelable 接口。


1
你可以直接传递HashMap,而无需将其解析为JSON。HashMap默认实现了Serializable接口,因此你可以直接使用:intent.putExtra("map", HashMap<>) 来传递它,而要读取它,则可以使用getIntent().getSerializableExtra("map")。 - Ran Hassid
我更新了我的回答。 - Nayan Srivastava

0

你好,你可以使用 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(MIT许可证)
  • SerializableParcelable Generator(MIT许可证)
  • Parcelable Code Generator(适用于Kotlin)(Apache许可证2.0)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接