【问题标题】:Android, how to use a Map in a `BaseAdapter`?Android,如何在`BaseAdapter`中使用地图?
【发布时间】:2016-12-30 08:12:18
【问题描述】:

我正在尝试在 Android 中获取带有动态对象的 json。所以我使用Map<> 来获取json。我想在BaseAdapter 中使用地图。我收到以下错误

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

我的代码有什么问题?

【问题讨论】:

    标签: java android baseadapter


    【解决方案1】:

    这很简单,您的getItem 方法返回Map&lt;String, List&lt;EffectList&gt;&gt; 类型的对象,您不能将此类型直接转换为EffectList

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

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

    解决方案

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

    一些旁注:

    • 我建议你重新思考和重新设计你的数据结构,你真的需要一个包含MapList 包含一个List 吗?
    • 我个人不会使用Map 来支持AdapterMap 中的项目订单(通常)未定义(除非您在定义订单的地方使用Map 实现);李>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-20
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多