繁体   English   中英

如何实施searchview以按城市/名称过滤用户?

[英]How to implement searchview to filter users by their City/Name?

我不知道如何实现搜索视图来过滤我现在的代码。 此代码创建一个新的适配器,它获取用户名,城市及其图像。 我已经做了一个搜索视图,但我不知道如何实现它到这个代码,我看过的大多数教程有自己的适配器,但我在这里创建一个。

我尝试了几个教程但没有成功。

public class SearchFragment extends Fragment {
private RecyclerView usersList;
private RecyclerView.Adapter usersListAdapter;
private DatabaseReference databaseReference;
private DatabaseReference userDatabase;
private DatabaseReference checkName;
private FirebaseAuth firebaseAuth;
private String currentUserID;
private View view;
private SearchView searchID;


public SearchFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view =  inflater.inflate(R.layout.fragment_search, container, false);
    usersList = view.findViewById(R.id.search_users);
    searchID = view.findViewById(R.id.search);
    firebaseAuth = FirebaseAuth.getInstance();
    currentUserID = firebaseAuth.getCurrentUser().getUid();


    databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
    userDatabase = FirebaseDatabase.getInstance().getReference();
    checkName = userDatabase.child("Users").child(currentUserID).child("name");
    usersList.setHasFixedSize(true);
    usersList.setLayoutManager(new LinearLayoutManager(getContext()));


    return view;
}
@Override
public void onStart() {
    super.onStart();



    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
            final FirebaseRecyclerOptions<Users> options = new FirebaseRecyclerOptions.Builder<Users>()
                    .setQuery(databaseReference, Users.class)
                    .build();

            FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Users, SearchFragment.UsersViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull SearchFragment.UsersViewHolder holder, int position, @NonNull final Users users) {
                    String checkID = currentUserID;
                    String checkID1 = dataSnapshot.child("").getValue().toString();



                        holder.setName(users.getName());
                        holder.setCity(users.getCity());
                        holder.setImage(users.getImage());



                    final String user_ID = getRef(position).getKey();
                    final String user_Name = users.getName();
                    final String user_Img = users.getImage();

                    /*if(user_ID == checkID){
                        holder.setIsRecyclable(false);
                        holder.mView.setVisibility(View.INVISIBLE);
                        holder.mView.setVisibility(View.GONE);

                        return;
                    }*/

                    holder.mView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            CharSequence pickOption[] = new CharSequence[]{"View Profile", "Send Message"};

                            final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                            builder.setTitle("Select an Option");
                            builder.setItems(pickOption, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {

                                    if (i == 0){
                                        Intent otherProfileIntent = new Intent(getActivity(), OthersProfileActivity.class);
                                        otherProfileIntent.putExtra("user_ID", user_ID);
                                        otherProfileIntent.putExtra("user_Name", user_Name);
                                        otherProfileIntent.putExtra("user_Img", user_Img);
                                        startActivity(otherProfileIntent);

                                    }
                                    if (i == 1){
                                        Intent messageIntent = new Intent(getActivity(), MessageActivity.class);
                                        messageIntent.putExtra("user_ID", user_ID);
                                        messageIntent.putExtra("user_Name", user_Name);
                                        messageIntent.putExtra("user_Img", user_Img);
                                        startActivity(messageIntent);
                                    }

                                }
                            });

                            builder.show();


                        }
                    });

                }

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

                    return new SearchFragment.UsersViewHolder(view);
                }
            };
            usersList.setAdapter(adapter);
            adapter.startListening();
        }

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

        }
    });


}

@Override
public void onStop() {
    super.onStop();
}

public static class UsersViewHolder extends RecyclerView.ViewHolder {
    View mView;

    public UsersViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }

    public void setName(String name) {
        TextView userNameView = mView.findViewById(R.id.single_userName);
        userNameView.setText(name);
    }
    public void setCity(String name) {
        TextView userNameView = mView.findViewById(R.id.single_userStatus);
        userNameView.setText(name);
    }

    public void setImage(String image) {
        CircleImageView userImage = mView.findViewById(R.id.single_userImg);
        if(!image.equals("default")) {
            Picasso.get().load(image).into(userImage);
        }
    }
}

}

我想知道如何使用搜索视图根据他们的名字/城市搜索用户,我只是不知道如何。

首先,您需要在recyclerView中的适配器中实现过滤器

public void setFilter(List<Model> listItem)
{
    modelList = new ArrayList<>();
    modelList.addAll(listItem);
    notifyDataSetChanged();
}

然后在你的活动中我们必须实现这个接口

implements SearchView.OnQueryTextListener

最后覆盖这个方法

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.item, menu);
    MenuItem menuItem = menu.findItem(R.id.searchId);
    SearchView searchView = (SearchView) menuItem.getActionView();
    searchView.setOnQueryTextListener(this);
    return true;
}

@Override
public boolean onQueryTextSubmit(String s) {
    return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    String userInput = newText;
    List<Model> newList = new ArrayList<>();
    for(Model name:modelObject)
    {
        if(name.getName().contains(userInput))
            newList.add(name);
    }
    ad.setFilter(newList);
    return false;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM