List<><>>> in BaseAdapter ,Android

标签: json Java Android
发布时间: 2017/3/4 2:32:10
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

以前这有问任何人。对不起,我是新来 Android ,这些问题的答案并没有帮助我。

我试图解析以下的 json 使用改造, Android 。在下面的 json,对象41动态的。所以我用地图来获取的数据。

 {  
"effect_list":[  
  {  
     "4":[  
        {  
           "effects_id":"18",
           "effects_name":"Band 1"
        },
        {  
           "effects_id":"19",
           "effects_name":"Band 2"
        }
     ],
     "1":[  
        {  
           "effects_id":"1",
           "effects_name":"Background Blur"
        },
        {  
           "effects_id":"4",
           "effects_name":"Blemish Removal"
        }
     ]
  }
]
}

现在我想显示里面的 json 数据 listview 使用 BaseAdapter

我试过了

MyContactAdapter2.java

public class MyContactAdapter2 extends BaseAdapter {
    List<Map<String, List<EffectList>>> contactList;
    Context context;
    private LayoutInflater mInflater;

    // Constructors
    public MyContactAdapter2(Context context, List<Map<String, List<EffectList>>> objects) {

        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        contactList = objects;
    }

    @Override
    public int getCount() {
        return contactList.size();
    }

    @Override
    public EffectList getItem(int position) {
        return (EffectList) contactList.get(position);  <----- ERROR HERE DURING RUNTIME
    }


    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final MyContactAdapter2.ViewHolder vh;
        if (convertView == null) {
            View view = mInflater.inflate(R.layout.get_layout_row_view, parent, false);
            vh = MyContactAdapter2.ViewHolder.create((RelativeLayout) view);
            view.setTag(vh);
        } else {
            vh = (MyContactAdapter2.ViewHolder) convertView.getTag();
        }

        EffectList item = getItem(position);  <----- ERROR HERE DURING RUNTIME


        vh.textViewName.setText(item.getEffectsId());
        vh.textViewEmail.setText(item.getEffectsName());

        return vh.rootView;
    }

    private static class ViewHolder {
        public final RelativeLayout rootView;
        public final ImageView imageView;
        public final TextView textViewName;
        public final TextView textViewEmail;

        private ViewHolder(RelativeLayout rootView, ImageView imageView, TextView textViewName, TextView textViewEmail) {
            this.rootView = rootView;
            this.imageView = imageView;
            this.textViewName = textViewName;
            this.textViewEmail = textViewEmail;
        }

        public static MyContactAdapter2.ViewHolder create(RelativeLayout rootView) {
            ImageView imageView = (ImageView) rootView.findViewById(R.id.imageView);
            TextView textViewName = (TextView) rootView.findViewById(R.id.textViewName);
            TextView textViewEmail = (TextView) rootView.findViewById(R.id.textViewEmail);
            return new MyContactAdapter2.ViewHolder(rootView, imageView, textViewName, textViewEmail);
        }
    }
}

我在运行时得到以下的错误。

java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap 不能转换为我班

在做什么在这里错了?

解决方法 1:

返回 Map<String,List<EffectList>>getItem 方法因为 contactList 作为项目包含映射︰

@Override
    public Map<String,List<EffectList>> getItem(int position) {
        return  contactList.get(position);   
    }

getView 使用键从地图访问值︰

   Map<String,List<EffectList>> map=getItem(position);
   List<EffectList> effectList = map.get("KEY_NAME");
   // get item from effectList using for loop
官方微信
官方QQ群
31647020