“>我制作了一个表示cardview中数据的recyclerview。我在我的适配器类上实现了Filterable方法,并在我的getFilter方法中正确实现了这些方法。任何人?”
“>当过滤股票列表时,我的搜索过滤器运行良好,但是当我单击特定的股票类别时,它将获得列表中第一类的子类别。”
ViewStock.java
public class ViewStock extends AppCompatActivity implements
StockAdapter.AdapterListener,StockIObserver{
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setMaxWidth(Integer.MAX_VALUE);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
((StockAdapter)stockAdapter).getFilter().filter(s);
return false;
}
@Override
public boolean onQueryTextChange(String newtext) {
((StockAdapter)stockAdapter).getFilter().filter(newtext);
return false;
}
});
return true;
}
}
```StockAdapter.java
package adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import java.net.CookieHandler;
import java.util.ArrayList;
import java.util.List;
import beans.Category;
import beans.Stock;
import ws.wolfsoft.creative.MainActivity;
import ws.wolfsoft.creative.R;
import iobserver.StockIObserver;
//import com.andexert.library.RippleView;
//import com.github.marlonlom.utilities.timeago.TimeAgo;
public class StockAdapter extends RecyclerView.Adapter<StockAdapter.StockViewHolder> implements Filterable{
public StockIObserver mObserver;
private String name_tapped;
AdapterListener listener;
Context context;
private List <Stock> stockFiltered;
public static class StockViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView name;
TextView no;
TextView quantity;
TextView category;
StockViewHolder(View itemView) {
super(itemView);
cv = itemView.findViewById(R.id.card);
name = itemView.findViewById(R.id.name);
quantity = itemView.findViewById(R.id.quantity);
category = itemView.findViewById(R.id.category);
no = itemView.findViewById(R.id.no);
}
}
public void setListener(StockIObserver obs) { mObserver = obs; }
List<Stock> stock;
public StockAdapter(Context context, List<Stock> stock, AdapterListener listener){
this.stock = stock;
this.listener = listener;
this.context = context;
this.stockFiltered = new ArrayList<>();
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public StockViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.stock_cardview, viewGroup, false);
StockViewHolder pvh = new StockViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(@NonNull final StockViewHolder stockViewHolder, int i) {
this.stockFiltered.get((int) getItemId(i));
stockViewHolder.name.setText(stockFiltered.get(i).name);
stockViewHolder.quantity.setText(String.format("%s %s", stockFiltered.get(i).quantity, stockFiltered.get(i).metric));
stockViewHolder.category.setText(stockFiltered.get(i).category);
int val = i-1;
String s = String.valueOf(val);
stockViewHolder.no.setText(s);
name_tapped = stockFiltered.get(i-1).name;
final int posi = i-1;
stockViewHolder.cv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int posi= stockViewHolder.getAdapterPosition()-1;
mObserver.onCardClicked(posi, name_tapped, stockFiltered);
}
});
}
@Override
public int getItemCount() {
return stockFiltered.size();
}
@Override
public long getItemId(int i) {
return i-1;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()){
stockFiltered = stock;
} else {
List<Stock> filteredList = new ArrayList<>();
for (Stock stock : stock){
if (stock.name.toLowerCase().contains(charString.toLowerCase())){
filteredList.add(stock);
}
}
stockFiltered = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = stockFiltered;
return (FilterResults) filterResults.values;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
stockFiltered = (ArrayList<Stock>)filterResults.values;
// refresh the list with filtered data
notifyDataSetChanged();
}
};
}
public interface AdapterListener {
void onCategorySelected(Category category);
}
}