0

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

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());
}

知道如何解决吗?

4

3 回答 3

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;
于 2013-05-16T13:36:47.780 回答
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());
    }
于 2013-05-16T13:41:14.223 回答
0

正如评论所说,您正在将 TwoLineListItem 转换为 TextView。如文档中所示,您可以使用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
于 2013-05-16T13:38:01.877 回答