Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 我正在尝试按名称搜索用户,并将一些数据加载到Recycleview。但代码不起作用。谁能帮帮我吗_Android_Firebase_Firebase Realtime Database_Android Recyclerview - Fatal编程技术网

Android 我正在尝试按名称搜索用户,并将一些数据加载到Recycleview。但代码不起作用。谁能帮帮我吗

Android 我正在尝试按名称搜索用户,并将一些数据加载到Recycleview。但代码不起作用。谁能帮帮我吗,android,firebase,firebase-realtime-database,android-recyclerview,Android,Firebase,Firebase Realtime Database,Android Recyclerview,我已经粘贴了下面的所有代码,请有人检查一下,告诉我应该做什么更改 我认为问题在于firebase中的子名称。他们的名字是:全名,状态,档案图片第三个是非常正确的,但其余两个都在制造麻烦,请检查文章末尾的图片和我的“模型”类,我想问题在哪里 下面是我为layout创建的xml设计,我在adapter类中对其进行了扩展: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://sc

我已经粘贴了下面的所有代码,请有人检查一下,告诉我应该做什么更改

我认为问题在于firebase中的子名称。他们的名字是:全名,状态,档案图片第三个是非常正确的,但其余两个都在制造麻烦,请检查文章末尾的图片和我的“模型”类,我想问题在哪里

下面是我为layout创建的xml设计,我在adapter类中对其进行了扩展:

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


        }
    }
}

我的适配器类:

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


        }
    }
}

package com.example.sociapp;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ImageView;
导入android.widget.TextView;
导入androidx.annotation.NonNull;
导入androidx.recyclerview.widget.recyclerview;
导入com.squareup.picasso.picasso;
导入java.util.List;
导入de.hdodenhof.circleimageview.circleimageview;
公共类FindFriendAdapter扩展了RecyclerView.Adapter{
名单;
语境;
公共FindFriendAdapter(java.util.List,上下文){
列表=列表;
this.context=上下文;
}
@非空
@凌驾
public FindFriendViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
视图=布局更平坦。从(上下文)。充气(R.layout.all\u user\u display\u layout,parent,false);
返回新的FindFriendViewHolder(视图);
}
@凌驾
public void onBindViewHolder(@NonNull FindFriendViewHolder,int位置){
FindFriends FindFriends=List.get(位置);
Picasso.get().load(findFriends.getProfileimage()).placeholder(R.drawable.profile).into(holder.AllUserProfilePic);
holder.AllUserFullName.setText(findFriends.getFull_Name());
holder.Status.setText(findFriends.getStatus());
}
@凌驾
public int getItemCount(){
返回List.size();
}
公共类FindFriendViewHolder扩展了RecyclerView.ViewHolder{
CircleImageView AllUserProfilePic;
TextView AllUserFullName;
文本视图状态;
public findFriendViewHolder(@NonNull View itemView){
超级(项目视图);
AllUserProfilePic=itemView.findViewById(R.id.all\u user\u post\u image);
AllUserFullName=itemView.findViewById(R.id.all\u user\u profile\u full\u name);
Status=itemView.findviewbyd(R.id.all\u user\u Status);
}
}
}
我在其中调用适配器和模型类的活动:

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


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) {

         }
     });
    }
}

公共类FindFriendsActivity扩展了AppCompatActivity{
私有工具栏mtoolbar;
私有列表搜索结果;
私人图像按钮搜索按钮;
私有编辑文本搜索输入文本;
私人RecyclerView SearchResultList;
私有数据库引用AllUserDatabaseRef;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u find\u friends);
mtoolbar=(工具栏)findViewById(R.id.find\u friends\u appbar\u布局);
设置支持操作栏(mtoolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(“更新帖子”);
AllUserDatabaseRef=FirebaseDatabase.getInstance().getReference().child(“用户”);
SearchResultList=(RecyclerView)findViewById(R.id.search\u result\u list);
SearchResultList.setHasFixedSize(true);
SearchResultList.setLayoutManager(新的LinearLayoutManager(this));
SearchButton=(ImageButton)findViewById(R.id.search\u friend\u button);
SearchInputText=(EditText)findViewById(R.id.search\u box\u input);
AllUserSearchResult=new ArrayList();
LinearLayoutManager LinearLayoutManager=新的LinearLayoutManager(此);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
SearchResultList.setHasFixedSize(true);
SearchResultList.setLayoutManager(linearLayoutManager);
SearchButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v)
{
字符串SearchBoxInput=SearchInputText.getText().toString();
SearchPeopleAndFriends(SearchBoxInput);
}
});
}
private void SearchPeopleAndFriends(字符串searchBoxInput)
{
Toast.makeText(这是“搜索”,Toast.LENGTH_SHORT).show();
查询searchFriendQuery=AllUserDatabaseRef.orderByChild(“全名”)
.startAt(searchBoxInput).endAt(searchBoxInput+“\uf8ff”);
searchFriendQuery.addValueEventListener(新的ValueEventListener(){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
if(dataSnapshot.exists())
{
对于(DataSnapshot dataSnapshot1:DataSnapshot.getChildren()){
FindFriends FindFriends=dataSnapshot1.getValue(FindFriends.class);
添加(findFriends);
}
FindFriendAdapter FindFriendAdapter=新的FindFriendAdapter(AllUserSearchResult,FindFriendActivity.this);
SearchResultList.setAdapter(FindFriendAdapter);
}
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){
}
});
}
}
我正在检索的字段名称所在的Firebase节点被黄色包围:


ReyclerView是简单的强制转换,我调用了recyclerview上的适配器,这就是为什么没有包含我定义recyclerview的代码。

我看到项目getItemCount()不计算列表大小,所以请更改此代码

@Override
public int getItemCount() {
    return 0;
}
到下面的代码

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

我发现item getItemCount()不计算列表大小,因此请更改此代码

@Override
public int getItemCount() {
    return 0;
}
到下面的代码

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

FindFriendAdapter.class

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

顺便