skip to Main Content

If I add a Section in this database it says "Failed to convert the value of type java.util.ArrayList to String"
I am using recyclerviewoption query to display only the current user data.

I need to display the child "List".

Firebase User:

"Users":{
   "I50JqiuTcXfPR7e6poegk3wnIjp2":{
      "List":[
         1,
         {
            "Sections":"1-1"
         }
      ],
      "Password":"Randomber16",
      "email":"[email protected]",
      "fullName":"Sean Harvey C. Rosarda ",
      "uid":"I50JqiuTcXfPR7e6poegk3wnIjp2",
      "usertype":"Instructor"
   }

Main Activity

 sectionIn = view.findViewById(R.id.SectionIn);
        add = view.findViewById(R.id.add);
        recview = (RecyclerView) view.findViewById(R.id.recview);
        recview.setLayoutManager(new LinearLayoutManager(getContext()));
        user = FirebaseAuth.getInstance().getCurrentUser();
        reference = FirebaseDatabase.getInstance().getReference("Users");
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reference.child(user.getUid()).child("List").addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        String section = sectionIn.getEditText().getText().toString().trim();

                        if (snapshot.exists()){
                            maxid = (snapshot.getChildrenCount());
                        }
                            snapshot.getRef().child(String.valueOf(maxid+1)).child("Sections").setValue(section);

                    }

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

                    }
                });
            }
        });

MyAdapter

 public SectionsAdapter(@NonNull FirebaseRecyclerOptions<ModelUsers> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull myviewholder holder, int position, @NonNull ModelUsers model) {
        holder.sectionTv.setText(model.getList());

        holder.cardv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (model.getList().equals("1-1")){
                    Intent intent = new Intent(v.getContext(), I_MainFragment.class);
                    v.getContext().startActivity(intent);
                } else if (model.getList().equals("1-2")) {
                    Intent intent = new Intent(v.getContext(), I_MainFragmentC2.class);
                    v.getContext().startActivity(intent);
                } else if (model.getList().equals("1-3")) {
                    Intent intent = new Intent(v.getContext(), I_MainFragmentC3.class);
                    v.getContext().startActivity(intent);
                }else if (model.getList().equals("1-4")) {
                    Intent intent = new Intent(v.getContext(), I_MainFragmentC4.class);
                    v.getContext().startActivity(intent);
                }else if (model.getList().equals("1-5")) {
                    Intent intent = new Intent(v.getContext(), I_MainFragmentC5.class);
                    v.getContext().startActivity(intent);
                }else if (model.getList().equals("1-6")) {
                    Intent intent = new Intent(v.getContext(), I_MainFragmentC6.class);
                    v.getContext().startActivity(intent);
                }else if (model.getList().equals("1-7")) {
                    Intent intent = new Intent(v.getContext(), I_MainFragmentC7.class);
                    v.getContext().startActivity(intent);
                }else if (model.getList().equals("1-8")) {
                    Intent intent = new Intent(v.getContext(), I_MainFragmentC8.class);
                    v.getContext().startActivity(intent);
                }else if (model.getList().equals("1-9")) {
                    Intent intent = new Intent(v.getContext(), I_MainFragmentC9.class);
                    v.getContext().startActivity(intent);
                }else if (model.getList().equals("1-10")) {
                    Intent intent = new Intent(v.getContext(), I_MainFragmentC10.class);
                    v.getContext().startActivity(intent);
                }else if (model.getList().equals("1-11")) {
                    Intent intent = new Intent(v.getContext(), I_MainFragmentC11.class);
                    v.getContext().startActivity(intent);
                }else if (model.getList().equals("1-12")) {
                    Intent intent = new Intent(v.getContext(), I_MainFragmentC12.class);
                    v.getContext().startActivity(intent);
                }else if (model.getList().equals("1-13")) {
                    Intent intent = new Intent(v.getContext(), I_MainFragmentC13.class);
                    v.getContext().startActivity(intent);
                }else if (model.getList().equals("1-14")) {
                    Intent intent = new Intent(v.getContext(), I_MainFragmentC14.class);
                    v.getContext().startActivity(intent);
                }else if (model.getList().equals("1-15")) {
                    Intent intent = new Intent(v.getContext(), I_MainFragmentC15.class);
                    v.getContext().startActivity(intent);
                } else if (model.getList().isEmpty()) {
                    return;
                }
            }
        });

MyModelUser

public class ModelUsers {

    String List;
    String fullName;
    String yearSection;
    String email;
    String Password;
    String uid;
    String usertype;
    String studentNumber;

    String week2score, week3score, week4score, week5score, week6score, week7score, week8score, week10score, week11score, week12score, week13score, week14score, week15score, week16score, week17score;



    public ModelUsers() {
    }

    public ModelUsers(String List, String fullName, String yearSection, String email, String password, String uid, String usertype,String studentNumber, String week2score, String week3score, String week4score, String week5score, String week6score, String week7score, String week8score, String week10score, String week11score, String week12score, String week13score, String week14score, String week15score, String week16score, String week17score) {
        this.List = List;
        this.fullName = fullName;
        this.yearSection = yearSection;
        this.email = email;
        Password = password;
        this.uid = uid;
        this.usertype = usertype;
        this.studentNumber = studentNumber;
        this.week2score = week2score;
        this.week3score = week3score;
        this.week4score = week4score;
        this.week5score = week5score;
        this.week6score = week6score;
        this.week7score = week7score;
        this.week8score = week8score;
        this.week10score = week10score;
        this.week11score = week11score;
        this.week12score = week12score;
        this.week13score = week13score;
        this.week14score = week14score;
        this.week15score = week15score;
        this.week16score = week16score;
        this.week17score = week17score;
    }

    public String getList() {
        return List;
    }

    public void setList(String list) {
        List = list;
    }

I tried adding the "long mxid=0;" to add 1 in every section. then I don’t know why it shows that error.

2

Answers


  1. You are getting the following error:

    Failed to convert the value of type java.util.ArrayList to String

    Because the type of the List field in your database is an array, while in your ModelUsers class is a String, hence the error. If you need the field to be a String, as it is in the class, then you have to change the field in the database to be a String and not an array. On the other hand, if you need the field to be an array, then you have to change the field in the database to be a List<WhateverObjectYouHaveThere>.

    Login or Signup to reply.
  2. your database is in the form of an array and the user class is a string

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search