我将数据传递给 ListView 以显示一些餐厅名称。现在,当单击一个项目时,我想启动另一个活动以显示更多餐厅数据。我不确定该怎么做。我应该通过意图对象传递捆绑中的所有餐厅数据吗?还是我应该只传递餐厅 ID 并在其他活动中获取数据?在这种情况下,我如何从其他活动访问我的餐厅列表?无论如何,我如何从我点击的餐厅获取数据(视图只包含名称)?
任何帮助,欢迎指点!
ListView lv= (ListView)findViewById(R.id.listview);
lv.setAdapter( new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,restaurantList.getRestaurantNames()));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(Atable.this, RestaurantEdit.class);
Bundle b = new Bundle();
//b.putInt("id", ? );
startActivityForResult(i, ACTIVITY_EDIT);
}
});
RestaurantList.java
package org.digitalfarm.atable;
import java.util.ArrayList;
import java.util.List;
public class RestaurantList {
private List<Restaurant> restaurants = new ArrayList<Restaurant>();
public List<Restaurant> getRestaurants() {
return this.restaurants;
}
public void setRestaurants(List<Restaurant> restaurants) {
this.restaurants = restaurants;
}
public List<String> getRestaurantNames() {
List<String> restaurantNames = new ArrayList<String>();
for (int i=0; i<this.restaurants.size(); i++) {
restaurantNames.add(this.restaurants.get(i).getName());
}
return restaurantNames;
}
}
餐厅.java
package org.digitalfarm.atable;
public class Restaurant {
private int id;
private String name;
private float latitude;
private float longitude;
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public float getLatitude() {
return this.latitude;
}
public void setLatitude(float latitude) {
this.latitude = latitude;
}
public float getLongitude() {
return this.longitude;
}
public void setLongitude(float longitude) {
this.longitude = longitude;
}
}