Bazinga
Bazinga

Reputation: 1

Why my adapter isn't working and not showing on my list view?

after watching plenty of tutorials, and doing exactly the same my adapter still won't work.

my application is a forum-like application where users can post forums (questions) and other users can answer them (just like this website). the adapter I am building is supposed to connect to a list view and show the forums that the user searched.

the problem is that nothing is showing on the listview after searching for existing forums (and I've tried many codes) and I am seeking why is that.

Thanks in advance

the adapter code :

package com.example.mathmate.Adapters;

import android.content.Context;
import android.net.Uri;
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.bumptech.glide.Glide;
import com.example.mathmate.Models.Forum;
import com.example.mathmate.Models.User;
import com.example.mathmate.R;
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.Query;
import com.google.firebase.database.ValueEventListener;

import java.util.List;

public class ForumAdapter extends RecyclerView.Adapter<ForumAdapter.ViewHolder> {

    private Context context;
    private List<Forum> forums;

    public ForumAdapter(Context context, List<Forum> forums) {
        this.context = context;
        this.forums = forums;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(context).inflate(R.layout.search_forum_row, parent, false);
        return new ForumAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        final Forum forum = forums.get(position);

        holder.title.setText(forum.getTitle());
        holder.subject.setText(forum.getSubject());

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Registered Users");
        Query query = reference.orderByValue().equalTo(forum.getAuthorUid());
        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                    User user = dataSnapshot.getValue(User.class);
                    assert user != null;
                    Uri uriImage = Uri.parse(user.getUri());
                    Glide.with(context).load(uriImage).placeholder(R.drawable.default_pfp).into(holder.profilePicture);
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
            }
        });

        holder.itemView.setOnClickListener(v -> {
            // TODO : move the user to the forum activity
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView title, subject;
        public ImageView profilePicture;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            title = itemView.findViewById(R.id.title_et);
            subject = itemView.findViewById(R.id.subject_et);
            profilePicture = itemView.findViewById(R.id.profile_picture);
        }
    }


}

the fragment code :

package com.example.mathmate.Fragments;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mathmate.Adapters.ForumAdapter;
import com.example.mathmate.Models.Forum;
import com.example.mathmate.R;
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.Query;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;


public class ForumsFragment extends Fragment {

    // widgets
    private EditText search_bar;
    private RecyclerView recyclerView;

    // vars
    private List<Forum> mForumList;
    private DatabaseReference database;
    private ForumAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_forums, container, false);

        search_bar = v.findViewById(R.id.search_bar);
        database = FirebaseDatabase.getInstance().getReference("Forums");
        recyclerView = v.findViewById(R.id.recyclerView);

        initTextListener("title");







        return v;
    }

    private void initTextListener(String parameter) {
        mForumList = new ArrayList<>();

        search_bar.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String text = search_bar.getText().toString().toLowerCase(Locale.getDefault());
                searchForMatch(text, parameter);
            }
        });
    }

    private void searchForMatch(String keyword, String parameter) {
        mForumList.clear();

        if (TextUtils.isEmpty(keyword)) {
            ReadAllUsers();
        } else {
            Query query = database.orderByChild(parameter).startAt(keyword);

            query.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                        mForumList.add(dataSnapshot.getValue(Forum.class));

                        // update the users list view
                        updateForumList();
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });
        }
    }

    private void ReadAllUsers() {
        database.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                    Forum forum = dataSnapshot.getValue(Forum.class);
                    mForumList.add(forum);
                }
                updateForumList();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
            }
        });
    }

    private void updateForumList() {
        adapter = new ForumAdapter(getContext(), mForumList);
        recyclerView.setAdapter(adapter);
    }


}

its xml code :

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/darkBG"
    tools:context=".Fragments.ForumsFragment">


    <LinearLayout
        android:id="@+id/linearLayout7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:gravity="center"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="UselessParent">

        <EditText
            android:id="@+id/search_bar"
            android:layout_width="325dp"
            android:layout_height="48dp"
            android:layout_weight="1"
            android:autofillHints=""
            android:background="@drawable/corner_radius"
            android:ems="10"
            android:hint="@string/search"
            android:inputType="text"
            android:paddingStart="5dp"
            android:text=""
            android:textColor="@color/white"
            android:textColorHint="#9C9C9C"
            android:textSize="13sp"
            tools:ignore="RtlSymmetry" />

        <ImageButton
            android:id="@+id/search_btn"
            android:layout_width="48dp"
            android:layout_height="48dp"
            android:layout_marginStart="10dp"
            android:layout_weight="1"
            android:background="@drawable/corner_radius"
            android:backgroundTint="@color/darkBG"
            android:contentDescription="@string/todo"
            android:src="@drawable/search" />

    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="5dp"
        android:layout_weight="1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout7"
        tools:listitem="@layout/search_forum_row" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </ScrollView>


</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Views: 42

Answers (1)

Hammad
Hammad

Reputation: 1

Replace this:

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

With:

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

This is one mistake that I found in your code. Other errors may exist.

Upvotes: 0

Related Questions