Home > front end >  Error: no suitable method found for add(int,int)
Error: no suitable method found for add(int,int)

Time:05-15

I need to create an ArrayList with an id and value as resource so that I can know in the future which id the given icon is under. Unfortunately, with my solution I run into a problem, how to solve it?

Im giving error:

Error: no suitable method found for add(int,int) waypointsIcons.add(0, R.drawable.ic_point_default)

public class WaypointsIcons {

    public class IntValues {
        public int id;
        public int icon;
    }

    ArrayList<IntValues> waypointsIcons = new ArrayList<>();

    public ArrayList<IntValues> getWapointsIcons() {

        waypointsIcons.add(0, R.drawable.ic_point_default);

        return waypointsIcons;
    }
}

public class ImageSpinner extends ArrayAdapter<Integer> {
    private ArrayList<WaypointsIcons.IntValues> images;

    public ImageSpinner(Context context, ArrayList<WaypointsIcons.IntValues> images) {
        super(context, android.R.layout.simple_spinner_item, images);
        this.images = images;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getImageForPosition(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getImageForPosition(position);
    }

    private View getImageForPosition(int position) {
        ImageView imageView = new ImageView(getContext());
        imageView.setBackgroundResource(images.get(position).icon);
        imageView.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return imageView;
    }

}

ArrayList<WaypointsIcons.IntValues> waypointsIcons = new WaypointsIcons().getWapointsIcons();
ImageSpinner adapter = new ImageSpinner(context, waypointsIcons);
spinnerIconWaypoint.setAdapter(adapter);

CodePudding user response:

within getWapointsIcons , you wanted to add IntValues into ArrayList. You need to do something like this in getWapointsIcons :

IntValues values=new IntValues(0, R.drawable.ic_point_default);
waypointsIcons.add(values);
return waypointsIcons;

ArrayList / List won't access 2 arguments inside add

Also, to support above code, you have to add Constructor into IntValues class:

public class IntValues {
        public int id;
        public int icon;
   public IntValues(int id, int icon){
      this.id=id;
      this.icon=icon;
   }
 }
  • Related