Android,如何在`BaseAdapter`中使用Map?

时间:2016-12-30 08:12:18

标签: java android baseadapter

我想在Android中使用动态对象获取json。所以我使用Map<>来获取json。我想在BaseAdapter内使用Map。我收到以下错误

Incompatible types.Required:com.example.EffectList. Found:java.util.Map< java.lang.String,java.util.List< com.example.EffectList >>

这是我的BaseAdapter代码:

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 Map<String, List<EffectList>> getItem(int position) {
      return contactList.get(position);
  }


  @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);   // COMPILE TIME ERROR HERE



   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);
      }
  }
 }

我的代码中出了什么问题?

1 个答案:

答案 0 :(得分:0)

非常简单,您的getItem方法返回Map<String, List<EffectList>>类型的对象,您无法将此类型直接转换为EffectList

获取EffectList项目需要这样的东西:

EffectList item = getItem(position).get("somekey").get(0);

<强>解决方案

  • Map中获取一些内容,然后从List中获取内容 在Map
  • 修改BaseAdapter getItem方法以返回EffectList类型的对象,并修改您的getCount方法以返回正确的项目数;

一些附注:

  • 我建议您重新考虑并重新设计数据结构,是否真的需要List包含Map的{​​{1}}?
  • 我个人不会使用List来支持MapAdapter中的项目顺序(通常)未定义(除非您使用Map实施订单已定义);