1

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

1
  • Hi ... could you please mark my answer correct and upvote it? Thanks ... :) Commented May 29, 2016 at 13:31

2 Answers 2

1

While your code looks correct to me, you can try replacing

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);
            }
        });

with

Collections.sort(shakers, new Comparator<Shaker>() {
            @Override
            public int compare(Shaker a, Shaker b) {
                String a1 = a.getName();
                String a2 = b.getName();

                return a1.compareTo(a2);
            }
        });

and make sure you create the Adapter AFTER you sort the ArrayList, i.e. put this line of code

ShakerAdapter adapt = new ShakerAdapter((ArrayList<Shaker>) shakers);

after calling Collections.sort().

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your input ZygotInit, however, it did not work. I'm going to try to put a screenshot here so you can see the output. I just don't get it, I've sorted tons of arrays before, granted in swing, but this should technically be the same.... #dumbfounded
Screemshot is up, take a look at it and see if you see something that is painfully obvious that I am not seeing.
Resolved! Problem was originating from another class (Cabinete.class) that implemented its own dataSetChanged() and was not using any sorting logic. Once the sort logic was implemented in said function all is well in the world! Thanks to ZygotInit for chiming in! Cheers bud!
0

Resolved! Problem was originating from another class (Cabinete.class) that implemented its own dataSetChanged() and was not using any sorting logic. Once the sort logic was implemented in said function all is well in the world! Thanks to ZygotInit for chiming in! Cheers bud! –

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.