0

All my images are stored in drawable folder. I want to see a gallery view with some of the images.

The names of the images I want are in a String array.

Can I get the gallery view to show these images.

I found the code bellow, here the images are an Integer.

Can I use a String array?

public class ImageAdapter extends BaseAdapter {

    private Context context;

    public ImageAdapter(Context context){
        this.context = context;
    }

    public int getCount() {
        return pics.length;
    }

    public Object getItem(int arg0) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int arg0, View arg1, ViewGroup arg2) {
        ImageView iv = new ImageView(context);
        iv.setImageResource(pics);
        iv.setScaleType(ImageView.ScaleType.FIT_XY);
        iv.setLayoutParams(new Gallery.LayoutParams(150,120));
        iv.setBackgroundResource(imageBackground);
        return iv;
    }
}
alex
  • 6,359
  • 1
  • 23
  • 21
george
  • 13
  • 3
  • I can't see any array of strings in your code. Anyways, you should add a constructor which receives an array of strings then use [this link](http://stackoverflow.com/a/3044081/896038) – Saeid Farivar Apr 20 '13 at 15:45

1 Answers1

0

If your images are in your resources this is easy.

public class ImageAdapter extends BaseAdapter {

    private Context context;
    private Resources resources;

    private String[] imageNames;

    public ImageAdapter(Context context, String[] imageNames){
        this.context = context;
        this.resources = context.getResources();
        this.imageNames = imageNames;
    }

    public int getCount(){
        return imageNames.length;
    }

    public String getItem(int position){
        return imageNames[position];
    }

    public long getItemId(int position){
        return position;
    }

    public View getView(ViewGroup parent, View convertView, int position){
        if(convertView == null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.imageview, parent, false);
        }
        int imageId = resources.getIdentifier(getItem(position), "drawable", context.getPackageName());
        ImageView imageView = (ImageView) convertView.findViewById(R.id.imageview);
        imageView.setImageResource(imageId);
        return convertView;
    }
}
alex
  • 6,359
  • 1
  • 23
  • 21
  • thks for the responce!! i have problem again!! i can't understan exactly what is going on... i wrote that but i have problem in the getView function.. at the int imageId = resources.getIdentifier(getItem(position), "drawable", context.getPackage()); and imageView.setDrawable(imageId); for the first one there is no extention getPackage, only getPackageManager but i can't use that because the indetifiner(string,string,string). For the second line tthere is no exception setDrawable. – george Apr 22 '13 at 13:58