Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView - getView is called too much times

I know there are few questions regarding this issue of 'getView called few times' but my problem is little different.

I have a custom listView with custom row ( used a row_layout.xml). It generally works well. At the beginning I had a problem with multiple calls to getView and it was fixed by using one of the methods I saw here in stackoverflow. ( using the 'usedPositions' array).

now, I see in the logs this scenario: getView pos 0, getView pos 1, getView pos 0 , getView pos 1. This caused my rows to be doubled. It happens only when I call a new activity that covers the current activity and then close that activity. ( for example, open the camera activity and then close it).

I will show my code:

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View row = convertView;
    Toast toast = Toast.makeText(this.context, "getView " + position, 1000);
    toast.show();
    String pos = Integer.toString(position);
    if (!usedPositions.contains(pos)) { 

        CardHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new CardHolder();
            //holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.txtCouponTitle = (TextView)row.findViewById(R.id.txtTitle);
            holder.txtBusinessName = (TextView)row.findViewById(R.id.txtBusinessName);
            row.setTag(holder);
        }
        else
        {
            holder = (CardHolder)row.getTag();
        }

        Card card = data.get(position);
        holder.txtCouponTitle.setText(card.couponTitle);
        holder.txtBusinessName.setText(card.businessName);
        //holder.imgIcon.setImageResource(card.icon);

        TableLayout table = (TableLayout)row.findViewById(R.id.imagesTable); 
        for (int r=1; r<=1; r++){ 
            TableRow tr = new TableRow(this.context); 
            for (int c=1; c<=10; c++){ 
                ImageView im = new ImageView (this.context); 
                im.setImageDrawable(this.context.getResources().getDrawable(c<= card.numberOfStamps ? R.drawable.stamp_red :R.drawable.stamp_grey)); 
                im.setPadding(6, 0, 0, 0); //padding in each image if needed 
                //add here on click event etc for each image... 
                //... 
                tr.addView(im, 40,40);  
            } 
            table.addView(tr); 
        } 

        // Your code to fill the imageView object content 
        usedPositions.add(pos); // holds the used position 
    } 
    else
        usedPositions.remove(pos);

    return row;
}

Can you tell me what's wrong?

like image 865
Eran Tal Avatar asked Feb 06 '12 08:02

Eran Tal


1 Answers

Quoting android engineer RomainGuy

This is not an issue, there is absolutely no guarantee on the order in which getView() will be called nor how many times.

So the best you can handle is re-using the existing views (row layouts) properly.

Here is another good post.

like image 161
Adil Soomro Avatar answered Sep 30 '22 07:09

Adil Soomro