当evr单击foodmenu时,它显示黑屏,我的代码已正确编写在firebase recyclerview适配器中
在此foodList.java类中必须进行哪些更改,我尝试使用firebase recycler选项适配器,并将firebase数据库的权限设置为true。它附加了no适配器。 FoodList.java
public class FoodList extends AppCompatActivity {
RecyclerView recycler_food;
RecyclerView.LayoutManager layoutManager;
FirebaseDatabase database;
DatabaseReference foodList;
String categoryId = "";
FirebaseRecyclerAdapter<Food, FoodViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_list);
database = FirebaseDatabase.getInstance();
foodList = database.getReference("Foods");
recycler_food = (RecyclerView) findViewById(R.id.recyclerfood);
recycler_food.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recycler_food.setLayoutManager(layoutManager);
if (getIntent() != null)
categoryId = getIntent().getStringExtra("CategoryId");
if (categoryId != null && categoryId.isEmpty()) {
loadListFood(categoryId);
}
}
private void loadListFood(String categoryId) {
Query food = null;
assert food != null;
FirebaseRecyclerOptions<Food> options =
new FirebaseRecyclerOptions.Builder<Food>()
.setQuery(food, Food.class).build();
adapter = new FirebaseRecyclerAdapter<Food, FoodViewHolder>(options) {
@Override
public FoodViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.food_item, parent, false);
return new FoodViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull FoodViewHolder holder, int position, @NonNull Food model) {
holder.food_name.setText(model.getName());
Picasso.with(getBaseContext()).load(model.getImage()).into(holder.food_image);
final Food local = model;
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
Toast.makeText(FoodList.this, "" + local.getName(), Toast.LENGTH_SHORT).show();
}
});
}
};
recycler_food.setAdapter(adapter);
adapter.startListening();
}
}
FoodViewHolder
public class FoodViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView food_name;
public ImageView food_image;
private ItemClickListener itemClickListener;
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
public FoodViewHolder(View itemView) {
super(itemView);
food_name = itemView.findViewById(R.id.food_name);
food_image = itemView.findViewById(R.id.food_image);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
itemClickListener.onClick(view,getAdapterPosition(),false);
}
}
Logcat错误显示
06-26 20:57:44.626 15914-15914/com.example.sharma.digimenu I/Timeline: Timeline: Activity_launch_request id:com.example.sharma.digimenu time:94010711
06-26 20:57:44.637 15914-15942/com.example.sharma.digimenu V/FA: Recording user engagement, ms: 2649
06-26 20:57:44.638 15914-15942/com.example.sharma.digimenu V/FA: Activity paused, time: 241535027
06-26 20:57:44.641 15914-15942/com.example.sharma.digimenu D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=2649, firebase_screen_class(_sc)=Home, firebase_screen_id(_si)=5270971596160237574}]
06-26 20:57:44.649 15914-15914/com.example.sharma.digimenu V/FA: onActivityCreated
06-26 20:57:44.682 15914-15942/com.example.sharma.digimenu D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=Home, firebase_previous_id(_pi)=5270971596160237574, firebase_screen_class(_sc)=FoodList, firebase_screen_id(_si)=5270971596160237575}]
06-26 20:57:44.691 15914-15914/com.example.sharma.digimenu E/RecyclerView: No adapter attached; skipping layout
06-26 20:57:44.712 15914-15914/com.example.sharma.digimenu E/RecyclerView: No adapter attached; skipping layout
06-26 20:57:44.729 15914-15914/com.example.sharma.digimenu I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@35a28f6a time:94010814
06-26 20:57:44.731 15914-15942/com.example.sharma.digimenu V/FA: Activity resumed, time: 241535052
答案 0 :(得分:0)
public class FoodList extends AppCompatActivity {
RecyclerView recycler_food;
RecyclerView.LayoutManager layoutManager;
FirebaseDatabase database;
DatabaseReference foodList;
String categoryId = "";
FirebaseRecyclerAdapter<Food, FoodViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_list);
database = FirebaseDatabase.getInstance();
foodList = database.getReference("Foods");
recycler_food = (RecyclerView) findViewById(R.id.recyclerfood);
recycler_food.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recycler_food.setLayoutManager(layoutManager);
if (getIntent() != null)
categoryId = getIntent().getStringExtra("CategoryId");
if (categoryId != null && ! categoryId.isEmpty()) {
loadListFood(categoryId);
}
}
private void loadListFood(String categoryId) {
Query food = FirebaseDatabase.getInstance().getReference("Foods").orderByChild("MenuId").equalTo(categoryId);
FirebaseRecyclerOptions<Food> options =
new FirebaseRecyclerOptions.Builder<Food>().setLifecycleOwner(this)
.setQuery(food, Food.class).build();
adapter = new FirebaseRecyclerAdapter<Food, FoodViewHolder>(options) {
@NonNull
@Override
public FoodViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.food_item, parent, false);
return new FoodViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull FoodViewHolder holder, int position, @NonNull Food model) {
holder.food_name.setText(model.getName());
Picasso.with(getBaseContext()).load(model.getImage()).into(holder.food_image);
final Food clickItem = model;
//adapter.notifyDataSetChanged();
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
Toast.makeText(FoodList.this, "" + clickItem.getName(), Toast.LENGTH_SHORT).show();
}
});
}
};
recycler_food.setAdapter(adapter);
}
}