Open ghost opened 6 years ago
When i implement Filterable and make a search, the results wont get updated after notifyDataSetChanged();, any ideas on why is this happening?
notifyDataSetChanged();
public class AdapterTransactionList extends SectioningAdapter implements Filterable { static final String TAG = AdapterTransactionList.class.getSimpleName(); static final boolean USE_DEBUG_APPEARANCE = false; private List<Transactions> original_items = new ArrayList<>(); private List<Transactions> filtered_items = new ArrayList<>(); private ItemFilter mFilter = new ItemFilter(); private Context ctx; private Activity activity; private SharedPreferences sharedPref; private String oldDate; private int fuelType; private class Section { int index; String header; String footer; List<Transactions> items = new ArrayList<>(); } public AdapterTransactionList(List<Transactions> items, Context context) { original_items = items; filtered_items = items; ctx = context; this.activity = (Activity) ctx; this.sharedPref = PreferenceManager.getDefaultSharedPreferences(ctx); this.fuelType = sharedPref.getInt(PREF_SETTING_MEASURE_UNIT, 0); String date; String dateOld = ""; int numberSections = 0; Section section = new Section(); for (int i = 0; i < filtered_items.size(); i++) { Transactions trans = filtered_items.get(i); date = formattedDateFromString("", "dd MMM yyyy", trans.getDate()); if (!date.equals(dateOld)) { numberSections++; appendSection(section); section = new Section(); section.index = i; section.header = date; section.items.add(trans); } else { section.items.add(trans); if (i == filtered_items.size() - 1) { appendSection(section); } } dateOld = date; } } public class ItemViewHolder extends SectioningAdapter.ItemViewHolder { // each data item is just a string in this case public ImageView ivFoto; public TextView tvName; public TextView tvAddress; public TextView tvCardVirtual; public TextView tvAmount; public TextView tvVolume; public ItemViewHolder(View itemView) { super(itemView); ivFoto = itemView.findViewById(R.id.ivFoto); tvName = itemView.findViewById(R.id.tvName); tvAddress = itemView.findViewById(R.id.tvAddress); tvCardVirtual = itemView.findViewById(R.id.tvCardVirtual); tvAmount = itemView.findViewById(R.id.tvAmount); tvVolume = itemView.findViewById(R.id.tvVolume); } } public class HeaderViewHolder extends SectioningAdapter.HeaderViewHolder { TextView textView; public HeaderViewHolder(View itemView) { super(itemView); textView = itemView.findViewById(R.id.textView); } } ArrayList<Section> sections = new ArrayList<>(); void appendSection(Section section) { sections.add(section); } @Override public int getNumberOfSections() { return sections.size(); } @Override public int getNumberOfItemsInSection(int sectionIndex) { return sections.get(sectionIndex).items.size(); } @Override public boolean doesSectionHaveHeader(int sectionIndex) { return !TextUtils.isEmpty(sections.get(sectionIndex).header); } @Override public boolean doesSectionHaveFooter(int sectionIndex) { return !TextUtils.isEmpty(sections.get(sectionIndex).footer); } @Override public ItemViewHolder onCreateItemViewHolder(ViewGroup parent, int itemType) { LayoutInflater inflater = LayoutInflater.from(parent.getContext()); View v = inflater.inflate(R.layout.item_list_transactions, parent, false); return new ItemViewHolder(v); } @Override public HeaderViewHolder onCreateHeaderViewHolder(ViewGroup parent, int headerType) { LayoutInflater inflater = LayoutInflater.from(parent.getContext()); View v = inflater.inflate(R.layout.item_list_transactions_header, parent, false); return new HeaderViewHolder(v); } @SuppressLint("SetTextI18n") @Override public void onBindItemViewHolder(SectioningAdapter.ItemViewHolder viewHolder, int sectionIndex, final int itemIndex, int itemType) { final Section s = sections.get(sectionIndex); ItemViewHolder ivh = (ItemViewHolder) viewHolder; final int position = itemIndex; //ivh.textView.setText(s.items.get(itemIndex)); final Transactions transaction = s.items.get(itemIndex);//filtered_items.get(position); int necMeasure = transaction.getNecMeasure(); int idTransactionType = transaction.getIdtransactionType(); String date = formattedDateFromString("", "dd MMM", transaction.getDate()); ivh.tvName.setText(transaction.getName()); ivh.tvAddress.setText(transaction.getAddress()); ivh.tvAmount.setText(String.format("%s%s", transaction.getCurrencySymbol(), transaction.getAmountF())); //TODO Monipak uses "Username" ivh.tvCardVirtual.setText(transaction.getCardVirtual()); if (fuelType == 0) { ivh.tvVolume.setText(String.format("%s Lts", transaction.getFuelLtsF())); } else { ivh.tvVolume.setText(String.format("%s Gls", transaction.getFuelGlsF())); } if (necMeasure == 30) { // Dinero ivh.tvVolume.setVisibility(View.GONE); } if (!transaction.getIcon().isEmpty()) { Picasso.with(ctx).load(transaction.getIcon()).into(ivh.ivFoto); } else { Picasso.with(ctx).load(R.drawable.ic_combustible).into(ivh.ivFoto); } // Switch color of Amount depending on idTransactionType 1 = CONSUMO, 2 = REVERSION, 3 = RECARGA, 6 = RECORTE switch (idTransactionType) { case 1: ivh.tvAmount.setTextColor(ctx.getResources().getColor(R.color.trans_red)); ivh.tvVolume.setTextColor(ctx.getResources().getColor(R.color.trans_red)); break; case 2: ivh.tvAmount.setTextColor(ctx.getResources().getColor(R.color.trans_green)); ivh.tvVolume.setTextColor(ctx.getResources().getColor(R.color.trans_green)); break; case 3: ivh.tvAmount.setTextColor(ctx.getResources().getColor(R.color.trans_green)); ivh.tvVolume.setTextColor(ctx.getResources().getColor(R.color.trans_green)); break; case 6: ivh.tvAmount.setTextColor(ctx.getResources().getColor(R.color.trans_red)); ivh.tvVolume.setTextColor(ctx.getResources().getColor(R.color.trans_red)); break; } oldDate = date; ivh.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ActivityTransactionsReceipt.navigate((AppCompatActivity) activity, v, s.items.get(itemIndex)); } }); } @SuppressLint("SetTextI18n") @Override public void onBindHeaderViewHolder(SectioningAdapter.HeaderViewHolder viewHolder, int sectionIndex, int headerType) { Section s = sections.get(sectionIndex); HeaderViewHolder hvh = (HeaderViewHolder) viewHolder; hvh.textView.setText(s.header); } @Override public void onBindGhostHeaderViewHolder(SectioningAdapter.GhostHeaderViewHolder viewHolder, int sectionIndex) { if (USE_DEBUG_APPEARANCE) { viewHolder.itemView.setBackgroundColor(0xFF9999FF); } } public Filter getFilter() { return mFilter; } private class ItemFilter extends Filter { @Override protected FilterResults performFiltering(CharSequence constraint) { String query = constraint.toString().toLowerCase(); FilterResults results = new FilterResults(); final List<Transactions> list = original_items; final List<Transactions> result_list = new ArrayList<>(list.size()); for (int i = 0; i < list.size(); i++) { String str_title = list.get(i).getName(); String str_cardVirtual = list.get(i).getCardVirtual(); String str_amountF = list.get(i).getAmountF(); if (str_title.toLowerCase().contains(query) || str_cardVirtual.toLowerCase().contains(query) || str_amountF.toLowerCase().contains(query)) { result_list.add(list.get(i)); } } results.values = result_list; results.count = result_list.size(); return results; } @SuppressWarnings("unchecked") @Override protected void publishResults(CharSequence constraint, FilterResults results) { filtered_items = (List<Transactions>) results.values; notifyDataSetChanged(); } } }
When i implement Filterable and make a search, the results wont get updated after
notifyDataSetChanged();
, any ideas on why is this happening?