我需要在Android Studio中创建一个listView,它包含一个文本和一个与该文本对应的图像。我知道我必须制作自己的自定义适配器,但是我不知道如何使用不仅仅是每行中的文本视图。适配器将接收文本的字符串数组和与图片对应的URL的另一个字符串数组。如何创建此适配器来执行此操作。我最喜欢使用毕加索,但如果你有任何其他建议也可以。
class MyAdapter extends ArrayAdapter <String> {
Context context;
String[] descriptionArray;
String[] url;
MyAdapter (Context c, String[] importerArray,String[] url) {
super(c, R.layout.content_orders,R.id.textView2, importerArray);
this.context =c;
this.descriptionArray = importerArray;
this.url = url;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.content_orders, list, false);
ImageView myImage = (ImageView) row.findViewById(R.id.imageView2);
TextView description = (TextView) row.findViewById(R.id.textView2);
description.setText(descriptionArray[position]);
Picasso.with(OrdersActivity.this).load(url[position]).into(myImage);
return convertView;
}
答案 0 :(得分:1)
使用BaseAdapter和Bean
public class MyAdapter extends BaseAdapter {
private Context mContext;
private List<Bean> mList;
public MyAdapter(Context context,List<Bean> list){
mContext=context;
mList=list;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
//use converview recycle
if(convertView==null){
holder=new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_demo, parent, false);
holder.textView= (TextView) convertView.findViewById(R.id.textview);
holder.imageView= (ImageView) convertView.findViewById(R.id.imageview);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
//set text and url
holder.textView.setText(mList.get(position).getText());
Picasso.with(mContext).load(mList.get(position).getUrl()).into(holder.imageView);
return convertView;
}
class ViewHolder{
TextView textView;
ImageView imageView;
}
}
添加类Bean
public class Bean {
String text;
String url;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
MainActivity:
ArrayList<Bean> list=new ArrayList<>();
Bean bean1=new Bean;
bean1.setText("YOUR TEXT");
bean1.setUrl("IMAGE URL");
//and bean2……3……4……
list.add(bean1);
//add(bean2)…………
MyAdapter adapter =new MyAdapter(this,list);
listview.setAdapter(adapter);