Android:访问ExpandableListViewActivity的Child内部的TextView

时间:2011-05-10 09:54:31

标签: java android layout expandablelistview textcolor

我通过ExpandableListViewActivity使用ExpandableList。

现在,我想在每个ChildView中更改某个TextView的文本颜色。我不希望它们都是相同的,而是绿色的“ok”和红色的“error”等。

我偶然发现了getExpandableListAdapter()。getChildView(...),但我不确定最后的参数(View convertView,ViewGroup parent)应该是什么。另外我不太清楚我是否需​​要将返回值(View)转换为某些内容?

TextView tv = (TextView)this.getExpandableListAdapter().getChildView(0, 0, false, ???, ???).findViewById(R.id.xlist_child_tv);

亲切的问候, 水母

解决方案摘要

SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(...parameters...){
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
    {
        final View itemRenderer = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
        final TextView tv = (TextView) itemRenderer.findViewById(R.id.kd_child_value);

        if (tv.getText().toString().contentEquals(getString(R.string.true)))
        {
            tv.setTextColor(getResources().getColor(R.color.myred));
        }
        else if (tv.getText().toString().contentEquals(getString(R.string.false)))
        {
            tv.setTextColor(getResources().getColor(R.color.mygreen));
        }
        else
        {
            tv.setTextColor(getResources().getColor(R.color.white));
        }

        return itemRenderer;

    }
};

色彩资源:

<resources>
    <color name="white">#ffffffff</color>
    <color name="myred">#FFFF3523</color>
    <color name="mygreen">#FFA2CD5A</color>
</resources>

1 个答案:

答案 0 :(得分:2)

您应该覆盖getChildView实现的ExpandableListAdapter方法,并在其中根据此适配器可访问的任何标志设置文本颜色。

如果更改是明确的,请不要忘记在更改标志后调用适配器上的notifyDatasetChanged()

要覆盖getChildView的{​​{1}}方法,您需要对其进行扩展(此处为匿名):

SimpleExpandableListAdapter

<强>更新
着色问题在于您的资源文件,您需要为文本颜色设置Alpha值,因此您的红色应为final SimpleExpandableListAdapter sa = new SimpleExpandableListAdapter(/*your list of parameters*/) { @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final View itemRenderer = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent); final TextView tv = (TextView)itemRenderer.findViewById(R.id.text); if (/*check whether the data at groupPosition:childPosition is correct*/) tv.setTextColor(getResources().getColor((R.color.green)); else tv.setTextColor(getResources().getColor((R.color.red)); return itemRenderer; } }; ,绿色应为#FFFF3523

#FFA2CD5A