0

すべての画像は描画可能なフォルダーに保存されます。いくつかの画像を含むギャラリー ビューを表示したい。

必要な画像の名前は文字列配列にあります。

これらの画像を表示するギャラリー ビューを取得できますか。

以下のコードを見つけました。ここでは、画像は整数です。

文字列配列を使用できますか?

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;
    }
}
4

1 に答える 1

0

画像がリソースにある場合、これは簡単です。

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;
    }
}
于 2013-04-20T15:48:33.690 に答える