Sorting my ArrayAdapter does not change my data source

I have a strange problem. I have an ArrayAdapter that I am sorting. This displays correctly on my screen, however, when I check the actual data source, the content is not sorted. How can I guarantee that sorting my ListAdapter will also sort my data source?

Collections.sort(quotes, new PercentChangeComparator()); //sort my data source (this isn't necessary)
quotesAdapter.sort(new PercentChangeComparator()); //sort my ListAdapter
Toast toast = Toast.makeText(getApplicationContext(),quotes.get(0).getSymbol(), Toast.LENGTH_SHORT);
toast.show(); //this shows that my data source hasn't been updated, even though my ListAdapter presents my ListView in the correctly sorted order.

For example: If my data source [10,9,1,20]

after sorting my listview will be displayed [1,9,10,20]

but the data source will be [10,9,1,20]

How can i solve this?

+3
source share
1 answer

It's the other way around: sorting your data source will order an ArrayAdapter.

, - .

ArrayList<PercentChangeComparator> quotes = getQuotesFromSomewhere();
QuotesAdapter quotesAdapter = new QuotesAdapter(this, R.layout.xxx, quotes);

, , , DataSet,

Collections.sort(quotes, new PercentChangeComparator());
quotesAdapter.notifyDataSetChanged();

, , .

: ( ), . , ListView, :

quotes.clear();
quotes.add(...);
quotes.add(...);

, , .

Collections.sort(quotes, new PercentChangeComparator());

, , .

+4

Source: https://habr.com/ru/post/1764625/


All Articles