Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting the contents of ArrayAdapter or ArrayList

I am working on android project and am making using of a ListView that retrieves data from the SQLite database.

I am making a dataset using an ArrayList and then adding this ArrayList into an ArrayAdapter.

When the data is being retrieved from the database, I am telling SQLite to do the sorting so everything is in alphabetical order when it is added into the ListView. At certain times, the information will be added dynamically to to the ListView without it requiring to re-fetch everythin from the database again. However, I want to keep everything in alphabetical order.

How would I do this, do I sort the DataSet and then call the notifyDataSet Changes or do I do the sort directly on the ArrayAdapter. I've looked into performing the sort on the ArrayAdapter but this wants an argument that uses a Comparator but not sure what this is and can't find any working examples that may be of any help for what I want to achieve.

Below is the code that populates the array and sets the list adapter

ArrayList<Spanned> passwords = managePasswordList.getPasswordList();
        if (passwords != null && passwords.size() > 0)
        {
            passwordArrayAdapter = new ArrayAdapter<Spanned>(getActivity().getApplicationContext(), 
                    android.R.layout.simple_list_item_activated_1, passwords);
            setListAdapter(passwordArrayAdapter);
            myListView.setTextFilterEnabled(true);
            txtNoRecords.setVisibility(View.GONE);
        }
        else
        {
            txtNoRecords.setVisibility(View.VISIBLE);
        }

I am then adding data to the dataset and refreshing the list view using the following

String company = Encryption.decrypt(passwords.get(i).company);
                    String username = Encryption.decrypt(passwords.get(i).username);
                    details = Html.fromHtml(company + "<br />" + "<small><font color=\"#767676\">" + username + "</b></small>");    

passwords.add(details);
passwordArrayAdapter.notifyDataSetChanged();

Thanks for any help you can provide.

UPDATE 1 I've tried doing what Nick Bradbury suggested but I am having a problem with the comparator. I have the following code but I don't know where to go from here.

SQLiteDatabase myDb = null;
        Cursor cursor = null;
        ArrayList<Spanned> passwords = new ArrayList<Spanned>();
        try
        {
            myDb = context.openOrCreateDatabase("PasswordManager", Context.MODE_PRIVATE, null);
            cursor = myDb.rawQuery("SELECT * FROM password ASC", null);
            while (cursor.moveToNext())
            {
                final String company = Encryption.decrypt(cursor.getString(2));
                final String username = Encryption.decrypt(cursor.getString(4));
                Spanned details = Html.fromHtml(company + "<br />" + "<small><font color=\"#767676\">" + username + "</b></small>");
                passwords.add(details);

                Collections.sort(passwords, new Comparator<Spanned>() {

                    public int compare(Spanned lhs, Spanned rhs) {
                        return 0;
                    }
                });
            }
        }
        catch (SQLiteException ex)
        {
            common.showBasicAlertDialog("Unfortunately something has gone wrong.\n\nWe will fix this as soon as we can", false);
            Log.e("Database Error", ex.toString());
            return null;
        }

In the return statement I have no idea what to do, I've tried return lhs.compareTo but the lhs and rhs variables don't have the compareTo function so I have not got a clue what to do.

like image 220
Boardy Avatar asked Jan 15 '23 13:01

Boardy


1 Answers

Here's a simple example of sorting an ArrayList using Comparator. In this example, the ArrayList is defined as:

public class StatusList extends ArrayList<Status>

A sort routine for this ArrayList could look like this:

public void sort() {
    Collections.sort(this, new Comparator<Status>() {                
        @Override
        public int compare(Status item1, Status item2) {
            return item2.getDate().compareTo(item1.getDate());
        }
    });
}

Replace <Status> with whatever object your ArrayList contains, then change the comparison to compare the values of the object you wish to sort by.

like image 123
Nick Bradbury Avatar answered Jan 29 '23 07:01

Nick Bradbury