我想在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);
}
}
}
我的代码中出了什么问题?
答案 0 :(得分:0)
非常简单,您的getItem
方法返回Map<String, List<EffectList>>
类型的对象,您无法将此类型直接转换为EffectList
。
获取EffectList项目需要这样的东西:
EffectList item = getItem(position).get("somekey").get(0);
<强>解决方案强>
Map
中获取一些内容,然后从List
中获取内容
在Map
。getItem
方法以返回EffectList
类型的对象,并修改您的getCount
方法以返回正确的项目数; 一些附注:
List
包含Map
的{{1}}?List
来支持Map
,Adapter
中的项目顺序(通常)未定义(除非您使用Map
实施订单已定义);