无法将类型 java.util.ArrayList 的值转换为 String

问题描述 投票:0回答:2

如果我在此数据库中添加一个部分,它会显示“无法将类型 java.util.ArrayList 的值转换为字符串” 我正在使用 recyclerviewoption 查询仅显示当前用户数据。

我需要显示孩子“列表”。

Firebase 用户:

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

主要活动

 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) {

                    }
                });
            }
        });

我的适配器

 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;
                }
            }
        });

我的模型用户

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;
    }

我尝试添加“long mxid=0;”在每个部分中添加 1。然后我不知道为什么会显示该错误。

java android firebase firebase-realtime-database android-recyclerview
2个回答
0
投票

您收到以下错误:

无法将类型 java.util.ArrayList 的值转换为 String

因为数据库中

List
字段的类型是一个数组,而在您的
ModelUsers
类中是一个字符串,因此错误。如果您需要该字段是一个字符串,就像它在类中一样,那么您必须将数据库中的字段更改为一个字符串而不是一个数组。另一方面,如果您需要字段是一个数组,那么您必须将数据库中的字段更改为
List<WhateverObjectYouHaveThere>
.


0
投票

你的数据库是数组形式,用户类是字符串

© www.soinside.com 2019 - 2024. All rights reserved.