无法将java.lang.String类型的对象转换为类型(Firebase,RecyclerView)

时间:2019-10-07 22:20:30

标签: java android firebase-realtime-database android-recyclerview

此刻我在同一页面上同时获得了两个RecyclerView,分别是Breakfast and Lunch RecyclerView,但我遇到以下错误无法将java.lang.String类型的对象转换为com.example类型

突出显示此行

  

userRecordslist.add(ds.getValue(UserRecordsModel.class));

我尝试了几种方法。

但是当我使用此代码时,来自不同记录的数据显示在Breakfast RecyclerView中

shadowPluginVersion

这些是我的Firebase和我的应用程序的屏幕截图。您可以看到来自不同记录的两个数据都显示在同一RecyclerView上。

Database My App

,后来我尝试使用此“新”代码作为数据库参考,应该从Firebase检索的数据未显示在Breakfast Recycler View上,并且我得到了无法转换类型为java的对象.lang.String键入错误

 myRef = FirebaseDatabase.getInstance().
            getReference("UsersRecords").child(FirebaseAuth.getInstance().getCurrentUser().getUid())
            .child(date_record);

我想获取数据并将其显示在“假定” RecyclerView中。请帮忙。

我的PlanMeal活动的此代码:

 myRef = FirebaseDatabase.getInstance().
            getReference("UsersRecords").child(FirebaseAuth.getInstance().getCurrentUser().getUid())
            .child(date_record).child("BreakfastRecord");

这是我的模特:


    //BUTTON
    Button backBtn;
    Button addMealBreakBtn;
    Button addMealLunchBtn;
    Button addMealDinnerBtn;

    //DATABASE
    FirebaseAuth mAuth;
    FirebaseUser currentUser;
    DatabaseReference userRecordRef, myRef,requiredCalorieRef, mylunchRef;

    //TEXT VIEW
    TextView userRequiredCalorie;
    ArrayList<UserRecordsModel> userRecordslist;
    RecyclerView recyclerView, recyclerViewlunch;
    private RecyclerView.Adapter userRecordHolder;

   //DATE
    String date_record ;

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

        date_record = new SimpleDateFormat("yyMMdd", Locale.getDefault()).format(new Date());
        //create a date string.
        String date_n = new SimpleDateFormat("MMM dd, yyyy", Locale.getDefault()).format(new Date());
        //get hold of textview.
        TextView date  = (TextView) findViewById(R.id.datePlanMeal);
        //set it as current date.
        date.setText(date_n);



        //INI VIEWS
        userRequiredCalorie= (TextView) findViewById(R.id.outputPlanMealCalorie);


        //FIREBASE AUTH
        mAuth = FirebaseAuth.getInstance();
        currentUser=mAuth.getCurrentUser();

        //DATABASE REFERENCE
        myRef = FirebaseDatabase.getInstance().
                getReference("UsersRecords").child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                .child(date_record);

        /*mylunchRef=FirebaseDatabase.getInstance().
                getReference("UsersRecords").child(FirebaseAuth.getInstance().getCurrentUser().getUid())
                .child(date_record).child("LunchRecord");*/



        //myRef = FirebaseDatabase.getInstance().getReference("UsersRecords").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
        //mylunchRef = FirebaseDatabase.getInstance().getReference("UsersRecords").child(FirebaseAuth.getInstance().getCurrentUser().getUid());


        //RECYCLER VIEW
        //*********BREAKFAST******************************************//
        recyclerView = findViewById(R.id.userRecordRecylerView);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);

        //ADAPTER
        userRecordslist = new ArrayList<>();
        userRecordHolder = new UserRecordsHolder(userRecordslist);
        recyclerView.setAdapter(userRecordHolder);

        //*********LUNCH******************************************//
        recyclerViewlunch = findViewById(R.id.userRecordRecylerViewLunch);
        LinearLayoutManager manager1 = new LinearLayoutManager(this);
        recyclerViewlunch.setLayoutManager(manager1);
        recyclerViewlunch.setHasFixedSize(true);

        //ADAPTER
        userRecordslist = new ArrayList<>();
        userRecordHolder = new UserRecordsHolder(userRecordslist);
        recyclerViewlunch.setAdapter(userRecordHolder);



        //BUTTON
        addMealBreakBtn = (Button) findViewById(R.id.addMealBreakBtn);
        backBtn = (Button)findViewById(R.id.backBtnPlan) ;


        //**********************DATABASE REFERENCE FOR USER REQUIRED CALORIE***************************//
        requiredCalorieRef = FirebaseDatabase.getInstance().getReference("Users").child(FirebaseAuth.getInstance().getCurrentUser().getUid());
        requiredCalorieRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {


                String userCalorieSuggestion = String.valueOf((dataSnapshot.child("daily calorie").getValue()));
                userRequiredCalorie.setText((userCalorieSuggestion  +"kcal"));

                /*String userCalorieSuggestion = Double.toString((Double) dataSnapshot.child("daily calorie").getValue());
                showDailyCalorie.setText(("Daily Calorie Suggestion:  " + userCalorieSuggestion  +"kcal"));*/
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }

        });


        //BACK BUTTON*************************************************
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent signIn = new Intent(PlanMealUser.this,HomepageUser.class);
                startActivity(signIn);
            }
        });


        //ADD MEAL BUTTONS**********************************************
        addMealBreakBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent breakfast = new Intent(PlanMealUser.this,ViewProduct.class);
                startActivity(breakfast);

            }
        });

        addMealLunchBtn = (Button) findViewById(R.id.addMealLunchBtn);
        addMealLunchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent signIn = new Intent(PlanMealUser.this,ViewProduct_Lunch.class);
                startActivity(signIn);

            }
        });

        addMealDinnerBtn = (Button) findViewById(R.id.addMealDinnerBtn);
        addMealDinnerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent signIn = new Intent(PlanMealUser.this,ViewProduct.class);
                startActivity(signIn);


            }
        });

    }


    @Override
    protected void onStart() {
        super.onStart();
        if (myRef != null) {
            myRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                    if (dataSnapshot.exists()) {
                        userRecordslist = new ArrayList<>();
                        for (DataSnapshot ds : dataSnapshot.getChildren()) {
                            userRecordslist.add(ds.getValue(UserRecordsModel.class));



                        }
                        UserRecordsHolder userRecordHolder = new UserRecordsHolder(userRecordslist);
                        recyclerView.setAdapter(userRecordHolder);
                    }

                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    Toast.makeText(PlanMealUser.this, databaseError.getMessage(),
                            Toast.LENGTH_SHORT).show();
                }
            });

        }
    }


}

这是我的适配器

package com.example.buddymealplanneruser.Child.UserRecords;

public class UserRecordsModel {
    private String foodName;
    private String foodCalorie;

    //constructor

    public UserRecordsModel (String foodName,
                  String foodCalorie
                 )
    {

        this.foodName = foodName;
        this.foodCalorie = foodCalorie;
    }

    public UserRecordsModel(){

    }

    //Getter and Setter

    public String getFoodName() {
        return foodName;
    }

    public void setFoodName(String foodName) {
        this.foodName = foodName;
    }

    public String getFoodCalorie() {
        return foodCalorie;
    }

    public void setFoodCalorie(String foodCalorie) {
        this.foodCalorie = foodCalorie;
    }
}

希望有人可以提供帮助。

1 个答案:

答案 0 :(得分:1)

BreakfastRecord或LunchRecord下需要一个级别:

UserRecords
    UID
        Date
            BreakfastRecord
                1
                    foodCalorie
                    foodName
                2
                    foodCalorie
                    foodName
                3
                    foodCalorie
                    foodName