Android:按位置从listview获取视图

时间:2013-07-29 19:07:33

标签: android

我想以编程方式更改特定位置的listview(simple_list_item_2适配器)子项的颜色。 (例如,所有带有= lv.getCount();;

的项目
ListView lv = getListView();
int a = lv.getCount();
for (int i = 0; i < a; i++) {
            ((TextView) lv.getChildAt(i).findViewById(android.R.id.text1)).setTextColor(Color
                    .parseColor("#EEC900"));
}

getChildAt();并不总是对我有用。如果列表项不在,则getChild不会返回视图或其他内容。 如果是getChildAt,那么不是更好的解决方案吗?

1 个答案:

答案 0 :(得分:0)

您必须在Adapter类中执行此操作。 Android在列表视图中缓存并重新循环视图以节省内存。所以没有你无法看到的改变颜色的视图。

例如,如果您有一个arrayAdapter,您将覆盖getView函数并在那里运行检查:

@Override
    public View getView(int position, View view, ViewGroup parent) {
        // use "position" to determine which item you have.
        // Then set the properties of "view" which is your list row.

    }