简体   繁体   中英

Retrieving data from Firebase Realtime Database in Android - recyclerview

I'm trying to retrieve data from Firebase Realtime Database and display this data inside recyclerview but I can't do it and I can't understand why because I tried everything(Google / StackOverflow etc) and nothing works.

The code seems to be ok but when I run the app it gives me these messages: No setter/field for Surname found on class com.example.ipill.Users

I checked how to solve the problem and nothing works, I deleted and create a couple of times the database and nothing.

I'm new to Android Studio and I can't understand what I'm doing wrong.

Below I will attach the code and the database. The thing that I don't understand is that when I add a new Object to the database with Name = Alex I can see the new recycler view containing a new item inside, that means that the apps understand that there are more objects with Name = Alex but can't I retrieve the of that object from the database.

Thank you in advance.

CODE ACTIVITY

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


    }

}

}

enter code here

DATABASE:

这是带有数据库的图像在此处输入图片说明 The problem that I have:

W/PersistentConnection: pc_0 - Using an unspecified index. Your data will be downloaded and filtered             
on the client. Consider adding '".indexOn": "Name"' at Users to your security and Firebase Database     
rules for better performance
W/ClassMapper: No setter/field for Surname found on class com.example.ipill.Users (fields/setters are 
case sensitive!)
W/ClassMapper: No setter/field for Name found on class com.example.ipill.Users (fields/setters are 
case sensitive!)
W/m.example.ipil: Accessing hidden field Landroid/view/View;- 
>mAccessibilityDelegate:Landroid/view/View$AccessibilityDelegate; (greylist, reflection, allowed)

Model CLASS

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

}

} 

enter code here

The error message is quite explicit on what you need to do:

Consider adding '".indexOn": "Name"' at Users

So you need to add ".indexOn": "Name" at the Users node in your rules.

So:

{
  "rules": {
    "Users": {
      ".indexOn": "Name"
    }
  }
}

The square brackets are fine ["Name"] , but right now you're defining the index on the wrong level.

您需要创建模型类和内部 getter setter

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM