I am new to android development and am trying to bind recycler View with setAdapter but am getting this error "Cannot resolve method 'setAdapter' in 'ConstraintLayout' "
my mainActivity.java code is:
my main objective is to use ActivityMainBinding to bind recyclerView with setAdapter
package com.example.baatcheet;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.example.baatcheet.usersAdapter.*;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.example.baatcheet.databinding.ActivityMainBinding;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
FirebaseDatabase database;
ArrayList<User> users;
usersAdapter UserAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
database = FirebaseDatabase.getInstance();
users = new ArrayList<>();
UserAdapter = new usersAdapter(this, users);
binding.recyclerView.setAdapter(UserAdapter);
database.getReference().child("users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
users.clear();
for(DataSnapshot snapshot1 : snapshot.getChildren()){
User user = snapshot1.getValue(User.class);
users.add(user);
}
UserAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.search:
Toast.makeText(this,"Search Clicked" , Toast.LENGTH_SHORT).show();
break;
case R.id.settings:
Toast.makeText(this , " setting Clicked" , Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.top_menu,menu);
return super.onCreateOptionsMenu(menu);
}
}
this is the part where i am getting an error -> binding.recyclerView.setAdapter(UserAdapter);
and it tells me to rename reference what does this actually mean?
the activity_main.xml file is:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@ id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@ id/bottomNavigationView2"
android:layout_width="match_parent"
android:layout_height="0dp"
app:elevation="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/menu">
</com.google.android.material.bottomnavigation.BottomNavigationView>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@ id/bottomNavigationView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/row_conversation"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
the users.java code is:
package com.example.baatcheet;
public class User {
private String uid,name,phoneNumber,profileImage;
public User(){
}
public User(String uid, String name, String phoneNumber, String profileImage) {
this.uid = uid;
this.name = name;
this.phoneNumber = phoneNumber;
this.profileImage = profileImage;
}
public void setUid(String uid) {
this.uid = uid;
}
public void setName(String name) {
this.name = name;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public void setProfileImage(String profileImage) {
this.profileImage = profileImage;
}
public String getUid() {
return uid;
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getProfileImage() {
return profileImage;
}
}
the usersAdapter.java code is:
package com.example.baatcheet;
import android.content.Context;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.baatcheet.databinding.RowConversationBinding;
import java.util.ArrayList;
public class usersAdapter extends RecyclerView.Adapter<usersAdapter.UsersViewHolder>{
Context context;
ArrayList<User> users;
public usersAdapter(Context context , ArrayList<User> users){
this.context = context;
this.users = users;
}
{
}
@NonNull
@Override
public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.row_conversation,parent,false);
return new UsersViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull UsersViewHolder holder, int position) {
User user = users.get(position);
holder.binding.userName.setText(user.getName());
}
@Override
public int getItemCount() {
return users.size();
}
public class UsersViewHolder extends RecyclerView.ViewHolder{
RowConversationBinding binding;
public UsersViewHolder(@NonNull View itemView) {
super(itemView);
binding = RowConversationBinding.bind(itemView);
}
}
}
CodePudding user response:
In your layout file, it is the root ConstraintLayout
that has the @ id/recyclerView
id:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@ id/recyclerView" ... />
You should move this id to the RecyclerView
itself:
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/recyclerView"
... />
Once you do this, binding.recyclerView
will refer to the RecyclerView
instead of the ConstraintLayout
.
If you need to access the ConstraintLayout
via the view binding for some other reason, you can use binding.root
.