-1

I have read for hours and hours to figure out how to sort and alphabetize a String[], but since I am pulling from my phone's Contacts and this whole ListView is a Fragment, there is nothing that really addresses my situation. I must use a CursorAdapter in my String[] and some alphabetize tutorials use other adapters, so I couldn't figure it out.

If you know of a very simple way to sort/alphabetize my String[] using the code I already have (considering all the particulars that come with using a ListView, Fragment, and pulling from Contacts), I would be very grateful. Currently this code works, but it pulls contacts that are not sorted.

Thanks for your suggestions.

ContactsFragment.java

package com.example.practiceapp.addressbook;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.ListFragment;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.example.practiceapp.R;

/*
 * Taken from http://stackoverflow.com/questions/18199359/how-to-display-contacts-in-a-listview-in-android-for-android-api-11
 */

public class ContactsFragment extends ListFragment implements 
                                LoaderCallbacks<Cursor>{

    private CursorAdapter mAdapter;
    public ListView listView;
    // Name should be displayed in the text1 TextView in item layout
    public static String[] from = { ContactsContract.Contacts.DISPLAY_NAME_PRIMARY };
    private static final int[] TO = { android.R.id.text1 };
    private android.content.Context context;
    ArrayList<String> elements;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // create adapter once
        context = getActivity();
        int layout = android.R.layout.simple_list_item_1;
        Cursor c = null; // there is no cursor yet
        int flags = 0; // no auto-requery! Loader requeries.

        Arrays.sort(from);

        // put List in adapter
        mAdapter = new SimpleCursorAdapter(context, layout, c, from, TO, flags);

    }

    // columns requested from the database
    private static final String[] PROJECTION = {
            Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME_PRIMARY
    };

    // Empty public constructor, required by the system
    public ContactsFragment() {}

    // A UI Fragment must inflate its View (all fragments must override onCreateView)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the fragment layout
        View view =  inflater.inflate(R.layout.contact_list_view,
            container, false);
        listView = (ListView) view.findViewById(R.id.list); 
        return view;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // and tell loader manager to start loading
        getLoaderManager().initLoader(0, null, this);

        listView.setAdapter(mAdapter);
        listView.setFastScrollEnabled(true);

    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        // load from the "Contacts table"
        Uri contentUri = Contacts.CONTENT_URI;

        // no sub-selection, no sort order, simply every row
        // projection says we want just the _id and the name column
        return new CursorLoader(getActivity(),
                contentUri,
                PROJECTION,
                null,
                null,
                null
        );
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
         // Once cursor is loaded, give it to adapter
        mAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        // Delete the reference to the existing Cursor,
        // so it can recycle it
        mAdapter.swapCursor(null);

    }
}
Azurespot
  • 3,066
  • 3
  • 45
  • 73

1 Answers1

1

The last parameter to the CursorLoader constructor is the sort order. It uses the same syntax as a normal SQL ORDER BY clause.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Thanks Gabe, I'm still learning what goes where. Much of this code was borrowed, which means I don't always understand what it does. So I looked at the `CursorLoader` API (thanks for the suggestion) and it takes a `String` as a sort order variable. But what would this `String` be? I'm not sure what to put there. I don't know what a `SQL ORDER BY` clause is... – Azurespot Dec 22 '14 at 04:11
  • Actually I got it! It was just asking for the field name I wanted to sort my contacts by. Thank you so much! I can't believe all the crazy complicated code out there that tries to do this same thing, when this is so simple. A million thanks. :) – Azurespot Dec 22 '14 at 04:15