在Android中从一个活动传递HashMap到另一个活动

3
我正在编写一个Android应用程序来显示附近的位置。我将它们存储在哈希映射列表中,就像这样:
List<HashMap<String, String>> places = null;

在第一个活动中,我在地图上显示了地点,并希望将它们传递到另一个活动中,在listView中列出。

在第一个活动中,我有一个按钮可以直接跳转到列表活动,例如:

public void list_airports(View v)
        {
            Intent intent;
            switch (v.getId()) {

            case R.id.list_items:
                intent = new Intent(getApplicationContext(), List_airports.class);
                intent.putExtra("places",places);
                startActivity(intent);
                break;

            }
            }

在下一个活动中,我做了这个:
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_airports);
        Bundle extras = getIntent().getExtras();
        String[] places=extras.getStringArray(Intent.EXTRA_TEXT);

但是putExtra方法不接受List<HashMap<String, String>>


你说得对,putExtra方法不接受List<HashMap<String,String>>...,那么问题是什么? - Scott
@Scott,问题是:有没有一种方法可以传递 'List<HashMap<String, String>>'? - roa.tah
3个回答

6

1
使用putExtra(String, Serializable)将其传递给Intent,使用getSerializableExtra(String)方法检索它。
此外,使用ArrayList代替List

0

什么是可序列化

可序列化截取当前类的屏幕截图。

1. 从上下文类传递意图到新类

HashMap<String, String> hashMap= adapter.getItem(position);
Intent intent = new Intent(OneActivity.this, SecondActivity.class);
intent.putExtra("key", value);
startActivity(intent);

2.将哈希映射检索到其他类

Bundle b=getIntent.getExtra();
 if(b!=null)
   {
     yourhasmaplist=b.getSerializableExtra("key"); 
   }

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