-1

i want to sort arraylist in android where data is adding over database. how can i do that please help me.

    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.provider.ContactsContract;

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;

    /**
     * Created by Imran on 10/25/2015.
     */
    public class DataSource {

        private DatabaseHelper databaseHelper;

        private SQLiteDatabase database;

        private ContactModel contactModel;


        public DataSource(Context context) {
            databaseHelper = new DatabaseHelper(context);
        }

        public void open(){
            database= databaseHelper.getWritableDatabase();
        }
        public void close(){
            databaseHelper.close();
        }

        public long insertData(ContactModel contactModel){
            this.open();
            ContentValues contentValues=new ContentValues();

            contentValues.put(DatabaseHelper.COL_NAME,contactModel.getName());

            contentValues.put(DatabaseHelper.COL_COMPANY,contactModel.getCompanyName());

            contentValues.put(DatabaseHelper.COL_DESIGNATION,contactModel.getDesignation());

            contentValues.put(DatabaseHelper.COL_PHONE_NO,contactModel.getPhoneNo());

            contentValues.put(DatabaseHelper.COL_EMAIL,contactModel.getEmail());



            long inserted=database.insert(DatabaseHelper.CONTACT_TABLE,null,contentValues);
            database.close();
            this.close();

            return inserted;
        }


        public boolean updateData(String id,ContactModel contactModel){


            this.open();
            ContentValues contentValues=new ContentValues();

            contentValues.put(DatabaseHelper.COL_NAME,contactModel.getName());

            contentValues.put(DatabaseHelper.COL_COMPANY,contactModel.getCompanyName());

            contentValues.put(DatabaseHelper.COL_DESIGNATION,contactModel.getDesignation());

            contentValues.put(DatabaseHelper.COL_PHONE_NO,contactModel.getPhoneNo());

            contentValues.put(DatabaseHelper.COL_EMAIL, contactModel.getEmail());

            long update=database.update(DatabaseHelper.CONTACT_TABLE, contentValues, DatabaseHelper.COL_ID + " = " + id,
                    null);

            if (update>0){
                return true;
            }else {
                return false;
            }

        }

        public ArrayList<ContactModel> getAllContact(){
            this.open();
            ArrayList<ContactModel> contactModelArrayList=new ArrayList<>();
            ArrayList<ContactModel> sortedContactModelArrayList=new ArrayList<>();


            Cursor cursor=database.query(DatabaseHelper.CONTACT_TABLE, null, null, null, null, null, null);

            if(cursor!=null && cursor.getCount()>0){
                cursor.moveToFirst();

                for(int i=0;i<cursor.getCount();i++){
                    String id=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_ID));
                    String name=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_NAME));
                    String company=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_COMPANY));
                    String designation=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DESIGNATION));
                    String phoneNo=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_PHONE_NO));
                    String email=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_EMAIL));


                    contactModel=new ContactModel(id,name,company,designation,phoneNo,email);

                    contactModelArrayList.add(contactModel);
                    cursor.moveToNext();
                    this.close();


                }
            }
         /*   Collections.sort(contactModelArrayList, new Comparator<>() {
                @Override
                public int compare(String s1, String s2) {
                    return s1.compareToIgnoreCase(s2);
                }
            });*/

            sortedContactModelArrayList=Collections.sort(contactModelArrayList);

            return sortedContactModelArrayList;
        }

        public boolean delete(String id){
            this.open();
            database.delete(DatabaseHelper.CONTACT_TABLE, DatabaseHelper.COL_ID + "=" + id, null);
            this.close();
            return true;
        }

        public ContactModel singleContact(String id){
            this.open();
            Cursor cursor=database.query(DatabaseHelper.CONTACT_TABLE,
                    new String[]{DatabaseHelper.COL_ID,DatabaseHelper.COL_NAME,DatabaseHelper.COL_COMPANY,DatabaseHelper.COL_DESIGNATION,
                            DatabaseHelper.COL_PHONE_NO,DatabaseHelper.COL_EMAIL},DatabaseHelper.COL_ID +" = " + id,null,null,null ,null);
            cursor.moveToFirst();

            String mId=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_ID));
            String name=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_NAME));
            String company=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_COMPANY));
            String designation=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_DESIGNATION));
            String phoneNo=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_PHONE_NO));
            String email=cursor.getString(cursor.getColumnIndex(DatabaseHelper.COL_EMAIL));
            cursor.close();
            contactModel=new ContactModel(mId,name,company,designation,phoneNo,email);;
            this.close();
            return contactModel;
        }


    }

i have done project where everything is working but can not make sort of data(name) alphatically

im07
  • 386
  • 2
  • 12
  • you must sort data in the way you want using `ORDER BY` when retrieving from database then you can add this sorted data to arraylist. – karimkhan Nov 11 '15 at 13:21

1 Answers1

0

When you have a list of Strings, you could sort them alphabetically like this for example

Collections.sort(list);

where list is your array with Strings

Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
  • is it ok if there adding object to the arraylist? and this line should be added? – im07 Nov 11 '15 at 15:07
  • the best decision is to sort the array at the end of the adding process (after all Strings are added into the list), but if you need it, you could sort the array after every adding iteration – Gabriella Angelova Nov 11 '15 at 15:20
  • i have tried with the code upload above.but its not working.will you please help me by fixing. – im07 Nov 11 '15 at 16:24
  • aaaa, your ArrayList is not of Strings, but of objects, that have fields of type String, so to compare them, you should implement `Comparable` in your ContactModel class and write a method that will compare them by name for example. Take a look at the selected as best answer here: http://stackoverflow.com/questions/11424369/how-to-sort-an-array-of-objects-in-java-in-field-level-for-grade-comparison – Gabriella Angelova Nov 11 '15 at 20:24