从自定义适配器获取Hashmap项(Get Hashmap item from custom adapter)

编程入门 行业动态 更新时间:2024-10-24 19:14:15
自定义适配器获取Hashmap项(Get Hashmap item from custom adapter)

我是否知道如何从自定义适配器获取HashMap项目

码:

ArrayList<HashMap<String, String>> feeds_List; ... ... HashMap<String, String> map = new HashMap<String, String>(); // adding each child node to HashMap key => value map.put("username", usr_name); map.put("datetime", feed_date); map.put("pts", point_txt); map.put("pic", pic); // adding HashList to ArrayList feeds_List.add(map); ListAdapter adapter = new ExtendedSimpleAdapter( getActivity(), feeds_List, R.layout.feed_item, new String[]{"username", "datetime", "pts"}, new int[]{R.id.usr_name, R.id.feed_time, R.id.no_stamp}); setListAdapter(adapter);

我的定制适配器:

public class ExtendedSimpleAdapter extends SimpleAdapter{ Context context2; public ExtendedSimpleAdapter(Context context, List<? extends Map<String, String>> data, int resource, String[] from, int[] to){ super(context, data, resource, from, to); context2=context; } public View getView(int position, View convertView, ViewGroup parent){ // here you let SimpleAdapter built the view normally. View v = super.getView(position, convertView, parent); // Then we get reference for Picasso ImageView img = (ImageView) v.getTag(); if(img == null){ img = (ImageView) v.findViewById(R.id.usr_pic); v.setTag(img); // <<< THIS LINE !!!! } // get the url from the data you passed to the `Map` String TAG_IMAGE="pic"; String url = ((Map)getItem(position)).get("pic"); // do Picasso // maybe you could do that by using many ways to start Picasso.with(context2) .load(url) //.resize(100, 100) .into(img); // return the view return v; } }

这是问题所在:

我无法在自定义适配器中获取url字符串。 总是得到错误

“不兼容的类型”(需要String,但找到的是对象)

这条线

String url = ((Map)getItem(position)).get("pic");

May i know how to get HashMap item from a custom adapter

Code:

ArrayList<HashMap<String, String>> feeds_List; ... ... HashMap<String, String> map = new HashMap<String, String>(); // adding each child node to HashMap key => value map.put("username", usr_name); map.put("datetime", feed_date); map.put("pts", point_txt); map.put("pic", pic); // adding HashList to ArrayList feeds_List.add(map); ListAdapter adapter = new ExtendedSimpleAdapter( getActivity(), feeds_List, R.layout.feed_item, new String[]{"username", "datetime", "pts"}, new int[]{R.id.usr_name, R.id.feed_time, R.id.no_stamp}); setListAdapter(adapter);

My Custom Adapter:

public class ExtendedSimpleAdapter extends SimpleAdapter{ Context context2; public ExtendedSimpleAdapter(Context context, List<? extends Map<String, String>> data, int resource, String[] from, int[] to){ super(context, data, resource, from, to); context2=context; } public View getView(int position, View convertView, ViewGroup parent){ // here you let SimpleAdapter built the view normally. View v = super.getView(position, convertView, parent); // Then we get reference for Picasso ImageView img = (ImageView) v.getTag(); if(img == null){ img = (ImageView) v.findViewById(R.id.usr_pic); v.setTag(img); // <<< THIS LINE !!!! } // get the url from the data you passed to the `Map` String TAG_IMAGE="pic"; String url = ((Map)getItem(position)).get("pic"); // do Picasso // maybe you could do that by using many ways to start Picasso.with(context2) .load(url) //.resize(100, 100) .into(img); // return the view return v; } }

Here is the problem:

I cannot get the url string in the custom adapter. always getting errors of

"Incompatible types"(needed String, but found is object)

for this line

String url = ((Map)getItem(position)).get("pic");

最满意答案

您要反对的对象应该具有确切的类型。 转换为通用Map不会强制执行编译时检查,这是泛型的意思。

String url = ((Map<String, String>)getItem(position)).get("pic");

The object you are casting against, should have the exact type. Casting to the generic Map doesn't enforce the compile time check, that the generics are meant for.

String url = ((Map<String, String>)getItem(position)).get("pic");

更多推荐

本文发布于:2023-08-04 09:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1414067.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   适配器   Hashmap   adapter   custom

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!