gpt4 book ai didi

java - 可序列化对象到Hashmap的Arraylist

转载 作者:行者123 更新时间:2023-12-05 00:17:41 25 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,在 ListView 中显示联系人的所有姓名。

点击联系人 ListView ,我需要在另一个 ListView 中显示该联系人的相应号码。

这里的问题是,我 bundle 了所有数字并向其发送了另一个 Activity ,并且也收到了该 bundle ,但收到的 bundle 是可序列化对象。

因此,要在Listview中显示,我需要将其转换为Hashmap的Arraylist。我怎样才能做到这一点?

这是代码:

发送 Activity :

HashMap<String, String> Numbermap = new HashMap<String, String>();

Numbermap.put("numb",number);
Numbermap.put("type",type);

NumberList.add(Numbermap);

Intent intent = new Intent (ContactNames.this,ContactsPhoneNumbers.class);

intent.putExtra("map", NumberList);


startActivity(intent);

接收 Activity :

    Serializable Numbermap = new HashMap<String, String>();

// contNum = (TextView)findViewById(R.id.ContactNumber);

Bundle bundle = getIntent().getExtras();

Intent intent = getIntent();

if(bundle!=null)
{
Numbermap = bundle.getSerializable("map");
}

最佳答案

你用键“map”放入 Intent ,对象NumberList我认为它是ArrayList的一个实例。但是在您的 Activity ContactsPhoneNumbers 中,您尝试使用键“map”获取 HashMap 的实例,这是不正确的。

这是你应该做的:

ArrayList<HashMap<String, String>> numberList = null;
Bundle bundle = getIntent().getExtras();
Intent intent = getIntent();
if(bundle!=null)
{
numberList = (ArrayList<HashMap<String, String>>) bundle.getSerializable("map");
}

关于java - 可序列化对象到Hashmap的Arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17233566/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com