Pham Hong Phong
Pham Hong Phong

Reputation: 145

Android textview doesn't show after scroll in gridview.

I created a gridview to show the category with an image and some text as:

 <GridView
            android:id="@+id/gridViewCategories"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#c7c9bf"
            android:fadingEdge="none"
            android:horizontalSpacing="1dp"
            android:listSelector="@null"
            android:numColumns="3"
            android:scrollbars="none"
            android:verticalSpacing="1dp" >

 </GridView>

This is the code for getView in my adapter

public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;

    if (convertView == null) {
        LayoutInflater inflater = ((Activity) context)
                .getLayoutInflater();
        convertView = inflater.inflate(R.layout.poi_category_item, parent,
                false);
        holder = new ViewHolder();
        holder.imageView = (ImageView) convertView.findViewById(R.id.imageViewIcon);
        holder.textView = (TextView) convertView.findViewById(R.id.textViewName);
        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    Category category = list.get(position);

    if (category != null) {

        if (Build.VERSION.SDK_INT < 11) {
            convertView.setBackgroundColor(Color.parseColor("#f8f8f3"));
        }

        holder.textView.setLines(2);
        holder.imageView.setImageResource(category.getResourceId());

        if (category.getId() > 0) {
            holder.imageView.setVisibility(View.VISIBLE);
        } else {
            holder.imageView.setVisibility(View.INVISIBLE);
            holder.textView.setVisibility(View.INVISIBLE);
        }
        holder.textView.setText(category.getName());
    }
    return convertView;
}

When I scroll up and down, some of the text belonging to a textview disappears randomly. Although there are no errors. I don't know what's happenning. Is something wrong with my code?

Upvotes: 1

Views: 621

Answers (1)

oks16
oks16

Reputation: 1294

You need to add this in the if(category.getId()>0) block

holder.textView.setVisibility(View.VISIBLE);

Upvotes: 1

Related Questions