public class Adapter extends ArrayAdapter {
private Context context;
private ArrayList<Item> items;
public Adapter(@NonNull Context context, ArrayList<Item> items) {
super(context, R.layout.listview_item);
this.context = context;
this.items = items;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = new ViewHolder();
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listview_item, parent, false);
viewHolder.imageView = convertView.findViewById(R.id.imageView);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.imageView.setImageBitmap(items.get(position).getImageView());
return convertView;
}
static class ViewHolder{
ImageView imageView;
}
}
在这里我要设置适配器
public void createList(ListView listView){
Adapter adapter = new Adapter(context,tanks.get(currentTank).getItems());
listView.setAdapter(adapter);
}
答案 0 :(得分:1)
public Adapter(@NonNull Context context, ArrayList<Item> items) {
super(context, R.layout.listview_item);
this.context = context;
this.items = items;
}
代替这个,试试这个:
public MyCustomListAdapter(Context context,int resource, ArrayList<Item> items)
{
super(context,resource,list);
this.context= context;
this.resource=resource;
this.list=list;
}
也在您的主类中编写以下代码:
Adapter adapter = new Adapter(context,R.layout.<Thename of your layout>,tanks.get(currentTank).getItems());