Я давно пытаюсь сделать панель поиска, чтобы найти какой-либо сервис по названию сервиса в моем списке просмотра.Я сделал некоторые изменения, но до сих пор ничего не работает должным образом.Я сделал метод filter
в своей деятельности ServiceAdapter.java
и создал текст редактирования с именем filterText для этого
ServiceListActivity.java
filterText = (EditText)findViewById(R.id.filterText);
mServicesListView = (ListView)findViewById(R.id.service_list_view);
mServiceAdapter = new ServiceAdapter(ServiceListActivity.this, mServices);
//mServiceAdapter.notifyDataSetChanged();
//mServicesListView.setAdapter(mServiceAdapter);
filterText.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) {
//ServiceListActivity.this.mServiceAdapter.filter(s);
}
@Override
public void afterTextChanged(Editable s) {
String text = filterText.getText().toString().toLowerCase(Locale.getDefault());
mServiceAdapter.filter(text);
}
});
ServiceAdapter.java
public class ServiceAdapter extends ArrayAdapter<Service> {
private final List<Service> mServiceList;
private List<Service> tmp;
private DatabaseReference mDatabaseServices;
private DatabaseReference mDatabaseUsers;
private FirebaseAuth mAuth;
private String mUserId;
private int mUserPoints;
public ServiceAdapter(Context context, List<Service> serviceList) {
super(context, R.layout.service_list_item_view, serviceList);
mServiceList = serviceList;
this.tmp = serviceList;
}
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View rowView = LayoutInflater.from(getContext()).inflate(R.layout.service_list_item_view, parent, false);
final Service service = mServiceList.get(position);
mDatabaseServices = FirebaseDatabase.getInstance().getReference("services");
mDatabaseUsers = FirebaseDatabase.getInstance().getReference("users");
mAuth = FirebaseAuth.getInstance();
mUserId = mAuth.getCurrentUser().getUid();
ImageView serviceIcon = rowView.findViewById(R.id.service_icon);
TextView serviceTitle = rowView.findViewById(R.id.service_title);
TextView serviceResource = rowView.findViewById(R.id.service_resource);
serviceIcon.setImageResource(R.drawable.service_icon);
serviceTitle.setText(service.getTitle());
serviceResource.setText(service.getResource());
mDatabaseUsers.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
User user = snapshot.getValue(User.class);
if (user.getId().equals(mUserId))
mUserPoints = user.getPoints();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return rowView;
}
//Filter class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
mServiceList.clear();
if (charText.length() == 0) {
mServiceList.addAll(tmp);
} else {
for (Service s: tmp) {
if(s.getTitle().toLowerCase(Locale.getDefault()).contains(charText)) {
System.out.println(s.getTitle());
mServiceList.add(s);
}
}
}
notifyDataSetChanged();
}
}