我是Android开发的新手,我一直在尝试将值从recyclerview
cardview
传递到detailActivity
,经过几个小时的搜索,我想出了这段代码在实现此代码之前,我能够查看cardview
上的所有MainActivity
,现在当我实现此代码应用程序时,当我启动应用程序,事件日志时崩溃。
这是我的片段,其中包含Firebaserecycler适配器和视图持有者类:
public class MakingFragment extends Fragment {
private FloatingActionButton fab;
ScaleAnimation shrinkAnim;
private RecyclerView mRecyclerView;
private LinearLayoutManager mLayoutManager;
private TextView tvNoMovies;
Context c;
ArrayList<Food> spacecrafts;
//Getting reference to Firebase Database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference mDatabaseReference = database.getReference();
private static final String userId = "53";
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.making,container,false);
//Initializing our Recyclerview
mRecyclerView = (RecyclerView) v.findViewById(R.id.my_recycler_view);
tvNoMovies = (TextView) v.findViewById(R.id.tv_no_movies);
//scale animation to shrink floating actionbar
shrinkAnim = new ScaleAnimation(1.15f, 0f, 1.15f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
if (mRecyclerView != null) {
//to enable optimization of recyclerview
mRecyclerView.setHasFixedSize(true);
}
//using staggered grid pattern in recyclerview
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mLayoutManager.setReverseLayout(true); // to reverse the items updating on recycleview
mLayoutManager.setStackFromEnd(true); // to scroll from top when layout is reversed
//Say Hello to our new FirebaseUI android Element, i.e., FirebaseRecyclerAdapter
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(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
openDetailActivity(model.getFoodName(),model.getFoodCategory(),model.getFoodAvailableUpto(),model.getFoodPrice());
}
});
}
@Override
public int getItemCount() {
return spacecrafts.size();
}
//OPEN DETAIL ACTIVITY
private void openDetailActivity(String...details)
{
Intent i=new Intent(c,DetailActivity.class);
i.putExtra("TITLE_KEY",details[0]);
i.putExtra("CAT_KEY",details[1]);
i.putExtra("AVAIL_KEY",details[2]);
i.putExtra("PRI_KEY",details[3]);
c.startActivity(i);
}
};
mRecyclerView.setAdapter(adapter);
fab = (FloatingActionButton) v.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent launchactivity=new Intent(getActivity().getApplicationContext(),AddFoodDetailsActivity.class);
startActivity(launchactivity);
//animation being used to make floating actionbar disappear
shrinkAnim.setDuration(400);
fab.setAnimation(shrinkAnim);
shrinkAnim.start();
shrinkAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
//changing floating actionbar visibility to gone on animation end
fab.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
});
return v;
}
public void onBackPressed() {
getActivity().onBackPressed();
if (fab.getVisibility() == View.GONE)
fab.setVisibility(View.VISIBLE);
}
//ViewHolder for our Firebase UI
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;
@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);
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");
//BIND DATA
nameTxt.setText(title);
catTxt.setText(cat);
availTxt.setText(avail);
priceTxt.setText(price);
}
}
请帮帮我,谢谢。
答案 0 :(得分:0)
在oncreate()
spacecrafts= new ArrayList<>();
这些也在oncreate()
database = FirebaseDatabase.getInstance();
mDatabaseReference = database.getReference();
并检查宇宙飞船是否为空