Я нашел много решений, которые они ищут, только в заданном списке, или они могут искать через searchview. Но в моем случае я должен искать, используя редактируемый текст.Выполните поиск в editText в программе RecyclelerView, где я получаю элементы из API с помощью retrofit. Вот мой код адаптера и программы RecyclerView. Я хочу отфильтровать список точек сбора данных по названию.спасибо заранее
private List<CollectionPoint> collectionPointList;
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView collectionPointId, collectionPointName;
private int collectionPointID;
MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
collectionPointId = (TextView) itemView.findViewById(R.id.txtCollectionPointID);
collectionPointName = (TextView) itemView.findViewById(R.id.txtCollectionPointName);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(itemView.getContext(), Appointments.class);`
intent.putExtra("CollectionPointID", collectionPointID);
Appointments.CollectionPointID = collectionPointID;
FragmentProcedure.CollectionPointID = collectionPointID;
itemView.getContext().startActivity(intent);
}
}
public CollectionPointAdapter(List<CollectionPoint> collectionPointList1) {
this.collectionPointList = collectionPointList1;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.collectionpointlistitems, viewGroup, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
CollectionPoint collectionPoint = collectionPointList.get(position);
holder.collectionPointId.setText("ID - " + String.valueOf(collectionPoint.getID()));
holder.collectionPointName.setText(collectionPoint.getName());
holder.collectionPointID = collectionPointList.get(position).getID();
}
@Override
public int getItemCount() {
return collectionPointList.size();
}
public void filterList(ArrayList<CollectionPoint> filteredList) {
collectionPointList = filteredList;
notifyDataSetChanged();
}
Активность:
RecyclerView recyclerView;
public static GetCollectionPointByUserIDResponse getCollectionPointByUserIDResponse = new GetCollectionPointByUserIDResponse();
private List<CollectionPoint> collectionPointList = new ArrayList<>();
CollectionPointAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collection_point);
getCollectionPoints();
recyclerView = findViewById(R.id.collectionPointRecyclerView);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext(), LinearLayoutManager.VERTICAL));
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
TextView logout = findViewById(R.id.logout);
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(CollectionPoints.this, Login.class);
startActivity(intent);
}
});
EditText editText = findViewById(R.id.search);
/* editText.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) {
}
@Override
public void afterTextChanged(Editable s) {
filter(s.toString());
}
});*/
}
private void filter(String text) {
ArrayList<CollectionPoint> filteredList = new ArrayList<>();
for (CollectionPoint item : filteredList) {
if (item.getName().toLowerCase().contains(text.toLowerCase())) {
filteredList.add(item);
}
}
mAdapter.filterList(filteredList);
}
private void getCollectionPoints() {
GetCollectionPointByUserIDResquest request = new GetCollectionPointByUserIDResquest();
request.Token = Login.session.Token;
request.SessionID = Login.session.ID;
request.UserID = Login.loginResponse.UserInfo.get(0).ID;
request.MethodName = "GetCollectionPointBuUserID";
BusinessService businessService = APIClient.getClient().create(BusinessService.class);
Call<GetCollectionPointByUserIDResponse> call = businessService.GetCollectionPointBuUserID(request);
call.enqueue(new Callback<GetCollectionPointByUserIDResponse>() {
@Override
public void onResponse(Call<GetCollectionPointByUserIDResponse> call, Response<GetCollectionPointByUserIDResponse> response) {
try {
if (response.isSuccessful()) {
getCollectionPointByUserIDResponse = response.body();
assert getCollectionPointByUserIDResponse != null;
if (getCollectionPointByUserIDResponse.ResponseCode == 1) {
collectionPointList = new ArrayList<>(getCollectionPointByUserIDResponse.getCollectionpoint());
CollectionPointAdapter collectionPointAdapter = new CollectionPointAdapter(collectionPointList);
recyclerView.setAdapter(collectionPointAdapter);
} else if (getCollectionPointByUserIDResponse.ResponseCode == 6) {
Intent intent = new Intent(CollectionPoints.this, Login.class);
startActivity(intent);
} else {
Toast.makeText(CollectionPoints.this, getCollectionPointByUserIDResponse.ResponseMessage, Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
Toast.makeText(CollectionPoints.this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<GetCollectionPointByUserIDResponse> call, Throwable t) {
Toast.makeText(CollectionPoints.this, t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}