FIrebase适配器未正确填充数据

时间:2019-04-01 15:10:08

标签: java android firebase firebase-realtime-database

对不起,如果我问的是me脚的问题,我是android开发的新手。 我有一个活动,其中我正在填充Firebase回收器适配器问题,是当我单击该活动一次时,它没有在填充回收器(loadFoodlist)。

但是当我两次或三次单击该活动后,它又能很好地获取数据,我不知道每次重新启动应用程序时会发生什么问题?

数据库结构     database image

public class FoodDetailModel {

 String name, price, description, type;

public FoodDetailModel(){

}

public FoodDetailModel(String name, String price, String description, String type) {
    this.name = name;
    this.price = price;
    this.description = description;
    this.type = type;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

}

公共类FoodDetailViewHolder扩展了RecyclerView.ViewHolder {

public TextView foodname,price,description;
public Button delete;
public ImageView type;

public FoodDetailViewHolder(@NonNull View itemView) {
    super(itemView);

    foodname = (TextView) itemView.findViewById(R.id.food_name);
    price = (TextView) itemView.findViewById(R.id.food_price);
    description = (TextView) itemView.findViewById(R.id.food_description);
    delete = (Button) itemView.findViewById(R.id.add_to_cart);
    type = itemView.findViewById(R.id.type);


}

}

公共类FoodDetail扩展了AppCompatActivity {

FirebaseDatabase database;
FirebaseAuth mAuth;
DatabaseReference ref;

FirebaseRecyclerAdapter<FoodDetailModel, FoodDetailViewHolder> adapter;

RecyclerView recycler_menu;
RecyclerView.LayoutManager layoutManager;
CollapsingToolbarLayout collapsingToolbarLayout;

ProgressDialog dialog1 ;

String resID = "", resName = "";

ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_food_detail);

    dialog1 = new ProgressDialog(FoodDetail.this);

    mAuth = FirebaseAuth.getInstance();
    imageView = (ImageView) findViewById(R.id.food_img);

    if(getIntent() != null){
        resID = getIntent().getStringExtra("restraunt_id");
        resName = getIntent().getStringExtra("name");
    }

    // init firebase

    collapsingToolbarLayout = findViewById(R.id.collapsing);
    collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppBar);
    collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppBar);
    collapsingToolbarLayout.setTitle(resName);

    // load restuarant list from firebase
    recycler_menu = (RecyclerView) findViewById(R.id.foodmenu_list);
    recycler_menu.setHasFixedSize(true);

    layoutManager = new LinearLayoutManager(this);
    recycler_menu.setLayoutManager(layoutManager);

    loadFoodList();
    loadName();

}

private void loadName() {

    DatabaseReference ref1 = FirebaseDatabase.getInstance().getReference();
    DatabaseReference mostafa = ref1.child("restaurant").child(resID).child("img");

    mostafa.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String imgURL = dataSnapshot.getValue(String.class);
            Picasso.with(getBaseContext()).load(imgURL).fit().into(imageView);
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

private void loadFoodList() {

    ref = FirebaseDatabase.getInstance().getReference().child("menu").child(resID);

    System.out.println("getttts here" + ref.toString());

    adapter = new FirebaseRecyclerAdapter<FoodDetailModel, FoodDetailViewHolder>
            (FoodDetailModel.class,R.layout.food_detail_blueprint, FoodDetailViewHolder.class,ref) {
        @Override
        protected void populateViewHolder(final FoodDetailViewHolder viewHolder, final FoodDetailModel model, final int position) {

            final String x = adapter.getRef(position).toString();

            viewHolder.foodname.setText(model.getName());
            viewHolder.price.setText("₹"+model.getPrice());
            viewHolder.description.setText(model.getDescription());

                String typ = model.getType();

                if (typ.equals("veg")){
                    viewHolder.type.setImageResource(R.drawable.veg);
                } else {
                    viewHolder.type.setImageResource(R.drawable.nonveg);
                }


            viewHolder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    final AlertDialog.Builder alert = new AlertDialog.Builder(FoodDetail.this);
                    alert.setTitle("Delete entry");
                    alert.setMessage("Are you sure you want to delete?");
                    alert.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                        }
                    });
                    alert.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            // close dialog
                            dialog.cancel();
                        }
                    });
                    alert.show();

                }
            });

        }
    };
    recycler_menu.setAdapter(adapter);

}

}

0 个答案:

没有答案