我发现了很多解决方案,但是他们只能在给定列表中进行搜索,或者可以通过searchview搜索。但是就我而言,我必须使用edite文本进行搜索。从recyclerview中的editText搜索,其中我使用retrofit从API获得项目。这是我的recyclerview适配器和类的代码。 我想根据name过滤collectionpoint列表。 提前谢谢
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();
}
});
}
答案 0 :(得分:1)
您可以通过调用Recyclerview
中的filter方法并将editText
传递给Adapter类来过滤addTextChangedListener
中的arraylist
个项目,如下代码:
Private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(yourLayout);
context = YourActivity.this;
editText_filter.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
try {
if (CollectionPointAdapter != null)
//Calling Adapter method
CollectionPointAdapter.getFilter().filter(editable);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void setNoDataVisible(int size) { //IF results empty handle UI from adapter.
try {
if (size == 0) {
txtView_noData.setVisibility(View.VISIBLE);
} else {
txtView_noData.setVisibility(View.GONE);
}
} catch (Exception e) {
e.printStackTrace();
}
}
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());
//Here You're passing List to Adatper, so that we can filter it.
CollectionPointAdapter collectionPointAdapter = new CollectionPointAdapter(context, 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();
}
});
}
按照以下代码更改Adapter
:
public class CollectionPointAdapter extends RecyclerView.Adapter<CollectionPointAdapter.MyViewHolder> implements Filterable {
private Context mContext;
private ArrayList<CollectionPoint> collectionPointResults;
private ArrayList<CollectionPoint> mFilteredList;
public CollectionPointAdapter(Context mContext, ArrayList<CollectionPoint> collectionPointResults) {
this.mContext = mContext;
this.collectionPointResults = collectionPointResults;
this.mFilteredList = collectionPointResults;
}
@Override
public int getItemCount() {
return mFilteredList.size();
}
@Override
public long getItemId(int position) {
return position;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.item_list_yours, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
try {
holder.collectionPointId.setText("ID - " + String.valueOf(mFilteredList.get(position).getID()));
holder.collectionPointName.setText(mFilteredList.get(position).getName());
} catch (Exception e) {
e.printStackTrace();
}
}
//Filter the adapter interface
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
try {
String charString = charSequence.toString();
if (charString.isEmpty()) {
mFilteredList = collectionPointResults;
} else {
ArrayList<RefLeadsgivenResult> filteredList = new ArrayList<>();
for (RefLeadsgivenResult row : collectionPointResults) {
if (row.getName().toLowerCase().contains(charString)) { //Searching by Name
filteredList.add(row);
}
}
mFilteredList = filteredList;
}
} catch (IndexOutOfBoundsException ie) {
ie.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
FilterResults filterResults = new FilterResults();
filterResults.values = mFilteredList;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
try {
mFilteredList = (ArrayList<RefLeadsgivenResult>) filterResults.values;
notifyDataSetChanged();
((YourActivity) mContext).setNoDataVisible(mFilteredList.size());
} catch (Exception e) {
e.printStackTrace();
}
}
};
}
class MyViewHolder extends RecyclerView.ViewHolder {
public final View mView;
@BindView(R.id.collectionPointId)
TextView collectionPointId;
@BindView(R.id.collectionPointName)
TextView collectionPointName;
public MyViewHolder(View itemView) {
super(itemView);
mView = itemView;
ButterKnife.bind(this, itemView);
}
}
}