Android在SimpleExpandableListAdapter中更改TextView的颜色

时间:2013-05-16 13:23:05

标签: java android

我正在开发Android项目。我需要一些错误的帮助。我想在List中更改文本颜色。它失败并出现错误:

05-16 15:15:19.867:E / AndroidRuntime(31408):致命异常:主要 05-16 15:15:19.867:E / AndroidRuntime(31408):java.lang.ClassCastException:android.widget.TwoLineListItem无法强制转换为android.widget.TextView


try {
    mAdapter = new SimpleExpandableListAdapter(
        this,
        groupData,
        android.R.layout.simple_expandable_list_item_2,
        new String[] { FIRST, SECOND },
        new int[] { android.R.id.text1, android.R.id.text2 },
        childData,
        android.R.layout.simple_expandable_list_item_2,
        new String[] { FIRST, SECOND },
        new int[] { android.R.id.text1, android.R.id.text2 }){
            @Override
            public View getChildView(int groupPosition, int childPosition,
                    boolean isLastChild, View convertView, ViewGroup parent) {

                TextView tv =  (TextView) super.getChildView(groupPosition, childPosition, isLastChild,convertView, parent);
                //change color text of tv here
                //tv.setTextColor(0xff00ff00);
                return tv;
            }
    };
    setListAdapter(mAdapter);
    getExpandableListView().setOnChildClickListener(this);
}
catch(Exception exc) {
    Log.e("Log", exc.toString());
}

知道如何解决这个问题吗?

3 个答案:

答案 0 :(得分:0)

最后我找到了解决方案:

final View itemRenderer = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
                        final TextView tv1 = (TextView)itemRenderer.findViewById(android.R.id.text1);
                        final TextView tv2 = (TextView)itemRenderer.findViewById(android.R.id.text2);
                        tv2.setTextColor(0xff0000ff);
                        return itemRenderer;

答案 1 :(得分:0)

正如评论所说,您正在将TwoLineListItem转换为TextView。如documentation所示,您可以使用getText1()getText2()来获取单独的文字视图。

TwoLineListItem tlli = (TwoLineListItem) super.getChildView(..);
TextView tv1 = tlli.getText1(); // get the first TextView
TextView tv2 = tlli.getText2(); // get the second TextView, if you want it
// Here you can change the text colours

答案 2 :(得分:0)

    try {
        mAdapter = new SimpleExpandableListAdapter(
            this,
            groupData,
            android.R.layout.simple_expandable_list_item_2,
            new String[] { FIRST, SECOND },
            new int[] { android.R.id.text1, android.R.id.text2 },
            childData,
            android.R.layout.simple_expandable_list_item_2,
            new String[] { FIRST, SECOND },
            new int[] { android.R.id.text1, android.R.id.text2 }){
                @Override
                public View getChildView(int groupPosition, int childPosition,
                        boolean isLastChild, View convertView, ViewGroup parent) {

                    TwoLineListItem tv =  (TwoLineListItem ) super.getChildView(groupPosition, childPosition, isLastChild,convertView, parent);
    final TextView tv1 = (TextView)tv.findViewById(android.R.id.text1);
                            final TextView tv2 = (TextView)tv.findViewById(android.R.id.text2);
tv1.setText("TextView1");
tv2.setText("TextView2");
                    //change color text of tv here
                    //tv.setTextColor(0xff00ff00);
                    return tv;
                }
        };
        setListAdapter(mAdapter);
        getExpandableListView().setOnChildClickListener(this);
    }
    catch(Exception exc) {
        Log.e("Log", exc.toString());
    }