Android的适配器排藏身 [英] android adaptor row hiding
问题描述
在arrayAdptor我们使用以下code:
In arrayAdptor we use following code:
final LayoutInflater inflater = activity.getLayoutInflater();
row = (LinearLayoutCustom) inflater.inflate(R.layout.row, null);
final TextView label = (TextView) row.findViewById(R.id.title);
label.setText(position + "" + items[position]);
return row;
现在假设某个值都为空(例如,在位置2,项目[2] = NULL),所以我不希望显示它行。我想隐藏它。如果我使用
Now suppose some value are null (for example at position 2 , items[2] = null ) so i dont want to show it in row. i want to hide it. if i use
row.setVisibility(View.GONE)
它的叶子在这行,我不想一个空格。所以我应该怎么办?
it leaves a blank space at this row which i dont want. so what should i do?
推荐答案
您需要让适配器返回非空的项目总数与 getCount将
和然后继续的位置映射到内部数据结构。
You'll need to have the adapter return the total number of non-null items with getCount
and then keep a mapping of position to your internal data structure.
例如。
你有一个列表
For example. You have a list
1 - John
2 - null
3 - Bill
4 - Susan
5 - null
当getCount将被调用时返回3。
When getCount is called it returns 3.
然后当 getView
被称为在位置1你列表中返回的项目[1]
。 getView
上的位置2返回列表[3]
(因为它是第二个非空)
等等。
Then when getView
is called on position 1 you return the item at list[1]
.
getView
on position 2 returns list[3]
(as it's the 2nd non-null),
and so forth.
这是我发现这样做的唯一途径。
This is the only way I've found to do this.
这篇关于Android的适配器排藏身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!