我想将点击的项目数据从MainActivity
转移到某些DetailActivity
,以下是代码:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent intent = new Intent(MainActivity.this,TourDetailActivity.class);
intent.putExtra("tourId", tours.get(position).getId());
startActivity(intent);
}
这是适配器:
public class TourListAdapter extends ArrayAdapter<Tour> {
Context context;
List<Tour> tours;
public TourListAdapter(Context context, List<Tour> tours) {
super(context, android.R.id.content, tours);
this.context = context;
this.tours = tours;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.list_item_tour, null);
Tour tour = tours.get(position);
TextView tv = (TextView) view.findViewById(R.id.titleText);
tv.setText(tour.getTitle());
tv = (TextView) view.findViewById(R.id.priceText);
NumberFormat nf = NumberFormat.getCurrencyInstance();
tv.setText(nf.format(tour.getPrice()));
ImageView iv = (ImageView) view.findViewById(R.id.imageView1);
int res = context.getResources().getIdentifier(
tour.getImage(), "drawable", context.getPackageName());
if (res != 0 ) {
iv.setImageResource(res);
}
return view;
}
}
我知道问题是tours.get(position).getId()
。但我不知道如何解决它
感谢
答案 0 :(得分:0)
如果你想通过intent在两个活动之间传递对象数据,你应该使用parcelable或Serializable。我使用Serializable做了这个样本。 请试试这个。
Tour Custom对象类将是。
public class Tour implements Serializable{
String title;
String price;
int Image;
public String getTitle() {
return title;
}
public int getImage() {
return Image;
}
public void setImage(int image) {
Image = image;
}
public void setTitle(String title) {
this.title = title;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
,你的数组适配器将是
public class Adapter extends ArrayAdapter<Tour> {
Context context;
List<Tour> tours;
public Adapter(Context context, List<Tour> tours) {
super(context,R.layout.list_item_tour, tours);
this.context = context;
this.tours = tours;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = vi.inflate(R.layout.list_item_tour, null);
Tour tour = tours.get(position);
TextView tv = (TextView) view.findViewById(R.id.titleText);
tv.setText(tour.getTitle());
tv = (TextView) view.findViewById(R.id.priceText);
/* NumberFormat nf = NumberFormat.getCurrencyInstance();
tv.setText(nf.format(tour.getPrice()));
*/
ImageView iv = (ImageView) view.findViewById(R.id.imageView1);
/*int res = context.getResources().getIdentifier(
tour.getImage(), "drawable", context.getPackageName());
if (res != 0 ) {
iv.setImageResource(res);
}*/
return view;
}
}
在您的第一个活动中,即在MainActivity中粘贴此代码。
Adapter adapter=new Adapter(this,tours);
listvView.setAdapter(adapter);
listvView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Tour tour=(Tour)listvView.getAdapter().getItem(i);
Intent intent = new Intent(Demo.this,Demo2.class);
intent.putExtra("tourId",tour);
startActivity(intent);
}
});
并在你的第二个活动(即DetailActivity)中创建粘贴此代码
Tour objects=(Tour)getIntent().getSerializableExtra("tourId");
Log.e("Selected Tour title is ",objects.getTitle());