字符串未分配给列表视图项

SD826E

我正在尝试使用strings.xml文件中的一些字符串作为列表视图的项目名称,但是在部署期间,我的应用程序将无法运行。我已经在Internet的许多领域中寻找了相关的教程,但是所有这些教程都只显示了经过硬编码的字符串。有谁知道我的代码出了什么问题以及如何使用字符串而不是硬编码来解决它?

    public class FragmentLineChooserList extends android.support.v4.app.Fragment {

        ListView list_linechooser;

        String[] listContent = {
                getResources().getString(R.string.item_0),
                getResources().getString(R.string.item_1),
                getResources().getString(R.string.item_2)
        };

        private boolean mTwoPane;

        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
            View v = inflater.inflate(R.layout.fragment_line_chooser_list, container, false);

            list_linechooser = (ListView)v.findViewById(R.id.list_linechooser);
        MyColoringAdapter adapter = new MyColoringAdapter(getActivity(),listContent);
        list_linechooser.setAdapter(adapter);
       );

        return v;
    }

    private class MyColoringAdapter extends ArrayAdapter<String> {
        private final Context context;
        private final String[] values;

        public MyColoringAdapter(Context context, String[] values) {
            super(context, R.layout.list_item, values);
            this.context = context;
            this.values = values;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.list_item, parent, false);
            TextView textView = (TextView) rowView.findViewById(R.id.list_item);
            textView.setText(values[position]);
            int textColorId = R.color.white; // Default color
            switch (position) {
                case 0:
                    textColorId = R.color.red; break;
                case 1:
                    textColorId = R.color.yellow; break;
                case 2:
                    textColorId = R.color.green; break;
            }
            textView.setTextColor(getResources().getColor(textColorId));
            return rowView;
        }
    }
}
医学博士

喜欢

  String[] listContent = {
            getActivity().getResources().getString(R.string.item_0),
            getActivity().getResources().getString(R.string.item_1),
            getActivity().getResources().getString(R.string.item_2)
    };

在下面 onCreateView(...)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章