如何实现搜索视图以按用户的城市/名称过滤用户?

时间:2019-04-24 22:57:51

标签: java android android-recyclerview

我无法解决如何实现搜索视图以过滤我现在拥有的代码的问题。此代码创建一个新的适配器,该适配器获取用户名,城市及其图像。我做了一个搜索视图,但是我不知道如何将其实现为这段代码,我看过的大多数教程都有自己的适配器,但是我在这里创建了一个。

我尝试了一些教程,但没有成功。

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

}

我想知道如何使用搜索视图根据用户的姓名/城市来搜索用户,我不确定该怎么做。

1 个答案:

答案 0 :(得分:0)

首先,您需要在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;
}