I want to sort a list of items in a string-array and then filter them using a SearchView in the toolbar.
This is the string-array (each item is the name of a png drawable in res folder):
<string-array name="brands">
<item>facebook</item>
<item>twitter</item>
<item>instagram</item>
<item>android</item>
<item>blackberry</item>
<item>samsung</item>
<item>huawei</item>
<item>starbucks</item>
<item>motorola</item>
<item>nexus</item>
<item>lg</item>
<item>beats</item>
<item>sony</item>
<item>lenovo</item>
<item>dell</item>
<item>hp</item>
</string-array>
In a RecyclerView Adapter, I have set the array and an ArrayList:
private String[] brands;
private ArrayList<Integer> drawables;
And I have a function that adds the id of the drawables from the string-array to the ArrayList so I can later load the drawable by simply writing drawables.get(position); :
private void loadLogo() {
drawables = new ArrayList<>();
brands = context.getResources().getStringArray(R.array.brands);
for (String extra : brands) {
int res = context.getResources().getIdentifier(extra, "drawable", context.getPackageName());
if (res != 0) {
final int brandInt = context.getResources().getIdentifier(extra, "drawable", context.getPackageName());
if (brandInt != 0)
drawables.add(brandInt);
}
}
}
What I want to know is:
- How to sort
brandsbefore adding items todrawables. - How to create a filter function to filter
brandsand make the content change in theRecylerViewproperly.
I hope someone can help me. Thanks in advance.
sortmethod, andListViewhas asetTextFiltermethod.