I have pasted all the code below Please someone check it out and tell me what changes should I make.
And I think the problem is with the Child name in firebase. they are by name: Full_Name, Status, profileimage the third one is quite correct but the rest of two are making trouble please check the image at the end of post and my "model" "class", I think the problem is somewhere there.
Below is the xml design I created for layout which I inflate in adapter Class:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:background="@color/colorProfile"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/all_user_post_image"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_marginLeft="10dp"
android:src="@drawable/profile"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<TextView
android:id="@+id/all_user_profile_full_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User Full Name"
android:textAlignment="textStart"
android:textColor="@android:color/background_light"
android:textSize="18sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/all_user_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Status"
android:textAlignment="textStart"
android:textColor="@android:color/background_light"
android:textSize="16sp"
/>
</LinearLayout>
</LinearLayout>
My model class:
package com.example.sociapp;
public class FindFriends
{
private String profileimage, Full_Name, Status;
public FindFriends ( )
{
}
public FindFriends(String profileimage, String Full_Name, String Status)
{
this.profileimage = profileimage;
this.Full_Name = Full_Name;
this.Status = Status;
}
public String getProfileimage() {
return profileimage;
}
public void setProfileimage(String profileimage) {
this.profileimage = profileimage;
}
public String getFull_Name() {
return Full_Name;
}
public void setFull_Name(String Full_Name) {
this.Full_Name = Full_Name;
}
public String getStatus() {
return Status;
}
public void setStatus(String Status) {
this.Status = Status;
}
}
My Adapter Class:
package com.example.sociapp;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
public class FindFriendsAdapter extends RecyclerView.Adapter<FindFriendsAdapter.FindFriendsviewHolder> {
List<FindFriends> List;
Context context;
public FindFriendsAdapter(java.util.List<FindFriends> list, Context context) {
List = list;
this.context = context;
}
@NonNull
@Override
public FindFriendsviewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.all_user_display_layout, parent, false);
return new FindFriendsviewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull FindFriendsviewHolder holder, int position) {
FindFriends findFriends = List.get(position);
Picasso.get().load(findFriends.getProfileimage()).placeholder(R.drawable.profile).into(holder.AllUserProfilePic);
holder.AllUserFullName.setText(findFriends.getFull_Name());
holder.Status.setText(findFriends.getStatus());
}
@Override
public int getItemCount() {
return List.size();
}
public class FindFriendsviewHolder extends RecyclerView.ViewHolder {
CircleImageView AllUserProfilePic;
TextView AllUserFullName;
TextView Status;
public FindFriendsviewHolder(@NonNull View itemView) {
super(itemView);
AllUserProfilePic = itemView.findViewById(R.id.all_user_post_image);
AllUserFullName = itemView.findViewById(R.id.all_user_profile_full_name);
Status = itemView.findViewById(R.id.all_user_status);
}
}
}
The Activity in which I am calling the adapter and model class:
public class FindFriendsActivity extends AppCompatActivity {
private Toolbar mtoolbar;
private List<FindFriends> AllUserSearchResult;
private ImageButton SearchButton;
private EditText SearchInputText;
private RecyclerView SearchResultList;
private DatabaseReference AllUserDatabaseRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_friends);
mtoolbar = (Toolbar) findViewById(R.id.find_friends_appbar_layout);
setSupportActionBar(mtoolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Update Post");
AllUserDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Users");
SearchResultList = (RecyclerView) findViewById(R.id.search_result_list);
SearchResultList.setHasFixedSize(true);
SearchResultList.setLayoutManager(new LinearLayoutManager(this));
SearchButton = (ImageButton) findViewById(R.id.search_friend_button);
SearchInputText = (EditText) findViewById(R.id.search_box_input);
AllUserSearchResult = new ArrayList<>();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
SearchResultList.setHasFixedSize(true);
SearchResultList.setLayoutManager(linearLayoutManager);
SearchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
String SearchBoxInput = SearchInputText.getText().toString();
SearchPeopleAndFriends(SearchBoxInput);
}
});
}
private void SearchPeopleAndFriends(String searchBoxInput)
{
Toast.makeText(this, "Searching", Toast.LENGTH_SHORT).show();
Query searchFriendQuery = AllUserDatabaseRef.orderByChild("Full_Name")
.startAt(searchBoxInput).endAt(searchBoxInput +"\uf8ff");
searchFriendQuery.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
FindFriends findFriends = dataSnapshot1.getValue(FindFriends.class);
AllUserSearchResult.add(findFriends);
}
FindFriendsAdapter findFriendsAdapter = new FindFriendsAdapter(AllUserSearchResult,FindFriendsActivity.this);
SearchResultList.setAdapter(findFriendsAdapter);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
The Firebase node where the Names of my fields I am retrieving is encircle with yellow color:
The ReyclerView is simply cast and I call the adapter on recyclerview, that's why didn't include the code where I defined recyclerview.