简体   繁体   English

在ListView适配器中的getView(...)中更改TextView颜色和文本

[英]Change TextView colour and text in getView(…) in ListView adapter

I've searched for this and I could find exactly the answer I needed... 我搜索过这个,我可以找到我需要的答案......

I have a navigation drawer ListView , and I when an item is clicked in the list, I want to be able to get the TextView from it, and change its background colour, text colour and make the text bold. 我有一个导航抽屉ListView ,当我在列表中单击一个项目时,我希望能够从中获取TextView ,并更改其背景颜色,文本颜色并使文本变为粗体。

I've tried getting the TextView by doing the following; 我尝试通过执行以下操作来获取TextView ;

adapter_navList = new ArrayAdapter<String>(this, R.layout.listview_navdrawer, drawerItems) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        TextView item = (TextView) lv_navList.getItemAtPosition(i); 
        // Code to set the background, formatting, colour, etc.

        return view;
    }
};
lv_navList.setAdapter(adapter_navList);

However, I keep receiving the following error: 但是,我一直收到以下错误:

java.lang.ClassCastException: java.lang.String cannot be cast to android.widget.TextView java.lang.ClassCastException:java.lang.String无法强制转换为android.widget.TextView

How would I resolve this issue? 我该如何解决这个问题?

You can use the following: 您可以使用以下内容:

adapter_navList = new ArrayAdapter<String>(this, R.layout.listview_navdrawer, drawerItems) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);

        TextView mytextview=(TextView)view;
        mytextview.setTextColor(Color.BLUE); 
        mytextview.setTextSize(14);
        // Other code you may want to add

        return view;
    }
};
lv_navList.setAdapter(adapter_navList);

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

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