java.lang.ClassCastException : com. google.gson.internal.LinkedTreeMap 无法转换为 com.example

标签 java android baseadapter

我正在尝试使用retrofit解析json。我正在尝试使用BaseAdapter在listview中显示值。当我在 BaseAdapter 中使用 map 时,出现以下错误。

java.lang.ClassCastException:com.google.gson.internal.LinkedTreeMap 无法转换为 com.example.prakash.pix91.get.Templates.pixjson.EffectList(位于 com.example.prakash.pix91.get.Templates.multiple_array.MyContactAdapter2.getView(MyContactAdapter2.java:76)

)

我在这里做错了什么?

谢谢

MyContactAdapter2.java

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

    public MyContactAdapter2(Context context, List<Map<String, List<EffectList>>> objects) {
        this.context = context;
        this.mInflater = LayoutInflater.from(context);
        contactList = objects;
    }


    @Override
    public int getCount() {
        Integer s = contactList.size();
        Log.d("Total count ", s.toString());
        return contactList.size();
    }


    @Override
    public Object getItem(int position) {

        return contactList.get(position);
    }


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

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


        EffectList item = (EffectList) getItem(position);


        //   vh.textViewName.setText(item.getEffectsId());


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

        // Picasso.with(context).load(item.getProfilePic()).placeholder(R.mipmap.ic_launcher).error(R.mipmap.ic_launcher).into(vh.imageView);

        return vh1.rootView;
    }


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

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

        public static MyContactAdapter2.ViewHolder1 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.ViewHolder1(rootView, imageView, textViewName, textViewEmail);
        }
    }
}

最佳答案

问题出在这一行:

 EffectList item = (EffectList) getItem(position);

您的元素不是EffectList s,它们是 Map 的列表正如您在这里看到的:

List<Map<String, List<EffectList>>> contactList;

您已覆盖 getItem方法,它从列表中返回适当的 map 。因为你得到了 MapgetItem 返回调用,您需要重新考虑获取 EffectList 的逻辑摆脱它。

我不知道你想用 List<Map<String, List<EffectList>>> 这个复杂的结构做什么,但也许您只想拥有 List<EffectList

关于java.lang.ClassCastException : com. google.gson.internal.LinkedTreeMap 无法转换为 com.example,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41379287/

相关文章:

java - 获取以 Apache 速度显示在浏览器中的请求 URI

java - Android Baseadapter 填充 ListView

java - HttpClient - 如何设置指纹和_csrf?

Android在ImageView中绘制边框

android - 我应该使用 GCM 进行设备之间的实时通信吗?

php - GCM 服务器出现 401 错误

android - 尝试从 BaseAdapter 启动 Activity 时出现未知错误

Android,列表适配器在getView中返回错误的位置

java - 选项卡式 Activity 中 fragment 之间的交互

java - GSON:将 JSON 数据输入到 Java 类,其中类名存储在字符串中