Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8561937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:37:03+00:00 2026-06-11T16:37:03+00:00

I am working on android project and am making using of a ListView that

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:37:04+00:00Added an answer on June 11, 2026 at 4:37 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an android project where I am using a ListView and
I am working on a android project that can read e-mails from any gmail
I am working on an android project that requires a database. The issue that
I am working on my android project with SQLite database. I need to get
I'm currently working on an Android project, where I have to collect data from
I am currently working on android project where I am using a custom list
I am working on an android project and I am using a spinner which
I am working on an Android project using C++ for the main development and
I've got an Android project I'm working on that, ultimately, will require me to
I am working on a android project. I n that I have to deal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.