0

I need to make my adapter display images so i made an array of integers and put in it an id of images, but when i sent it to the array adapter constructor it get me an error because The type Integer. How could i solve this problem?

the error appear Here super(context,resource, objects);

My GridViewAdapter class:

package com.example.hima.moviesapp;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import java.util.ArrayList;


/**
* Created by Hima on 3/23/2016.
 */
public class GridViewAdapter extends ArrayAdapter<String> {
   private Context mContext;
   private int layoutResourceId;
   private ArrayList<Integer > mGridImages = new ArrayList<Integer>();
 public GridViewAdapter(Context context, int resource, ArrayList<Integer> objects) {
     //the error apear here!!
     super(context,resource, objects);
     this.mContext=context;
     this.layoutResourceId=resource;
     this.mGridImages=objects;
 }

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    ImageView image;
    if(row == null){
        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        image= (ImageView)row.findViewById(R.id.movieImage);
        row.setTag(image);
    }
    else{
        image = (ImageView)row.getTag();
    }
    int temp = mGridImages.get(position);
    image.setImageResource(temp);
    return row;
}
}

MyMainFragment class:

package com.example.hima.moviesapp;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import java.util.ArrayList;

/**
 * A placeholder fragment containing a simple view.
 */
  public class GridViewFragment extends Fragment {
  int[] DummyImages = {R.drawable.first,R.drawable.second,R.drawable.third,R.drawable.fourth,
                    R.drawable.fifth,R.drawable.sixth};
public GridViewFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

  View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    ArrayList<Integer>DummyPosters= new ArrayList<>();
    for(int i=0;i<DummyImages.length;i++) {
        DummyPosters.add(DummyImages[i]);
    }
    GridView gridView = (GridView) rootView.findViewById(R.id.gridView);
    gridView.setAdapter(new GridViewAdapter(getActivity(),R.id.movieImage,DummyPosters));

    return rootView;
}
}
1
  • Change type of ArrayAdapter<String> to ArrayAdapter<Integer> Commented Mar 23, 2016 at 16:59

2 Answers 2

1

Your adapter is expecting array of Strings, that's why it crashes. It does so because you pass String as a generic type in your adapter

public class GridViewAdapter extends ArrayAdapter<String>

To fix that change your adapter to extend ArrayAdapter<Integer>

Sign up to request clarification or add additional context in comments.

Comments

1

Try to change

public class GridViewAdapter extends ArrayAdapter<String> {

to

public class GridViewAdapter extends ArrayAdapter<Integer> {

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.