我想在我的应用程序中使用列表视图,我做了正确的编码但是当我运行该应用程序时它接近了。这是我的代码:
setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1,R.id.textView1,getResources().getStringArray(R.array.cities)));
}
适配器
private class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] string) {
super(context, resource, textViewResourceId, string);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater in=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=in.inflate(R.layout.list_item, parent, false);
String[] items=getResources().getStringArray(R.array.cities);
TextView tv=(TextView) row.findViewById(R.id.textView1);
return super.getView(position, convertView, parent);
}
}
}
答案 0 :(得分:0)
更改为
LayoutInflater in;
String[] items;
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] string) {
super(context, resource, textViewResourceId, string);
in=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
items=context.getResources().getStringArray(R.array.cities);
}
getResources()
需要活动背景。
答案 1 :(得分:0)
您可以尝试这样的事情:
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MySimpleArrayAdapter(Context context, String[] values) {
super(context, R.layout.rowlayout, 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.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.label);
textView.setText(values[position]);
return rowView;
}
}