Sort the contents of an ArrayAdapter or ArrayList

I am working on an android project and am using ListView which retrieves data from SQLite database.

I make a dataset using an ArrayList, and then add this ArrayList to the ArrayAdapter.

When data is retrieved from the database, I tell SQLite to sort so that everything is in alphabetical order when it is added to the ListView. At certain points, information will be dynamically added to the ListView without the need to reselect from the database. However, I want to keep everything in alphabetical order.

How do I do this, I sort the DataSet and then call notifyDataSet Changes or do the sorting directly in the ArrayAdapter. I learned how to do sorting in the ArrayAdapter, but it requires an argument that Comparator uses, but not sure what it is, and cannot find any working examples that can be useful for what I want to achieve.

Below is the code that populates the array and installs 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); } 

Then I add data to the dataset and update 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 tried to do what Nick Bradbury suggested, but I have 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 tried return lhs.compareTo , but the lhs and rhs variables do not have the compareTo function, so I don’t know what to do.

+4
source share
1 answer

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

 public class StatusList extends ArrayList<Status> 

The sort procedure for this ArrayList might 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 any object that contains your ArrayList, then change the comparison to compare the values ​​of the object you want to sort.

+9
source

Source: https://habr.com/ru/post/1434893/


All Articles