MobDev
MobDev

Reputation: 1279

Android grid view recycle issue

I get an error when i try to use the a recycled gridview every time if convertView != null then i get an error here is my source code. it will give me an error right at text = (TextView) convertView; in the else statment. I am really lost here, I would just stop recycling the views but then its to heavy on memory and its choppy scrolling

$ here is the imageadapter.java

public View getView(int position, View convertView, ViewGroup parent) {
    RelativeLayout lay;

    ImageView image;
    TextView text;
    if (convertView == null) {
        Log.d("height", "Width = " + width);

        lay = new RelativeLayout(mContext);
        image = new ImageView(mContext);
        text = new TextView(mContext);


        //text.setText("This is a test");
        text.setTextSize(14);
        text.setTextColor(Color.WHITE);
        text.setGravity(Gravity.LEFT | Gravity.TOP);
        text.setPadding(2, 2, 2, 2);
        text.setBackgroundColor(Color.parseColor("#80000000"));
        RelativeLayout.LayoutParams textLayout = new RelativeLayout.LayoutParams(
                (int) Math.round(width / 2.0),
                (int) Math.round(width / 8.3));

        textLayout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        text.setLayoutParams(textLayout);

        MarginLayoutParams textMarginFix = (ViewGroup.MarginLayoutParams) text
                .getLayoutParams();
        textMarginFix.setMargins(0, 0, 0, (int) Math.round(width / 45.0));
        text.setLayoutParams(textMarginFix);

        image.setLayoutParams(new LayoutParams((int) Math
                .round(width / 2.0), (int) Math.round(width /              2.0)));

        image.setScaleType(ImageView.ScaleType.CENTER_CROP);
        //image.setImageResource(mThumbIds[position]);

        lay.setLayoutParams(new GridView.LayoutParams((int) Math
                .round(width / 2.0), (int) Math.round(width / 2.0)));
        lay.setBackgroundResource(R.drawable.shadowimage);
        lay.setPadding(5, 5, 15, 15);
        //lay.setId(mThumbIds[position]);

        //lay.addView(image);
        //lay.addView(text);

    } 
    else
    {
        text = (TextView) convertView;
        image = (ImageView) convertView;
        lay = (RelativeLayout) convertView;
    }

    image.setImageResource(mThumbIds[position]);
    text.setText("This is a test");

    lay.addView(image);
    lay.addView(text);

     return lay;

}
$here is where i call the imageadapter from another class

    @Override
public Object instantiateItem(View container, int position) {
   View contentView;        
   switch (position) {
    case 0:
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        View contentView = mInflater.inflate(R.layout.image_grid_view,   null);
        Display display = mContext.getWindowManager().getDefaultDisplay();
        final int width = display.getWidth();
        int height = display.getHeight();
        float scale = mContext.getResources().getDisplayMetrics().density;
        GridView gridview = (GridView) contentView.findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(mContext, width, height, scale));
        gridview.setFocusable(true);
        gridview.requestFocus();
        gridview.setOnItemClickListener(itemClickListener);

        ((ViewPager) container).addView(contentView, 0);
        break;
...return contentView

Upvotes: 2

Views: 4904

Answers (2)

btalb
btalb

Reputation: 7037

convertView is the view that entirely represents one item in your GridView. If that is a TextView, it will be a TextView, if it is an entire layout, you will receive the entire layout and so on.

So how do you know and define what is "the view that represents one item"? Simple, it is whatever you create when convertView == null and then return from getView.

Simply you are receiving a used item and you are just modifying it to update it to the appropriate content. So you should utilize type casting to get this View you receive in a format you want.

Code like below will get you what you want without redoing things you don't need to do (aka you do not need to re-add child Views from a convertView, only a new view):

public View getView(int position, View convertView, ViewGroup parent) {
    RelativeLayout lay;
    ImageView image;
    TextView text;

    if (convertView == null) {
        // Setup your 'item view'
        lay = new RelativeLayout(mContext);
        image = new ImageView(mContext);
        text = new TextView(mContext);

        // Do all your customizing stuff (aka size, color, format, padding layout params)

        lay.addView(image, 0);
        lay.addView(text, 1);
    } else {
        lay = (RelativeLayout) convertView;
        image = (ImageView) lay.getChildAt(0);
        text = (TextView) lay.getChildAt(1);
    }

    // Set content for your image and text

    return lay;
}

Upvotes: 3

trumpetlicks
trumpetlicks

Reputation: 7085

[ADDITION]

Further, you have the code

image.setImageResource(mThumbIds[position]);
text.setText("This is a test");

lay.addView(image);
lay.addView(text);

return lay;

outside the if and the else, what this means is that you are trying to add a subview EVERY TIME a cell's view is asked for. You really only need to add views to lay within the if portion of your routine.

[ORIGINAL] Most likely convertView is your parent, and all other views are sub to it. In your else clause, all you do is set each one to the SAME EXACT THING. How can the convertView be three completely different things all at once.

Most likely it needs to be:

text = (TextView) convertView.SomeTextViewSubToConvertView;
image = (ImageView) convertView.SomeImageViewSubToConvertView;
lay = (RelativeLayout) convertView.SomeRelativeLayoutSubToConvertView;

Upvotes: 0

Related Questions