问题描述
所以我尝试将图像添加到我的列表视图,但它被证明是一种痛苦。我想用毕加索,因为图像是通过HTTP请求。
So im trying to add images to a List View of mine, but it is proving to be a pain. I would like to use Picasso, as the images are requested via HTTP.
我目前只能String对象添加到列表视图TextViews。那是比较容易的部分。不过,我在想图像的裂纹如下:
I can currently only add String objects to TextViews in the List View. Thats the easy part. However, I had a crack at trying images as follows;
MainActivity.java
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
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;
}
}
帮助将在很大程度上AP preciated。
Help will be largely appreciated.
在哪里我会错呢?我迷路了,
某处的辛酸
Where did I go wrong? I lost my way,somewhere along in the bitterness
推荐答案
因此,一个同事解决了,我通过修改我的CustomListView如下:
So a colleague solved it for me by modifying my CustomListView as follows.
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;
}
}
我把它从我的主要活动是如此;
I call it from my Main Activity as so;
ListAdapter adapter = new CustomList(MainActivity.this, championList, R.layout.list_view_row, new String[] {}, new int[] {});
这篇关于毕加索集成到列表视图与具有一个HashMap自定义适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!