从自定义适配器获取 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 字符串。
总是收到
的错误
"Incompatible types"(需要字符串,但找到的是对象)
这条线
String url = ((Map)getItem(position)).get("pic");
使用
private ArrayList<Map<String, String>> data;
Map<String, String> category = new Map<String, String>();
category = data.get(position);
String url=category.get("pic");
只需更改此
String url = ((Map)getItem(position)).get("pic");
至
String url = list.get(position).get("pic");
也在这里
Context context2;
List<HashMap<String, String>> list;
public ExtendedSimpleAdapter(Context context, List<HashMap<String, String>> data, int resource, String[] from, int[] to){
super(context, data, resource, from, to);
context2=context;
this.list = data;
}
您要投射的对象应该具有准确的类型。转换为通用 Map 不会强制执行编译时检查,而泛型是为此而设计的。
String url = ((Map<String, String>)getItem(position)).get("pic");
我可以知道如何从自定义适配器获取 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 字符串。 总是收到
的错误"Incompatible types"(需要字符串,但找到的是对象)
这条线
String url = ((Map)getItem(position)).get("pic");
使用
private ArrayList<Map<String, String>> data;
Map<String, String> category = new Map<String, String>();
category = data.get(position);
String url=category.get("pic");
只需更改此
String url = ((Map)getItem(position)).get("pic");
至
String url = list.get(position).get("pic");
也在这里
Context context2;
List<HashMap<String, String>> list;
public ExtendedSimpleAdapter(Context context, List<HashMap<String, String>> data, int resource, String[] from, int[] to){
super(context, data, resource, from, to);
context2=context;
this.list = data;
}
您要投射的对象应该具有准确的类型。转换为通用 Map 不会强制执行编译时检查,而泛型是为此而设计的。
String url = ((Map<String, String>)getItem(position)).get("pic");