如何改变android.R.layout.simple_list_item_1的文本颜色?

时间:2015-01-15 11:10:58

标签: java android

我将适配器设置为listview,文本颜色显示为白色。如何在不创建自己的行项目布局的情况下更改文本颜色?下面是我的代码,它抛出了这个错误 -

01-15 16:33:40.197: E/AndroidRuntime(6088): java.lang.NullPointerException
01-15 16:33:40.197: E/AndroidRuntime(6088):     at com.mb.pyramid.ui.fragment.DeviceListFragment$2.getView(DeviceListFragment.java:73)

我的代码:

mBTAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_list_item_1) {
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    View view = super.getView(position, convertView, parent);
                    TextView textView = (TextView)view.findViewById(android.R.layout.simple_list_item_1);
                    textView.setTextColor(Color.BLACK);

                    return view;
                }
            };

1 个答案:

答案 0 :(得分:4)

android.R.layout.simple_list_item_1仅包含TextView。在这种情况下,您可以转换super.getView(...)的返回值,避免使用findViewById

TextView textView = (TextView) super.getView(position, convertView, parent);
textView.setTextColor(Color.BLACK);
return textView;