我正在尝试创建一个数组适配器来将文本数组中的内容设置到适配器,但我无法从活动中设置适配器。我不确定我应该为Int
传递什么资源PlacesListAdapter.java
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;
}
}
Place.java
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;
}
}
现在在MainActiivty中我正在尝试设置适配器,以便从数组中填充列表
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);
}
}
我不确定在resources
adapter = new PlacesListAdapter(this, resource, places);
设置什么
答案 0 :(得分:1)
初始化resource
变量:
// 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<Place> thePlaces = new ArrayList<Place>();
for (int i = 0; i < places.length; i++) {
Place pl = new Place("NO_ID", places[i], "NO_DISTANCE", "NO_CATEGORYICON");
thePlaces.add(pl);
}
int resource = android.R.layout.simple_list_item_1;
adapter = new PlacesListAdapter(this, resource, thePlaces);
答案 1 :(得分:1)
ArrayAdapter
创建自定义适配器,则传递行布局ID而不是默认的android布局:
adapter = new PlacesListAdapter(this,R.layout.item_place, places);
而不是
adapter = new PlacesListAdapter(this, resource, places);