diff --git a/codeview/src/main/java/com/amrdeveloper/codeview/CodeViewAdapter.java b/codeview/src/main/java/com/amrdeveloper/codeview/CodeViewAdapter.java index 46b636c..7bb13cd 100644 --- a/codeview/src/main/java/com/amrdeveloper/codeview/CodeViewAdapter.java +++ b/codeview/src/main/java/com/amrdeveloper/codeview/CodeViewAdapter.java @@ -41,21 +41,22 @@ /** * Custom base adapter that to use it in CodeView auto complete and snippets feature - * + *

* CodeViewAdapter supports to take a list of code which can include Keywords and snippets * * @since 1.1.0 */ public class CodeViewAdapter extends BaseAdapter implements Filterable { - private List codeList; private List originalCodes; + private List currentSuggestions; private final LayoutInflater layoutInflater; private final int codeViewLayoutId; private final int codeViewTextViewId; public CodeViewAdapter(@NonNull Context context, int resource, int textViewResourceId, @NonNull List codes) { - this.codeList = codes; + this.originalCodes = codes; + this.currentSuggestions = new ArrayList<>(); this.layoutInflater = LayoutInflater.from(context); this.codeViewLayoutId = resource; this.codeViewTextViewId = textViewResourceId; @@ -68,23 +69,21 @@ public View getView(int position, View convertView, ViewGroup parent) { } TextView textViewName = convertView.findViewById(codeViewTextViewId); - - Code currentCode = codeList.get(position); + Code currentCode = currentSuggestions.get(position); if (currentCode != null) { textViewName.setText(currentCode.getCodeTitle()); } - return convertView; } @Override public int getCount() { - return codeList.size(); + return currentSuggestions.size(); } @Override public Object getItem(int position) { - return codeList.get(position); + return currentSuggestions.get(position); } @Override @@ -94,11 +93,13 @@ public long getItemId(int position) { /** * Update the current code list with new list + * * @param newCodeList The new code list */ public void updateCodes(List newCodeList) { - codeList.clear(); - codeList.addAll(newCodeList); + currentSuggestions.clear(); + originalCodes.clear(); + originalCodes.addAll(newCodeList); notifyDataSetChanged(); } @@ -106,7 +107,8 @@ public void updateCodes(List newCodeList) { * Clear the current code list and notify data set changed */ public void clearCodes() { - codeList.clear(); + originalCodes.clear(); + currentSuggestions.clear(); notifyDataSetChanged(); } @@ -121,32 +123,29 @@ protected FilterResults performFiltering(CharSequence constraint) { FilterResults results = new FilterResults(); List suggestions = new ArrayList<>(); - if (originalCodes == null) { - originalCodes = new ArrayList<>(codeList); - } - - + // If no prefix text, show all codes if (constraint == null || constraint.length() == 0) { results.values = originalCodes; results.count = originalCodes.size(); - } else { - String filterPattern = constraint.toString().toLowerCase().trim(); + return results; + } - for (Code item : originalCodes) { - if (item.getCodePrefix().toLowerCase().contains(filterPattern)) { - suggestions.add(item); - } + // Calculate suggestions based on current text + String filterPattern = constraint.toString().toLowerCase().trim(); + for (Code item : originalCodes) { + if (item.getCodePrefix().toLowerCase().contains(filterPattern)) { + suggestions.add(item); } - results.values = suggestions; - results.count = suggestions.size(); } + results.values = suggestions; + results.count = suggestions.size(); return results; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { - codeList = (List) results.values; + currentSuggestions = (List) results.values; notifyDataSetChanged(); }