所以我试图将图像添加到我的列表视图中,但事实证明这是一种痛苦。我想使用Picasso,因为图像是通过HTTP请求的。
我目前只能在列表视图中将String对象添加到TextViews。这很容易。但是,我尝试按照以下方式尝试图像;
MainActivity.java
...
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", name);
map.put("title", title);
map.put("image_url",IMAGE_RELATIVE_URL+image_url+".png");
championList.add(map);
// Get ListView object from xml
final ListView listView = (ListView) findViewById(R.id.listView);
ListAdapter adapter = new SimpleAdapter(MainActivity.this, championList, R.layout.list_view_row, new String[]{"name","title"}, new int[]{R.id.name, R.id.title});
//ListAdapter adapter = new CustomList(MainActivity.this,new String[]{"name"}, new String[]{"title"}, new String[]{"image_url"})
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HashMap<String,String> map = (HashMap<String,String>)listView.getItemAtPosition(position);
String name= map.get("name");
String image= map.get("image_url");
Log.i("summoner",name+image);
//display in long period of time
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
}
});
...
CustomList.java
public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final String[] name;
private final String[] title;
private final String[] image_url;
public CustomList(Activity context, String[] name, String[] title, String[] image_url) {
super(context, R.layout.list_view_row, name);
this.context = context;
this.name = name;
this.title = title;
this.image_url = image_url;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_view_row, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.name);
txtTitle.setText(name[position]);
TextView txtName = (TextView) rowView.findViewById(R.id.title);
txtName.setText(title[position]);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
Picasso.with(this.context).load(image_url[position]).into(imageView);
return rowView;
}
}
将非常感谢帮助。
我哪里出错了?我迷路了, 在苦涩的某个地方
答案 0 :(得分:0)
修改适配器以扩展BaseAdapter。 使用Picasso.with(this.context).load(championList.get(position).get(“image_url”))。into(imageView)加载图片。
答案 1 :(得分:-1)
所以一位同事通过修改我的CustomListView为我解决了这个问题。
public class CustomList extends SimpleAdapter {
private Context mContext;
public LayoutInflater inflater=null;
public CustomList(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
mContext = context;
inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_view_row, null);
HashMap<String, Object> data = (HashMap<String, Object>) getItem(position);
TextView text = (TextView)vi.findViewById(R.id.name);
String name = (String) data.get("name");
text.setText(name);
ImageView image=(ImageView)vi.findViewById(R.id.img);
String image_url = (String) data.get("image_url");
Picasso.with(mContext).load(image_url).into(image);
return vi;
}
}
我从我的主要活动中调用它;
ListAdapter adapter = new CustomList(MainActivity.this, championList, R.layout.list_view_row, new String[] {}, new int[] {});