列表<Map<String, List<>>> 在 BaseAdapter 中,Android
List<Map<String, List<>>> in BaseAdapter, Android
这个问题以前有人问过。抱歉,我是 Android 的新手,这些答案对我没有帮助。
我正在尝试使用改进 Android 来解析以下 json。在下面的 json 中,对象 4 和 1 是 Dynamic。所以我正在使用 Map 来获取数据。
{
"effect_list":[
{
"4":[
{
"effects_id":"18",
"effects_name":"Band 1"
},
{
"effects_id":"19",
"effects_name":"Band 2"
}
],
"1":[
{
"effects_id":"1",
"effects_name":"Background Blur"
},
{
"effects_id":"4",
"effects_name":"Blemish Removal"
}
]
}
]
}
现在我想使用 BaseAdapter 在列表视图中显示 json 数据。
我试过了
MyContactAdapter2.java
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 EffectList getItem(int position) {
return (EffectList) contactList.get(position); <----- ERROR HERE DURING RUNTIME
}
@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); <----- ERROR HERE DURING RUNTIME
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.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap 无法转换为我的 class
我做错了什么?
Return Map<String,List<EffectList>>
来自 getItem
方法,因为 contactList
包含 Map 作为项目:
@Override
public Map<String,List<EffectList>> getItem(int position) {
return contactList.get(position);
}
并且在 getView
中使用键访问 Map 中的值:
Map<String,List<EffectList>> map=getItem(position);
List<EffectList> effectList = map.get("KEY_NAME");
// get item from effectList using for loop
这个问题以前有人问过。抱歉,我是 Android 的新手,这些答案对我没有帮助。
我正在尝试使用改进 Android 来解析以下 json。在下面的 json 中,对象 4 和 1 是 Dynamic。所以我正在使用 Map 来获取数据。
{
"effect_list":[
{
"4":[
{
"effects_id":"18",
"effects_name":"Band 1"
},
{
"effects_id":"19",
"effects_name":"Band 2"
}
],
"1":[
{
"effects_id":"1",
"effects_name":"Background Blur"
},
{
"effects_id":"4",
"effects_name":"Blemish Removal"
}
]
}
]
}
现在我想使用 BaseAdapter 在列表视图中显示 json 数据。
我试过了
MyContactAdapter2.java
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 EffectList getItem(int position) {
return (EffectList) contactList.get(position); <----- ERROR HERE DURING RUNTIME
}
@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); <----- ERROR HERE DURING RUNTIME
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.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap 无法转换为我的 class
我做错了什么?
Return Map<String,List<EffectList>>
来自 getItem
方法,因为 contactList
包含 Map 作为项目:
@Override
public Map<String,List<EffectList>> getItem(int position) {
return contactList.get(position);
}
并且在 getView
中使用键访问 Map 中的值:
Map<String,List<EffectList>> map=getItem(position);
List<EffectList> effectList = map.get("KEY_NAME");
// get item from effectList using for loop