Search code examples
javaandroidbaseadapter

Android, how to use a Map in a `BaseAdapter`?


I am trying to get json with dynamic objects in Android. So I am using Map<> to get the json. I want to use the Map inside BaseAdapter. I am getting the following error

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

This is my BaseAdapter code:

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

what's wrong in my code?


Solution

  • Well its quite simple, your getItem method returns an object of the type Map<String, List<EffectList>>, you can not cast this type directly to an EffectList.

    Something like this is required to obtain an EffectList item:

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

    Solutions

    • Get something out of the Map and then out of the List which is inside the Map.
    • Modify your BaseAdapter getItem method to return an object of the EffectList type, and modify your getCount method to return te correct item count;

    A few side notes:

    • I suggest you rethink and redesign your data structure, do you really need a List which contains a Map which contains a List?
    • I would personally not use a Map to back a Adapter, the item order within a Map is (often) undefined (unless you use a Map implementation where the order is defined);