我创建了一个gridview来显示带有图像的类别和一些文本:
<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>
这是我的适配器中的getView代码
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;
}
当我向上和向下滚动时,属于textview的一些文本会随机消失。虽然没有错误。我不知道发生了什么。我的代码有问题吗?
答案 0 :(得分:1)
您需要在if(category.getId()&gt; 0)块
中添加它holder.textView.setVisibility(View.VISIBLE);