开发者社区> 问答> 正文

Android FirebaseRecyclerView中实现搜索用户功能的问题

我正在寻求实现搜索功能。当它运行时,logcat告诉我:

E / RecyclerView:未连接适配器;跳过布局。

我搜索了SO和网络,答案告诉我,我需要设置适配器和线性布局管理器。正如您将在我的代码中看到的那样,我同时设置了两者。Alex Mamo(@AlexMamo)在这里进行的工作似乎为我指明了正确的方向,但我仍然遇到错误。

我的活动

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.MyApp.Objects.ParticipantsObject;
import com.MyApp.R;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;



public class ParticipantsActivity extends AppCompatActivity {

    private EditText mSearchField;
    private ImageButton mSearchBtn;
    private RecyclerView mResultList;
    private DatabaseReference mUserDatabase;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_neighbors);

        mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child("Participants");
        mSearchField = (EditText) findViewById(R.id.search_field);
        mSearchBtn = (ImageButton) findViewById(R.id.search_btn);
        mResultList = (RecyclerView) findViewById(R.id.result_list);
        mResultList.setHasFixedSize(true);
        mResultList.setLayoutManager(new LinearLayoutManager(this));

        mSearchBtn.setOnClickListener(view -> {

            String searchText = mSearchField.getText().toString();
            firebaseUserSearch(searchText);

        });

    }

    private void firebaseUserSearch(String searchText) {
        Toast.makeText(ParticipantsActivity.this, "Started Search", Toast.LENGTH_LONG).show();
        Query firebaseSearchQuery = mUserDatabase.orderByChild("name").startAt(searchText);


        FirebaseRecyclerOptions<ParticipantsObject> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<ParticipantsObject>()
                .setQuery(firebaseSearchQuery, ParticipantsObject.class)
                .build();

        class UserHolder extends RecyclerView.ViewHolder {
            private TextView imageThumbTextView, nameTextView     
            UserHolder(View itemView) {
                super(itemView);
                imageThumbTextView = itemView.findViewById(R.id.profile_image);
                nameTextView = itemView.findViewById(R.id.name_text);
            }


            void setUsers(ParticipantsObject participantsObject) {
                String imageThumb = driverObject.getThumb_image();
                imageThumbTextView.setText(imageThumb);
                String name = participantsObject.getName();
                nameTextView.setText(name);
            }
        }

        FirebaseRecyclerAdapter<ParticipantsObject, UserHolder> firebaseRecyclerAdapter;

        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ParticipantsObject, UserHolder>(firebaseRecyclerOptions) {
            @Override
            protected void onBindViewHolder(@NonNull UserHolder userHolder, int position, @NonNull ParticipantsObject participantsObject) {
                userHolder.setUsers(participantsObject);
            }

            @Override
            public UserHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_layout, parent, false);

                return new UserHolder(view);
            }
        };
        mResultList.setAdapter(firebaseRecyclerAdapter);
        firebaseRecyclerAdapter.startListening();

    }

} 

我的活动XML文件

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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="#ffffff"
        tools:context="com.MyApp.ParticipantsActivity">


        <TextView
            android:id="@+id/heading_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="30dp"
            android:text="Firebase Search"
            android:textColor="#555555"
            android:textSize="24sp" />

        <EditText
            android:id="@+id/search_field"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/heading_label"
            android:layout_below="@+id/heading_label"
            android:layout_marginRight="20dp"
            android:layout_marginTop="20dp"
            android:layout_toStartOf="@+id/search_btn"
            android:background="@drawable/search_layout"
            android:ems="10"
            android:hint="Search here"
            android:inputType="textPersonName"
            android:paddingBottom="10dp"
            android:paddingLeft="20dp"
            android:paddingRight="20dp"
            android:paddingTop="10dp"
            android:textColor="#999999"
            android:textSize="16sp" />

        <ImageButton
            android:id="@+id/search_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/search_field"
            android:layout_alignParentEnd="true"
            android:layout_alignTop="@+id/search_field"
            android:layout_marginRight="30dp"
            android:background="@android:color/background_light"
            app:srcCompat="@mipmap/search_button" />


    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/result_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/search_field"
            android:layout_marginTop="50dp">
    </androidx.recyclerview.widget.RecyclerView>

    </RelativeLayout>

我的清单布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/profile_image"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        app:srcCompat="@mipmap/ic_default_user" />

    <TextView
        android:id="@+id/name_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="14dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="50dp"

        android:layout_marginEnd="213dp"
        android:layout_marginRight="20dp"
        android:layout_toEndOf="@+id/profile_image"
        android:text="Username"
        android:textColor="#555555"
        android:textSize="16sp" />

</RelativeLayout>

问题来源:Stack Overflow

展开
收起
montos 2020-03-23 16:08:37 710 0
1 条回答
写回答
取消 提交回答
  • 您的代码仅在按下按钮后才设置适配器。因为您没有在第一次设置RecyclerView 之前在屏幕上进行渲染,所以没有设置它,所以它将通过在日志中看到的消息来警告您。RecyclerView在渲染时必须连接一个适配器,以便显示任何内容。

    回答来源:Stack Overflow

    2020-03-23 16:08:53
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载