我尝试过glide ...but unable to handel it..."https://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/“在本教程中他们使用了一个图像类。它是必要的,还是可以用Glide或picaso来解决?我是android新手,请帮帮忙。我的主要活动
String name = persons.getString("name");
String skills = persons.getString("skills");
String image = persons.getString("image");
// tmp hash map for single person
HashMap<String, String> person = new HashMap<>();
// adding each child node to HashMap key => value
person.put("name", name);
person.put("skills", skills);
/* Glide.with(getApplicationContext ())
.load(image)
.into();*/
// adding person toperson list
personList.add(person);
ListAdapter adapter = new SimpleAdapter (
MainActivity.this, personList,
R.layout.persons_list, new String[]{"name", "skills"}, new int[]{R.id.name,
R.id.skills});发布于 2018-02-09 19:35:10
首先,Glide或Picasso是在列表视图和回收视图中处理图像的简单方法。您需要有一个自定义适配器,因为您的每个人都有一个要加载的图像。
创建一个如下的模型类;
public class Person {
private String name;
private String skills;
private String image;
// getters and setters
}然后,您需要一个自定义适配器类,如下所示;
public class PersonAdapter extends ArrayAdapter<Person> {
private Context mContext;
private List<Person> mData;
public PersonAdapter(@NonNull Context context, @NonNull List<Person> data) {
super(context, 0, data);
mContext = context;
mData = data;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder holder;
if(convertView != null) {
holder = (ViewHolder) convertView.getTag();
} else {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.person_list_item, null);
holder = new ViewHolder();
holder.image = convertView.findViewById(R.id.imgPerson);
holder.name = convertView.findViewById(R.id.tvName);
holder.skills = convertView.findViewById(R.id.tvSkills);
convertView.setTag(holder);
}
Person person = getItem(position);
holder.name.setText(person.getName());
holder.skills.setText(person.getSkills());
// set image with picasso
Picasso.with(mContext).load(person.getImage()).into(holder.image);
return convertView;
}
static class ViewHolder {
ImageView image;
TextView name;
TextView skills;
}
}使用ViewHolder保存数据总是很好的,因为这将使内存处理变得容易
然后,在活动中创建适配器的实例并将其附加到列表视图
PersonAdapter adapter = new PersonAdapter(MainActivity.this, persons);
mListView.setAdapter(adapter);我用过毕加索,你也可以用Glide :)
发布于 2018-02-09 19:35:07
为person创建一个pojo类,然后可以使用Gson进行数据解析,如下所示:
Gson gson = new Gson();
Persons persons = gson.fromJson(productList, Persons.class);然后可以像这样使用glide:
requestOptions = new RequestOptions();
requestOptions
.placeholder(placeholder)
.error(error)
.dontTransform()
.dontAnimate();
Glide.with(mContext).load(url).apply(requestOptions).into(imageView);https://stackoverflow.com/questions/48704174
复制相似问题