Basically I have a spinner
where you can sort your listview
, the code i am using for sorting:
if(arg2==0)
{
Sorting_class.QueueSort(mList);
}
else if(arg2==1)
{
Sorting_class.AlphaSort(mList);
}
After sorting and logging the list, it looks sorted without any problem! But as soon as I call adapter.notifydatasetchanged
it messes up, for example it overrides the info of the last element with the first element.
If my objects has a string name like this:
z
s
a
after calling alpha sort it looks like this from the logcat
:
a
s
z
but after calling adapter.notifydatasetchanged
to display the new info, it looks like this:
a
s
a
and it keeps on doing this after each sort until all elements get the same info. After sorting for the second time, my listview looks like this:
a
a
a
this is the code i am using for the sorting:
public static void QueueSort(ArrayList<item_base> mList)
{
Collections.sort(mList, new Comparator<item_base>() {
@Override
public int compare(item_base lhs, item_base rhs) {
return lhs.GetTimeMil() < rhs.GetTimeMil() ? -1 : 1;
}
});
}
public static void AlphaSort(ArrayList<item_base> mList)
{
Collections.sort(mList, new Comparator<item_base>() {
@Override
public int compare(item_base lhs, item_base rhs) {
return lhs.getmName().compareTo(rhs.getmName());
}
});
}
this is the top part of the getview function " it's very long "
if(convertView == null)
convertView = getActivity().getLayoutInflater().inflate(R.layout.listview_shopping, parent,false);
final item_base item = mList.get(pos);
Log.d("sorting" , "getview = " + item.getmName() + " pos = " + pos);
the only solution I found is to set the adapter again after each sort:
mAdapter = new Listview_customAdapter(getActivity(), mList, R.layout.listview_shopping);
mListView.setAdapter(mAdapter);