Im having a problem with my listview not sorting despite adding sorting code to the backing arraylist, implementing a comparable interface on the object and setting a comparator on the sort function of the the array adapter. When I track the code via debuger one can see the values are actually sorted correctly (alphabetical order), however, the the render view of the values is not sorted at all.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
cabinet = (ListView) rootView.findViewById(R.id.lstCabinet);
final Cabinet cubard = Cabinet.get(getActivity());
List<Shaker> shakers = cubard.getShakers();
ShakerAdapter adapt = new ShakerAdapter((ArrayList<Shaker>) shakers);
adapt.sort(new Comparator<Shaker>() {
@Override
public int compare(Shaker a, Shaker b) {
String a1 = a.getName();
String a2 = b.getName();
return a1.compareTo(a2);
}
});
cabinet.setAdapter(adapt);
Collections.sort(shakers);
adapt.notifyDataSetChanged();
registerForContextMenu(cabinet); //register the context menus for the list elements.
mainActivity.setCabinet(cabinet);
mainActivity.setCubard(cubard);
mainActivity.newShaker = (Button) rootView.findViewById(R.id.btnNewShaker);
cabinet.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent in = new Intent(getActivity(), ShakerViewActivity.class);
in.putExtra(SHAKER, cubard.getShaker(position));
in.putExtra(PASSPHRASE, mainActivity.getPassPhrase());
in.putExtra(SESTRAKER, session);
if (mainActivity.validateSession()) {
if (mainActivity.passPhrase != null && mainActivity.passPhrase.length() > 0) {
startActivity(in);
}
}
}
});
return rootView;
}
Below is the ShakerAdapter code
//private class to overide the array adapter for the list view of the shakers
private class ShakerAdapter extends ArrayAdapter {
ArrayList<Shaker> shakers;
private ShakerAdapter(ArrayList<Shaker> shakers) {
super(getActivity(), 0, shakers);
// Collections.sort(shakers,new Comparator<Shaker>() {
// @Override
// public int compare(Shaker a, Shaker b) {
// String a1 = a.getName();
// String b1 = b.getName();
//
// return a1.compareTo(b1);
//// return a2.compareTo(a1);
// }
// });
Collections.sort(shakers);
this.shakers = shakers;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.shaker_element, null);
}
Shaker shaker = shakers.get(position);
TextView title = (TextView) convertView.findViewById(R.id.tvElementTitle);
title.setText(shaker.getName());
return convertView;
}
}
}
I know I have a lot of redundant sorting code and it will be removed once I get this working, but in mean time I will greatly appreciate any help you guys can provide.
Here is a screenshot of what the rendered ListView looks like after all the Shakers have been sorted:
-->Well looks like I don't have enough reputation points to post images so, if you want to look at the screen shot here is the link to my g+ photo: https://lh5.googleusercontent.com/-5YTRcA8RfSI/VQCCXyiN2WI/AAAAAAAAL7Q/XCMSFa4GkcE/w465-h411-no/shakers.png