A
A
arlanchik52018-03-01 22:28:52
Java
arlanchik5, 2018-03-01 22:28:52

How to fix cannot be applied error?

Good evening, there was such a problem of type incompatibility with context.

public class FragmentMems extends Fragment {

    private GridView gridView;
    private GridViewAdapter gridViewAdap;
    String[] items = {
            "http://topmemas.top/img/img/1517414160.jpg",
            "http://topmemas.top/img/img/1518633309.jpg",
            "http://topmemas.top/img/img/1517859960.jpg",
            "http://topmemas.top/img/img/1517640360.jpg",
            "https://cdn.pastemagazine.com/www/system/images/photo_albums/memeish-top-gun/large/top-gun-meme-10.jpg?1384968217",
            "http://topmemas.top/img/img/1518633309.jpg",
            "http://topmemas.top/img/img/1518633309.jpg",
            "http://topmemas.top/img/img/1518633309.jpg",

    };



    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_user_mems, container, false);
        gridView = (GridView) rootView.findViewById(R.id.gridview);
        gridViewAdap = new GridViewAdapter(FragmentMems.this, items);
        gridView.setAdapter(gridViewAdap);
        return rootView;
    }

Error:(41, 56) error: incompatible types: FragmentMems cannot be converted to Context
}

public class GridViewAdapter extends BaseAdapter {
    private Context context;
    private String[] items;

    public GridViewAdapter(Context context, String[] items){
        super();
        this.context=context;
        this.items=items;
    }
    @Override
    public int getCount() {
        return items.length;
    }
    @Override
    public Object getItem(int position) {
        return items[position];
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertview, ViewGroup parent) {
        ImageView img;
        if (convertview == null){
            img = new ImageView(context);
            convertview = img;
            img.setPadding(5,5,5,5);

        }else {
            img =(ImageView)convertview;
        }

        Picasso.with(context).load(items[position]).placeholder(R.drawable.image)
                .resize(100,100)
                .into(img);
                return convertview;  } }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Gnoemes, 2018-03-02
@arlanchik5

gridViewAdap = new GridViewAdapter(FragmentMems.this, items);
Replace FragmentMems.this with getContext()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question