0

Non-static method cannot be referenced from a non-static context. The problem is coming from my PersonHolder.setPerson(person) with setPerson being underlined. Is this because the setPerson is inside of my onCreate?

I have 3 classes and 4 activity_layouts.

  • my activity_main class is just for the menu activity_main - just a menu
  • Person class
  • ReadFirebase class attached to my activity_read_firebase which is meant to pull the data from firebase and put it into my recycler.
  • WriteToFirebase class used to send data to the firebase.

The last activity is my firebase_item_file that contains the items I require for my recycler in my activity_write_to_firebase.

public class ReadFirebase extends AppCompatActivity {

    private FirebaseRecyclerAdapter<Player, PersonHolder> firebaseRecyclerAdapter;

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

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        DatabaseReference roofRef = FirebaseDatabase.getInstance().getReference();
        Query query = roofRef.child("adult").child("male");

        FirebaseRecyclerOptions<Person> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<Person>()
                .setQuery(query, Person.class)
                .build();

        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Person, PersonHolder>(firebaseRecyclerOptions) {
            @Override
            protected void onBindViewHolder(@NonNull PersonHolder personHolder, int i, @NonNull Person person) {
                PersonHolder.setPerson(person);
            }

            @NonNull
            @Override
            public PersonHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.firebase_item_file, parent, false);

                return new PersonHolder(view);
            }
        };
        recyclerView.setAdapter(firebaseRecyclerAdapter);

    }



 private class PersonHolder extends RecyclerView.ViewHolder {
        private TextView name1;
        private TextView name2;
        private TextView name3;
        private TextView age1;
        private TextView age2;
        private TextView age3;

        PersonHolder(View itemView){
            super(itemView);
            pNameOne = itemView.findViewById(R.id.name1);
            pNameTwo = itemView.findViewById(R.id.name2);
            pNameThree = itemView.findViewById(R.id.name3);
            pAgeOne = itemView.findViewById(R.id.age1);
            pAgeTwo = itemView.findViewById(R.id.age2);
            pAgeThree = itemView.findViewById(R.id.age3);
        }

        void setPerson(Person person){

            String pName1 = person.getName();
            pNameOne.setText(playerName1);
            String pName2 = person.getName();
            pNameTwo.setText(playerName2);
            String pName3= person.getName();
            pNameThree.setText(playerName3);
            int pAge1= person.getAge();
            pAgeOne.setText(Integer.toString(personAge1));
            int pAge2 = person.getAge();
            pAgeTwo.setText(Integer.toString(personAge2));
            int pAge3 = person.getAge();
            pAgeThree.setText(Integer.toString(personAge3));

        }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Sean O
  • 238
  • 3
  • 14

1 Answers1

1

Non-static method cannot be referenced from a non-static context.

You are trying to access method setPerson using class PersonHolder in onBindViewHolder

You should use instance of PersonHolder which you get in onBindViewHolder as parameter

@Override
protected void onBindViewHolder(@NonNull PersonHolder personHolder, int i, @NonNull Person person) {
          personHolder.setPerson(person);
}
Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51