код для моего класса адаптера: publi c class friendsAd расширяет RecyclerView.Adapter реализует Filterable {
ArrayList<friendsGs> mfriendsGs = new ArrayList<friendsGs>();
ArrayList<friendsGs> exampleListFull = new ArrayList<friendsGs>();
private FirebaseDatabase mFirebaseDatabase;
private DatabaseReference mDatabaseRef;
private ChildEventListener mChildEvent;
public friendsAd (){
mFirebaseDatabase = FirebaseDatabase.getInstance();
mDatabaseRef = mFirebaseDatabase.getReference().child("users");
mChildEvent = new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
friendsGs pp = dataSnapshot.getValue(friendsGs.class);
pp.setUid(dataSnapshot.getKey());
mfriendsGs.add(pp);
notifyItemInserted( mfriendsGs.size() - 1);
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
};
mDatabaseRef.addChildEventListener(mChildEvent);
}
@NonNull
@Override
public friendsVh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
View itemView = LayoutInflater.from(context)
.inflate(R.layout.friends, parent, false);
return new friendsVh(itemView);
}
@Override
public void onBindViewHolder(@NonNull friendsVh holder, int position) {
friendsGs post = mfriendsGs.get(position);
holder.userName.setText(post.getFullName());
holder.time.setText(post.getStatus());
Picasso.get().load(post.getProfileImage()).into(holder.profileImage);
}
@Override
public int getItemCount() {
return mfriendsGs.size();
}
@Override
public Filter getFilter() {
return exampleFilter;
}
private Filter exampleFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<friendsGs> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0) {
filteredList.addAll(exampleListFull);
} else {
String filterPattern = constraint.toString().toLowerCase().trim();
for (friendsGs item : exampleListFull) {
if (item.getFullName().toLowerCase().contains(filterPattern)) {
filteredList.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
// mfriendsGs.clear();
mfriendsGs.addAll((List) results.values);
notifyDataSetChanged();
}
};
public class friendsVh extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView time, userName;
CircleImageView profileImage;
ImageView postimage;
public friendsVh(@NonNull View itemView) {
super(itemView);
time = itemView.findViewById(R.id.friend_status);
userName = itemView.findViewById(R.id.friend_user_name);
profileImage = itemView.findViewById(R.id.friend_profile_image);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int position = getAdapterPosition();
friendsGs discpostClick = mfriendsGs.get(position);
Intent intent = new Intent(v.getContext(), Ffriends_activity.class);
intent.putExtra("Clickable", discpostClick);
v.getContext().startActivity(intent);
}
}
}
это код для моего основного активность
publi c класс MainActivity расширяет AppCompatActivity {
SearchView searchView;
RecyclerView recyclerView2;
friendsAd friendsAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchView =findViewById(R.id.search_view);
friendsAd adapter3 = new friendsAd();
recyclerView2 = findViewById(R.id.friends_result);
recyclerView2.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager3 = new LinearLayoutManager(this);
linearLayoutManager3.setReverseLayout(true);
linearLayoutManager3.setStackFromEnd(true);
recyclerView2.setLayoutManager(linearLayoutManager3);
recyclerView2.setAdapter(adapter3);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
friendsAd.getFilter().filter(newText);
return false;
}
});
}
}
всякий раз, когда я начинаю связывать в окне поиска, приложение вылетает, я пробовал методы например, Как добавить поисковый фильтр в RecyclerView из firebase duplicate