gpt4 book ai didi

java - 从 recyclerview 中的 editText 进行搜索,我使用改造从 API 获取项目

转载 作者:行者123 更新时间:2023-12-02 09:55:33 25 4
gpt4 key购买 nike

我找到了很多解决方案,但是他们只在给定的列表中搜索,或者他们可以通过searchview进行搜索。但是就我而言,我必须使用编辑文本进行搜索。从 recyclerview 中的 editText 进行搜索,我使用改造从 API 获取项目。这是我的 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();
}

Activity :

 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();
}
});
}

最佳答案

您可以通过调用 addTextChangedListener 中的过滤器方法并将 arraylist 传递给您的适配器,从 editText 过滤 Recyclerview 项目类如下代码:

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();
}
});
}

更改您的适配器,如下代码:

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);
}
}
}

关于java - 从 recyclerview 中的 editText 进行搜索,我使用改造从 API 获取项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56034688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com