开发者

Error setting layout params to ImageView

I'm having issues trying to set layoutparams to an ImageView created programmatically:

imageView.setLayoutParams(new LinearLayout.LayoutParams(gallerySize.x, gallerySize.y));

The imageView is inside an LinearLayout, and I think that should work, but I get this error:

09-30 10:33:24.450: ERROR/AndroidRuntime(5418): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams

Maybe, the fact that this imageView Activity is configured to use an different layout for portrait and landscape view (I'm using this with an different copy of xml layout inside the layout-land folder). When it's in portrait view the problematic code line isn't executed, instead I execute the following line:

imageView.setLayoutParams(new Gallery.LayoutParams(gallerySize.x, gallerySize.y));

I tried to keep this line unchanged, but then I get the error:

09-30 10:49:47.450: ERROR/AndroidRuntime(5760): java.lang.ClassCastException: android.widget.Gallery$LayoutParams

The main difference in the portrait and landscape layout, is that portrait uses a LinearLayout with vertical orientation and have an Gallery widget (that uses that imageView), while the problematic landscape uses horizontal orientation and ListView instead of Gallery.

I'm kind lost here, any tips will be appreciated.


EDITED

The imageView is implemented in this class:

    public class ImageAdapter extends BaseAdapter {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(mContext);

            Bitmap temp = BitmapFactory.decodeFile(appFolder+"/"+imagesPath[position]);
            //productImages[position] = temp;

            imageView.setImageBitmap(temp);

            if(landscape)
                imageView.setLayoutParams(new LinearLayout.LayoutParams(gallerySize.x, gallerySize.y));
            else
                imageView.setLayoutParams(new Gallery.LayoutParams(gallerySize.x, gallerySize.y));


            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imageView.setBackgroundResource(mGalleryItemBackground);

            return imageView;
        }
    }

And the ImageAdapter instance is used here:

    public onCreate(...) {
        ...
        if(!landscape) {
        Gallery gallery = (Gallery) findViewById(R.infoproduto.gallery);
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView parent, View v, int position, long id) {
                    setSelectedImage(position);
                }
            }); 
        }
        else {
        ListView galleryLView = (ListView) findViewById(R.infoproduto.galleryLView);
        galleryLView.setAdapter(new ImageAdapter(this));
        galleryLView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItem开发者_如何学PythonClick(AdapterView parent, View v, int position, long id) {
                    setSelectedImage(position);
                }
            });
        }
    }


Well, I figured out the solution, it was so obvious!

The ImageView is used inside ImageAdapter, and this ImageAdapter is setted as adapter in an ListView instance:

    ListView galleryLView = (ListView) findViewById(R.infoproduto.galleryLView);
    galleryLView.setAdapter(new ImageAdapter(this));

So Instead using LinearLayout.LayoutParams (that is the only layout in my xml file), i used ListView.LayoutParams and it works perfectly!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜