我想从strings.xml文件中为列表视图使用某些字符串作为项目的名称,但在部署期间,我的应用程序无法运行。我已经在互联网的许多领域寻找相关教程,但他们都只是显示了硬编码的字符串。有谁知道我的代码有什么问题,以及如何使用字符串修复它,而不是硬编码?未将字符串分配到列表视图项
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;
}
}
}
'getActivity()getResources()的getString(R.string.item_0);'哪里是你的'adapter'类? –
@MD这不起作用 – MacaronLover
你能告诉我你的问题是什么?有任何错误?如果是,然后张贴它。 –