簡體   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