如何使用来自Arraylist <hashmap <string,string>&gt;的bundle使用putserializable传递valus

时间:2015-11-02 09:05:44

标签: java android arraylist hashmap serializable

我使用以下命令创建了一个列表,它从具有getter和setter的模型类中获取值。

    int k = model.getChildren().size();
    for(int i=0;i < k;i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        galleryChildModel = model.getChildren().get(i);
        map.put("caption", galleryChildModel.getImagecaption());
        map.put("imageurl", galleryChildModel.getImageurl());
        list.add(map);
    }

然后使用此setOnItemClickListener

将此值传递给下一个片段
       gridchild.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Fragment fragment = new GalleryDetailFragment();
            Bundle bundle = new Bundle();
            bundle.putSerializable("object", list.get(position));
            fragment.setArguments(bundle);
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container_body, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });

我使用

获取相应片段中的值
   Bundle bundle = this.getArguments();

    if (bundle != null) {
          ArrayList<HashMap<String, String>> maps = (ArrayList<HashMap<String, String>>) bundle.get("object"); // here i cannot cast the value to hashmap or any list. im really stuck here can anyone help me out with this
    }

im im get的值就是这样

Bundle [{object = [{ IMAGEURL = http://futhead.cursecdn.com/static/img/15/players/20801.png,caption=Ronaldo}]}]

如何从中获取数据并设置为textview和imageview ....

1 个答案:

答案 0 :(得分:1)

bundle.putSerializable("object", list.get(position));

您的Serializable存储在&#34;对象&#34;是一个HashMap而不是List,所以你应该转换为Hashmap:

HashMap<String,String> map = (HashMap<String, String>) bundle.getSerializable("object");