firebase 未找到setter/field

flvlnr44  于 2023-08-07  发布在  其他
关注(0)|答案(3)|浏览(104)

大家好,我试图从Firebase数据集检索数据,我一直得到这个错误,即使我尝试了StackOverflow的每一个解决方案,我在谷歌上找到了什么,但没有任何好处。
下面我将链接活动、类和数据集。
我是Android的新手,我不明白为什么不工作,因为有些人解决了这个问题,我做了同样的事情,但没有工作。
先谢谢你了。
代码活动

package com.example.ipill;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.GenericTypeIndicator;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FirebaseSearch extends AppCompatActivity {

    private EditText mSearchField;
    private ImageButton mSearchBtn;

    private RecyclerView mResultList;


    private DatabaseReference mUserDatabase;

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

        mUserDatabase = FirebaseDatabase.getInstance().getReference("Users");

        mSearchField = (EditText) findViewById(R.id.search_field);

        mSearchBtn = findViewById(R.id.search_btn);
        mResultList = (RecyclerView)findViewById(R.id.result_list);

        mResultList.setHasFixedSize(true);
        mResultList.setLayoutManager(new LinearLayoutManager(this));

        mSearchBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String searchText = mSearchField.getText().toString();

                firebaseUserSearch(searchText);

            }
        });

    }

    private void firebaseUserSearch(String searchText) {

        Toast.makeText(FirebaseSearch.this, "Started Search", Toast.LENGTH_LONG).show();

        Query firebaseSearchQuery = mUserDatabase.orderByChild("Name").startAt(searchText).endAt(searchText + "\uf8ff");

        FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(

                Users.class,
                R.layout.list_layout,
                UsersViewHolder.class,
                firebaseSearchQuery

        ) {
            @Override
            protected void populateViewHolder(UsersViewHolder viewHolder, Users model, int position) {
                viewHolder.setDetails(model.getName(), model.getSurname());

            }
        };

        mResultList.setAdapter(firebaseRecyclerAdapter);

    }

    // View Holder Class

    public static class UsersViewHolder extends RecyclerView.ViewHolder {

        View mView;

        public UsersViewHolder(View itemView) {
            super(itemView);
            mView = itemView;

        }

        public void setDetails(String name, String surname) {

            TextView user_name = (TextView) mView.findViewById(R.id.name_text);
            TextView user_surname = (TextView) mView.findViewById(R.id.status_text);

            user_name.setText(name);
            user_surname.setText(surname);

        }

    }
}

字符串
数据库:

{
  "Users" : {
    "01" : {
      "Name" : "Alex",
      "Surname" : "Kyto"
    }
  }
}


我的问题是:
在类com.example.ipill.Users上找不到Surname的setter/field(字段/setter区分大小写!)在com.example.ipill.Users类中找不到Name的setter/field(字段/setter区分大小写!)

车型级别

package com.example.ipill;

public class Users {

    public String name, surname;

    public String getName() {
        return name;
    }

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

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public Users(String name, String surname, String status) {
        this.name = name;
        this.surname = surname;

    }

    public Users(){

    }

}

zaqlnxep

zaqlnxep1#

为了能够将POJO类Map到数据库,它需要具有相同的变量名,并且示例变量是大小写敏感的。问题在于,在数据库中,NameSurname的大写字母,而在POJO类中,它们的小写字母。
因此,您需要将数据库更改为以下内容:

{
  "Users" : {
    "01" : {
      "name" : "Alex",
      "surname" : "Kyto"
    }
  }
}

字符串

tzcvj98z

tzcvj98z2#

将您的Users属性更改为与Firebase匹配,如下所示。您的数据库和POJO应该保持一致。

public String Name, Surname; //Capitalize each property

字符串

ubof19bj

ubof19bj3#

我得到这个错误,因为我访问错误的变量(父变量)

public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()){
                   list.add(snapshot.getValue(CategoryModel.class));
                   // Here in list I should have added dataSnapshot.getValue(CategoryModel.class)
               }

            
}

字符串
希望对你有帮助。

相关问题