我能够将textview数据从cardview传递到另一个名为" DetailActivity"的活动,但我不知道如何将图像传递给DetailAtivity,以及我如何发送其他数据这不在我的viewholder类中,就像我想使用intent发送项目的描述一样?
这是我的FirebaseAdapter
FirebaseRecyclerAdapter<Food,FoodViewHolder> adapter = new FirebaseRecyclerAdapter<Food, FoodViewHolder>(
Food.class,
R.layout.food_item,
FoodViewHolder.class,
//referencing the node where we want the database to store the data from our Object
mDatabaseReference.child("users").child(userId).child("food").getRef()
) {
@Override
protected void populateViewHolder(final FoodViewHolder viewHolder, final Food model, int position) {
if(tvNoMovies.getVisibility()== View.VISIBLE){
tvNoMovies.setVisibility(View.GONE);
}
viewHolder.tvFoodName.setText(model.getFoodName());
viewHolder.ratingBar.setRating(model.getFoodRating());
viewHolder.tvFoodCategory.setText(model.getFoodCategory());
viewHolder.tvFoodAvailableUpto.setText(model.getFoodAvailableUpto());
viewHolder.tvFoodPrice.setText(model.getFoodPrice());
Picasso.with(getActivity().getApplicationContext()).load(model.getFoodPoster()).into(viewHolder.ivFoodPoster);
viewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(int pos) {
//OPEN DETAI ACTIVITY
Intent i = new Intent(getContext(), DetailActivity.class);
i.putExtra("TITLE_KEY",viewHolder.tvFoodName.getText().toString());
i.putExtra("CAT_KEY",viewHolder.tvFoodCategory.getText().toString());
i.putExtra("AVAIL_KEY",viewHolder.tvFoodAvailableUpto.getText().toString());
i.putExtra("PRI_KEY",viewHolder.tvFoodPrice.getText().toString());
here // i.putExtra("LOGO_KEY",viewHolder.ivFoodPoster);
getContext().startActivity(i);
}
});
}
};
mRecyclerView.setAdapter(adapter);
这是我的观察者类
public static class FoodViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tvFoodName;
RatingBar ratingBar;
ImageView ivFoodPoster;
TextView tvFoodCategory;
TextView tvFoodAvailableUpto;
TextView tvFoodPrice;
ImageView ivNextArrow;
ItemClickListener itemClickListener;
public FoodViewHolder(View v) {
super(v);
tvFoodName = (TextView) v.findViewById(R.id.tvName);
ratingBar = (RatingBar) v.findViewById(R.id.rating_bar);
ivFoodPoster = (ImageView) v.findViewById(R.id.imgThumb);
tvFoodCategory = (TextView) v.findViewById(R.id.tvCategory);
tvFoodAvailableUpto= (TextView) v.findViewById(R.id.tvavailable);
tvFoodPrice = (TextView) v.findViewById(R.id.tvprice);
ivNextArrow = (ImageView) v.findViewById(R.id.NextArrow);
v.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener)
{
this.itemClickListener=itemClickListener;
}
@Override
public void onClick(View view) {
this.itemClickListener.onItemClick(this.getLayoutPosition());
}
}
这是我的DetailActivity
public class DetailActivity extends AppCompatActivity {
TextView nameTxt,catTxt,availTxt,priceTxt;
ImageView imageLogo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
nameTxt = (TextView) findViewById(R.id.titleTxt);
imageLogo = (ImageView) findViewById(R.id.imagelogo);
catTxt= (TextView) findViewById(R.id.catTxt);
availTxt = (TextView) findViewById(R.id.availTxt);
priceTxt = (TextView) findViewById(R.id.pricesTxt);
//GET INTENT
Intent i=this.getIntent();
//RECEIVE DATA
String title=i.getExtras().getString("TITLE_KEY");
String cat=i.getExtras().getString("CAT_KEY");
String avail=i.getExtras().getString("AVAIL_KEY");
String price=i.getExtras().getString("PRI_KEY");
//here // String image=i.getExtras().getString("LOGO_KEY");
//BIND DATA
nameTxt.setText(title);
catTxt.setText(cat);
availTxt.setText(avail);
priceTxt.setText(price);
// here// Picasso.with(DetailActivity.this).load(image).into(imageLogo);
}
}
答案 0 :(得分:0)
好的,因为model.getFoodPoster()
会返回String
,你可以这样做:
i.putExtra("IMAGE_KEY",model.getFoodPoster());
但是我建议您阅读:serializable
或parcelable
(推荐),使用其中任何一个都可以让您将整个对象model
发送到另一个活动!
看看:
答案 1 :(得分:0)
在FireBaseAdapter中
viewHolder.tvFoodName.setText(model.getFoodName());
viewHolder.ratingBar.setRating(model.getFoodRating());
viewHolder.tvFoodCategory.setText(model.getFoodCategory());
viewHolder.tvFoodAvailableUpto.setText(model.getFoodAvailableUpto());
viewHolder.tvFoodPrice.setText(model.getFoodPrice());
Picasso.with(getActivity().getApplicationContext()).load(model.getFoodPoster()).into(viewHolder.ivFoodPoster);
viewHolder.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(int pos) {
//OPEN DETAI ACTIVITY
Intent i = new Intent(getContext(), DetailActivity.class);
i.putExtra("TITLE_KEY",viewHolder.tvFoodName.getText().toString());
i.putExtra("CAT_KEY",viewHolder.tvFoodCategory.getText().toString());
i.putExtra("AVAIL_KEY",viewHolder.tvFoodAvailableUpto.getText().toString());
i.putExtra("PRI_KEY",viewHolder.tvFoodPrice.getText().toString());
i.putExtra("LOGO_KEY",model.getFoodPoster);
getContext().startActivity(i);
}
对于DetailActivity,
Picasso.with(this).load(i.getExtras().getString("LOGO_KEY")).into(imageLogo);
//if you doesn't want any placeholder for it.
//if you want then place it as load().placeholder(R.drawable.anypic).into(imageLogo)