【问题标题】:List<Map<String, List<>>> in BaseAdapter, AndroidAndroid BaseAdapter 中的 List<Map<String, List<>>>
【发布时间】:2016-12-30 05:29:34
【问题描述】:

之前有人问过这个问题。抱歉,我是 Android 新手,这些答案对我没有帮助。

我正在尝试使用改造,Android 解析以下 json。在下面的 json 中,对象 41Dynamic。所以我使用 Map 来获取数据。

 {  
"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"
        }
     ]
  }
]
}

现在我想使用 BaseAdapter 在列表视图中显示 json 数据。

我试过了

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 无法转换为我的班级

我在这里做错了什么?

【问题讨论】:

    标签: java android json retrofit baseadapter


    【解决方案1】:

    getItem 方法返回 Map&lt;String,List&lt;EffectList&gt;&gt;,因为 contactList 包含 Map 作为项目:

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

    getView 中使用键来访问 Map 中的值:

       Map<String,List<EffectList>> map=getItem(position);
       List<EffectList> effectList = map.get("KEY_NAME");
       // get item from effectList using for loop
    

    【讨论】:

    • 感谢您的响应。现在我收到以下错误不兼容的类型。必需:com.example.prakash.pix91.get.Templates.pixjson.EffectList 找到:java.util.Map> in EffectList item = getItem(position);
    猜你喜欢
    • 2018-09-22
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 2021-03-25
    • 2020-04-01
    • 2021-02-28
    • 2016-01-12
    • 1970-01-01
    相关资源
    最近更新 更多