繁体   English   中英

Android在SimpleExpandableListAdapter中更改TextView的颜色

[英]Android changing color of TextView in SimpleExpandableListAdapter

我正在从事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());
}

任何想法如何解决?

终于我找到了解决方案:

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;

就像评论所说,您正在将TwoLineListItem投射到TextView。 文档所示,可以使用getText1()getText2()来获取单独的textviews。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM