为什么我的适配器无法工作且未显示在我的列表视图上?

问题描述 投票:0回答:1

在观看了大量教程并执行完全相同的操作后,我的适配器仍然无法工作。

我的应用程序是一个类似论坛的应用程序,用户可以在其中发布论坛(问题),其他用户可以回答这些问题(就像这个网站一样)。 我正在构建的适配器应该连接到列表视图并显示用户搜索的论坛。

问题是搜索现有论坛后列表视图上没有显示任何内容(我已经尝试了很多代码),我正在寻找原因。

  • 如前所述,我看了很多关于该主题的教程(没有一个对我有帮助)
  • 我尝试在互联网上搜索帮助,但没有找到与我有相同问题的人

提前致谢

适配器代码:

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);
        }
    }


}

片段代码:

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);
    }


}

其 xml 代码:

<?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>
java android-studio listview adapter
1个回答
0
投票

替换这个:

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

与:

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

这是我在您的代码中发现的一个错误。 可能存在其他错误。

© www.soinside.com 2019 - 2024. All rights reserved.