gpt4 book ai didi

java - 更改 RecyclerView 项目时刷新 SearchView

转载 作者:太空宇宙 更新时间:2023-11-04 09:10:01 24 4
gpt4 key购买 nike

我正在努力解决一个问题 - 尝试在删除 RecyclerView 项目后刷新搜索结果。

现在,当我过滤掉项目并删除其中一个项目时,RecyclerView 会再次显示所有其他项目,而无需应用搜索过滤器。但是如何实现搜索过滤器,使其即使在 RecyclerView 列表发生更改时也保持打开状态?

这是我在主要 Activity 中加载数据的方式:

private void loadData(final int ordernumber) {

orderLineViewModel.getAllUnCheckedSingleOrderLines(ordernumber).observe(this, new Observer<List<OrderLine>>() {
@Override
public void onChanged(List<OrderLine> orderLines) {

orderLineListAdapter.setOrderLines(orderLines);
}
});
}

这是我在主要 Activity 中的 SearchView 代码:

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
orderLineListAdapter.getFilter().filter(s);
return false;
}

@Override
public boolean onQueryTextChange(String s) {
orderLineListAdapter.getFilter().filter(s);
return false;
}
});

这是我在 RecyclerViewAdapter 中的过滤器:

public void setOrderLines(List<OrderLine> orderLines) {

orderLineList = orderLines;
orderLineListFull = new ArrayList<>(orderLines);

notifyDataSetChanged();
}

@Override
public Filter getFilter() {
return orderFilter;
}

private Filter orderFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
List<OrderLine> filteredList = new ArrayList<>();

if (charSequence == null || charSequence.length() == 0){
filteredList.addAll(orderLineListFull);
}else {
String filterPattern = charSequence.toString().toLowerCase().trim();

for (OrderLine orderLine: orderLineListFull){
if (orderLine.getProductCode().toLowerCase().contains(filterPattern)){
filteredList.add(orderLine);
}
}
}

FilterResults results = new FilterResults();
results.values = filteredList;

return results;
}

@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
orderLineList.clear();
orderLineList.addAll((List)filterResults.values);
notifyDataSetChanged();
}
};

按下 RecyclerView 项目中的复选框:

@Override
public void onClick(View view) {

int position = getAdapterPosition();
OrderLine line = orderLineList.get(position);

switch (view.getId()) {

case R.id.foam_in_checkbox:

if (okCheckBox.isChecked()) {

line.setIsArrived(1);
orderLineViewModel.update(line);
orderLineList.remove(position);
notifyItemRemoved(position);

} else {
line.setIsArrived(0);
orderLineViewModel.update(line);
}
break;
}
}

编辑:添加了整个 RecyclerViewAdapter:

public class OrderLineListAdapter extends 

RecyclerView.Adapter<OrderLineListAdapter.OrderLineViewHolder> implements Filterable {

private Context context;
private final LayoutInflater orderLineInflater;
private List<OrderLine> orderLineList;
private List<OrderLine> orderLineListFull;
private OrderLineViewModel orderLineViewModel;

public OrderLineListAdapter(Context context) {
this.context = context;
orderLineInflater = LayoutInflater.from(context);
orderLineViewModel = ViewModelProviders.of((FoamInActivity) context).get(OrderLineViewModel.class);
}

@NonNull
@Override
public OrderLineViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View view = orderLineInflater.inflate(R.layout.foam_in_single_line, parent, false);

return new OrderLineViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull OrderLineViewHolder orderLineViewHolder, int position) {

if (orderLineList != null) {

OrderLine current = orderLineList.get(position);

orderLineViewHolder.productCodeTextView.setText(current.getProductCode());
orderLineViewHolder.orderedQuantityTextView.setText(String.valueOf(current.getOrderedQuantity()));

orderLineViewHolder.okCheckBox.setOnCheckedChangeListener(null);
orderLineViewHolder.okCheckBox.setChecked(false);

} else {
orderLineViewHolder.productCodeTextView.setText(R.string.no_open_positions);
}
}

public void setOrderLines(List<OrderLine> orderLines) {

orderLineList = orderLines;
orderLineListFull = new ArrayList<>(orderLines);

notifyDataSetChanged();
}

@Override
public int getItemCount() {

if (orderLineList != null) {
return orderLineList.size();
} else {
return 0;
}
}

public class OrderLineViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

public TextView productCodeTextView, orderedQuantityTextView;
public CheckBox okCheckBox;

public OrderLineViewHolder(@NonNull View itemView) {
super(itemView);

productCodeTextView = itemView.findViewById(R.id.foam_in_product_code);
orderedQuantityTextView = itemView.findViewById(R.id.foam_in_quantity);
okCheckBox = itemView.findViewById(R.id.foam_in_checkbox);

okCheckBox.setOnClickListener(this);
productCodeTextView.setOnClickListener(this);
}

@Override
public void onClick(View view) {

int position = getAdapterPosition();

OrderLine line = orderLineList.get(position);

switch (view.getId()) {

case R.id.foam_in_checkbox:

if (okCheckBox.isChecked()) {

line.setIsArrived(1);
orderLineViewModel.update(line);
orderLineList.remove(position);
notifyItemRemoved(position);

} else {
line.setIsArrived(0);
orderLineViewModel.update(line);
}

break;

case R.id.foam_in_product_code:
Toast.makeText(context, "Status: " + line.getIsArrived(), Toast.LENGTH_SHORT).show();
break;
}
}
}

@Override
public Filter getFilter() {
return orderFilter;
}

private Filter orderFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
List<OrderLine> filteredList = new ArrayList<>();

if (charSequence == null || charSequence.length() == 0){
filteredList.addAll(orderLineListFull);
}else {
String filterPattern = charSequence.toString().toLowerCase().trim();

for (OrderLine orderLine: orderLineListFull){
if (orderLine.getProductCode().toLowerCase().contains(filterPattern)){
filteredList.add(orderLine);
}
}
}

FilterResults results = new FilterResults();
results.values = filteredList;

return results;
}

@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
orderLineList.clear();
orderLineList.addAll((List)filterResults.values);
notifyDataSetChanged();
}
};

最佳答案

整个RecyclerViewAdapter:

public class OrderLineListAdapter extends RecyclerView.Adapter<OrderLineListAdapter.OrderLineViewHolder> implements Filterable {

private Context context;
private final LayoutInflater orderLineInflater;
private List<OrderLine> orderLineList;
private List<OrderLine> orderLineListFull;
private OrderLineViewModel orderLineViewModel;

public OrderLineListAdapter(Context context) {
this.context = context;
orderLineInflater = LayoutInflater.from(context);
orderLineViewModel = ViewModelProviders.of((FoamInActivity) context).get(OrderLineViewModel.class);
}

@NonNull
@Override
public OrderLineViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View view = orderLineInflater.inflate(R.layout.foam_in_single_line, parent, false);

return new OrderLineViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull OrderLineViewHolder orderLineViewHolder, int position) {

if (orderLineList != null) {

OrderLine current = orderLineList.get(position);

orderLineViewHolder.productCodeTextView.setText(current.getProductCode());
orderLineViewHolder.orderedQuantityTextView.setText(String.valueOf(current.getOrderedQuantity()));

orderLineViewHolder.okCheckBox.setOnCheckedChangeListener(null);
orderLineViewHolder.okCheckBox.setChecked(false);

} else {
orderLineViewHolder.productCodeTextView.setText(R.string.no_open_positions);
}
}

public void setOrderLines(List<OrderLine> orderLines) {

orderLineList = orderLines;
orderLineListFull = new ArrayList<>(orderLines);

notifyDataSetChanged();
}

@Override
public int getItemCount() {

if (orderLineList != null) {
return orderLineList.size();
} else {
return 0;
}
}

public class OrderLineViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

public TextView productCodeTextView, orderedQuantityTextView;
public CheckBox okCheckBox;

public OrderLineViewHolder(@NonNull View itemView) {
super(itemView);

productCodeTextView = itemView.findViewById(R.id.foam_in_product_code);
orderedQuantityTextView = itemView.findViewById(R.id.foam_in_quantity);
okCheckBox = itemView.findViewById(R.id.foam_in_checkbox);

okCheckBox.setOnClickListener(this);
productCodeTextView.setOnClickListener(this);
}

@Override
public void onClick(View view) {

int position = getAdapterPosition();

OrderLine line = orderLineList.get(position);

switch (view.getId()) {

case R.id.foam_in_checkbox:

if (okCheckBox.isChecked()) {

line.setIsArrived(1);
orderLineViewModel.update(line);
orderLineList.remove(position);
notifyItemRemoved(position);

} else {
line.setIsArrived(0);
orderLineViewModel.update(line);
}

break;

case R.id.foam_in_product_code:
Toast.makeText(context, "Status: " + line.getIsArrived(), Toast.LENGTH_SHORT).show();
break;
}
}
}

@Override
public Filter getFilter() {
return orderFilter;
}

private Filter orderFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
List<OrderLine> filteredList = new ArrayList<>();

if (charSequence == null || charSequence.length() == 0){
filteredList.addAll(orderLineListFull);
}else {
String filterPattern = charSequence.toString().toLowerCase().trim();

for (OrderLine orderLine: orderLineListFull){
if (orderLine.getProductCode().toLowerCase().contains(filterPattern)){
filteredList.add(orderLine);
}
}
}

FilterResults results = new FilterResults();
results.values = filteredList;

return results;
}

@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
orderLineList.clear();
orderLineList.addAll((List)filterResults.values);
notifyDataSetChanged();
}
};

}

关于java - 更改 RecyclerView 项目时刷新 SearchView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59760484/

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