Я хочу искать элемент по названию места, а не по названию. Точно ... Я хочу, чтобы пользователь обыскал его место. Я добавил возможность поиска по пользователю.
Может ли кто-нибудь помочь изменить этот код на поиск пользователя для поиска места? Я буду искать место, и он покажет список пользователей по названию места. Найдите место и покажите список.
package com.example.mfree.Fragment;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import com.example.mfree.Adapter.UserAdapter;
import com.example.mfree.Adapter.UserBloodAdapter;
import com.example.mfree.Model.User;
import com.example.mfree.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;
public class BloodSearchFragment extends Fragment {
RecyclerView recyclerView;
private UserBloodAdapter muserAdapter;
private List<User> mUsers;
EditText search_bar;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_blood_search, container, false);
recyclerView=view.findViewById(R.id.recyler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
search_bar=view.findViewById(R.id.search_bar);
mUsers=new ArrayList<>();
muserAdapter=new UserBloodAdapter(getContext(),mUsers,true);
recyclerView.setAdapter(muserAdapter);
readUsers();
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) {
searchUser(s.toString().toLowerCase());
}
@Override
public void afterTextChanged(Editable s) {
}
});
return view;
}
private void searchUser(String s){
Query query= FirebaseDatabase.getInstance().getReference("Users").orderByChild("username")
.startAt(s)
.endAt(s+"\uf8ff");
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
mUsers.clear();
for (DataSnapshot snapshot:dataSnapshot.getChildren()){
User user=snapshot.getValue(User.class);
mUsers.add(user);
}
muserAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void readUsers(){
DatabaseReference reference=FirebaseDatabase.getInstance().getReference("Users");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(search_bar.getText().toString().equals("")){
mUsers.clear();
for (DataSnapshot snapshot:dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
mUsers.add(user);
}
muserAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
Адаптер ...
package com.example.mfree.Adapter;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.hardware.Camera;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.mfree.Fragment.ProfileFragment;
import com.example.mfree.MainActivity;
import com.example.mfree.Model.User;
import com.example.mfree.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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.ValueEventListener;
import java.util.HashMap;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
public class UserBloodAdapter extends RecyclerView.Adapter<UserBloodAdapter.ViewHolder> {
private Context mContext;
private List<User> mUser;
FirebaseUser firebaseUser;
private Boolean isfragments;
public UserBloodAdapter(Context mContext, List<User> mUser,boolean isfragments) {
this.mContext = mContext;
this.mUser = mUser;
this.isfragments=isfragments;
}
@NonNull
@Override
public UserBloodAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(mContext).inflate(R.layout.blood_item,parent,false);
return new UserBloodAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final UserBloodAdapter.ViewHolder holder, int position) {
firebaseUser= FirebaseAuth.getInstance().getCurrentUser();
final User user=mUser.get(position);
holder.btn_follow.setVisibility(View.VISIBLE);
holder.username.setText(user.getUsername());
holder.fullname.setText(user.getFullname());
holder.fullname.setText(user.getFullname());
holder.areaname.setText(user.getAreaname());
holder.district.setText(user.getDistrict());
holder.blood.setText(user.getBlood());
try {
// Glide.with(mContext).load(user.getImageurl()).into(holder.image_profile);
Glide.with(mContext).setDefaultRequestOptions(new RequestOptions().timeout(30*1000)).load(user.getImageurl()).into(holder.image_profile);
}catch (Exception e){}
isFollowing(user.getId(),holder.btn_follow);
if(user.getId().equals(firebaseUser.getUid())){
holder.btn_follow.setVisibility(View.GONE);
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isfragments) {
SharedPreferences.Editor editor = mContext.getSharedPreferences("PREFS", Context.MODE_PRIVATE).edit();
editor.putString("profileid", user.getId());
editor.apply();
((FragmentActivity) mContext).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new ProfileFragment()).commit();
}else {
Intent intent=new Intent(mContext, MainActivity.class);
intent.putExtra("publisherid",user.getId());
mContext.startActivity(intent);
}
}
});
holder.btn_follow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(holder.btn_follow.getText().toString().equals("Follow")){
FirebaseDatabase.getInstance().getReference().child("Follow").child(firebaseUser.getUid())
.child("following").child(user.getId()).setValue(true);
FirebaseDatabase.getInstance().getReference().child("Follow").child(user.getId())
.child("followers").child(firebaseUser.getUid()).setValue(true);
addNotifications(user.getId());
}
else {
FirebaseDatabase.getInstance().getReference().child("Follow").child(firebaseUser.getUid())
.child("following").child(user.getId()).removeValue();
FirebaseDatabase.getInstance().getReference().child("Follow").child(user.getId())
.child("followers").child(firebaseUser.getUid()).removeValue();
}
}
});
}
private void addNotifications(String userid){
DatabaseReference reference=FirebaseDatabase.getInstance().getReference("Notifications").child(userid);
HashMap<String,Object> hashMap=new HashMap<>();
hashMap.put("userid",firebaseUser.getUid());
hashMap.put("text","started following you");
hashMap.put("postid","");
hashMap.put("ispost",false);
reference.push().setValue(hashMap);
}
@Override
public int getItemCount() {
return mUser.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView username;
public TextView fullname;
public TextView areaname;
public TextView district;
public TextView blood;
public CircleImageView image_profile;
public Button btn_follow;
public ViewHolder(@NonNull View itemView) {
super(itemView);
username=itemView.findViewById(R.id.username);
fullname=itemView.findViewById(R.id.fullname);
areaname=itemView.findViewById(R.id.areaname);
district=itemView.findViewById(R.id.district);
blood=itemView.findViewById(R.id.blood);
image_profile=itemView.findViewById(R.id.image_profile);
btn_follow=itemView.findViewById(R.id.btn_follow);
}
}
public void isFollowing(final String userid, final Button button ){
DatabaseReference reference= FirebaseDatabase.getInstance().getReference().child("Follow").
child(firebaseUser.getUid()).child("following");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.child(userid).exists()){
button.setText("following");
}else {
button.setText("Follow");
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}