1

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: enter image description here

The ReyclerView is simply cast and I call the adapter on recyclerview, that's why didn't include the code where I defined recyclerview.

Edric
  • 24,639
  • 13
  • 81
  • 91

3 Answers3

0

I am seeing that item getItemCount() dont count list size so please change this code

@Override
public int getItemCount() {
    return 0;
}

to below code

@Override
public int getItemCount() {
    return list.size();
}
shakac
  • 379
  • 4
  • 15
0

FindFriendsAdapter.class

@Override
public int getItemCount() {
    return 0;
}

to

@Override
public int getItemCount() {
    if(List != null) {
        return List.size();
    }
    return 0;
}

By the way,Member variables‘s initials should not be capitalized.

dakun
  • 24
  • 3
  • How can write some please, by default they were not I made them capital as I am using child names Like Full_Name, Status etc in Firebase database –  Mar 29 '20 at 12:05
  • Using annotations should solve your problem. Why not look in the official documents? – dakun Mar 29 '20 at 12:17
  • Can you please include that document link. –  Mar 29 '20 at 12:22
0

If anyone is facing the same issue,there for I am gonna answer the question cause I solve it myself.

There are Two Problems:

First: To include.

@Override
public int getItemCount() {
    return list.size();
}

Second: To make the following changes, as you guys can see I am using Capital letter in start of parameter inside getter and setter changing them to small did my work.

package com.example.sociapp;

public class FindFriends
{
    private String profileimage, Full_Name, Status;


    public FindFriends ( )
    {

    }
    public FindFriends(String profileimage, String fullName, String status)
    {
        this.profileimage = profileimage;
        Full_Name = fullName;
       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 fullName) {
         Full_Name = fullName;
    }

    public String getStatus() {
        return Status;
    }

    public void setStatus(String status) {
       Status = status;
    }
}