无法将Android资源解析为变量
我正在尝试创建一个数组适配器,以将文本数组中的内容设置为适配器,但无法从活动中设置适配器。我不确定应该将资源传递为Int PlacesListAdapter.java无法将Android资源解析为变量,android,listview,android-listview,android-arrayadapter,Android,Listview,Android Listview,Android Arrayadapter,我正在尝试创建一个数组适配器,以将文本数组中的内容设置为适配器,但无法从活动中设置适配器。我不确定应该将资源传递为Int PlacesListAdapter.java public class PlacesListAdapter extends ArrayAdapter<Place> { public Context context; private List<Place> places; public PlacesListAdapter(Con
public class PlacesListAdapter extends ArrayAdapter<Place> {
public Context context;
private List<Place> places;
public PlacesListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public PlacesListAdapter(Context context, int resource, List<Place> places) {
super(context, resource, places);
this.context = context;
this.places = places;
// imageLoader = new ImageLoader(context.getApplicationContext());
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View view = convertView;
Place p = places.get(position);
if (convertView == null) {
LayoutInflater viewInflater;
viewInflater = LayoutInflater.from(getContext());
view = viewInflater.inflate(R.layout.item_place, null);
holder = new ViewHolder();
holder.placeTitle = (TextView) view.findViewById(R.id.place_title);
holder.placeDistance = (TextView) view
.findViewById(R.id.place_distance);
view.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.placeTitle.setText(p.getPlaceTitle());
holder.placeDistance.setText("200");
holder.placeCategoryIcon.setImageResource(R.drawable.marker);
return view;
}
static class ViewHolder {
TextView placeId;
TextView placeTitle;
TextView placeDistance;
ImageView placeCategoryIcon;
}
}
public class Place {
String placeId = "", placeTitle = "", placeDistance = "",
placeCategoryIcon = "";
public Place(String placeId, String placeTitle, String placeDistance,
String placeCategoryIcon) {
this.placeId = placeId;
this.placeTitle = placeTitle;
this.placeDistance = placeDistance;
this.placeCategoryIcon = placeCategoryIcon;
}
public String getPlaceId() {
return placeId;
}
public void setPlaceId(String placeId) {
this.placeId = placeId;
}
public String getPlaceTitle() {
return placeTitle;
}
public void setPlaceTitle(String placeTitle) {
this.placeTitle = placeTitle;
}
public String getPlaceDistance() {
return placeDistance;
}
public void setPlaceDistance(String placeDistance) {
this.placeDistance = placeDistance;
}
public String getPlaceCategoryIcon() {
return placeCategoryIcon;
}
public void setPlaceCategoryIcon(String placeCategoryIcon) {
this.placeCategoryIcon = placeCategoryIcon;
}
}
现在在MainActivity中,我正在尝试设置适配器,以便它从数组填充列表
public class MainActivity extends SherlockFragmentActivity implements
SearchView.OnQueryTextListener {
private final String[] places = new String[] { "Mysore", "Bangalore", "Mangalore",
"Wayanad", "Bandipur National Park", "Chickmaglur",
"Bandipura", "Coorg", "Kodaikanal", "Hampi",
"Ghati Subramanya", "Mekedatu", "Muththathhi", "Shivasamudram",
"Talakadu", "Savana Durga" };
public SearchView mSearchView;
private TextView mStatusView;
private Menu mainMenu = null;
PlacesListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("Nomad", "onCreate");
ListView listView = (ListView) findViewById(R.id.place_list);
adapter = new PlacesListAdapter(this, resource, places);
}
}
我不确定在
adapter=newplacesListAdapter(这个,资源,位置)中为resources
设置什么代码>初始化资源
变量:
// it doesn't matter what values you assing to the resource variable because you build
// the row layout yourself in the getView method of the adapter
int resource = android.R.layout.simple_list_item_1;
adapter = new PlacesListAdapter(this, resource, places);
编辑:
List thePlaces=newarraylist();
for(int i=0;i
如果您要通过扩展阵列适配器来创建自定义适配器,请传递行布局id而不是默认的android布局:
adapter = new PlacesListAdapter(this,R.layout.item_place, places);
而不是
adapter = new PlacesListAdapter(this, resource, places);
我试过了,但是现在得到了这个错误,构造函数PlacesListAdapter(MainActivity,int,String[])是undefined@HarshaMV您的适配器的构造函数希望没有数据或一个位置列表
,您正试图向它传递一个字符串数组。将字符串数组转换为位置
对象的列表,并将该列表传递给适配器。你能给我指出一些教我如何创建对象的资源吗。
adapter = new PlacesListAdapter(this, resource, places);