package fr.free.nrw.commons.nearby;


import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import fr.free.nrw.commons.R;

public class NearbyListFragment extends ListFragment {

    private LatLng mLatestLocation;
    private int mImageSize;
    private boolean mItemClicked;

    private static final String TAG = "NearbyListFragment";

    public NearbyListFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        Log.d(TAG, "NearbyListFragment created");

        View view = inflater.inflate(R.layout.fragment_nearby, container, false);


        // Create a progress bar to display while the list loads
        ProgressBar progressBar = new ProgressBar(getActivity());
        progressBar.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        progressBar.setIndeterminate(true);
        getListView().setEmptyView(progressBar);
        // Must add the progress bar to the root of the layout
        ViewGroup root = (ViewGroup) view.getRootView();
        root.addView(progressBar);

        // For the cursor adapter, specify which columns go into which views
        String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME};
        int[] toViews = {android.R.id.text1}; // The TextView in simple_list_item_1

        // Create an empty adapter we will use to display the loaded data.
        // We pass null for the cursor, then update it in onLoadFinished()
        mAdapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_1, null,
                fromColumns, toViews, 0);
        setListAdapter(mAdapter);

        // Prepare the loader.  Either re-connect with an existing one,
        // or start a new one.
        getLoaderManager().initLoader(0, null, this);
        return view;
    }


    // This is the Adapter being used to display the list's data
    SimpleCursorAdapter mAdapter;

    // These are the Contacts rows that we will retrieve
    static final String[] PROJECTION = new String[] {ContactsContract.Data._ID,
            ContactsContract.Data.DISPLAY_NAME};

    // This is the select criteria
    static final String SELECTION = "((" +
            ContactsContract.Data.DISPLAY_NAME + " NOTNULL) AND (" +
            ContactsContract.Data.DISPLAY_NAME + " != '' ))";


    // Called when a new Loader needs to be created
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        // Now create and return a CursorLoader that will take care of
        // creating a Cursor for the data being displayed.
        return new CursorLoader(this, ContactsContract.Data.CONTENT_URI,
                PROJECTION, SELECTION, null, null);
    }

    // Called when a previously created loader has finished loading
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        // Swap the new cursor in.  (The framework will take care of closing the
        // old cursor once we return.)
        mAdapter.swapCursor(data);
    }

    // Called when a previously created loader is reset, making the data unavailable
    public void onLoaderReset(Loader<Cursor> loader) {
        // This is called when the last Cursor provided to onLoadFinished()
        // above is about to be closed.  We need to make sure we are no
        // longer using it.
        mAdapter.swapCursor(null);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Do something when a list item is clicked
    }
}

@@ -426,16 +426,47 @@
 wGroup;%0A
+import android.widget.Adapter;%0A
 import a
@@ -677,16 +677,38 @@
 Clicked;
+%0A    Adapter mAdapter;
 %0A%0A    pr
@@ -1635,228 +1635,68 @@
   //
- For the cursor adapter, specify which columns go into which views%0A        String%5B%5D fromColumns = %7BContactsContract.Data.DISPLAY_NAME%7D;%0A        int%5B%5D toViews = %7Bandroid.R.id.text1%7D; // The TextView in simple_list_item_1%0A
+TODO: Original is an AttractionAdapter. Not a CursorAdapter.
 %0A   
@@ -1890,32 +1890,16 @@
 er(this,
-%0A               
  android
@@ -1933,32 +1933,16 @@
 1, null,
-%0A               
  fromCol
@@ -2185,1539 +2185,10 @@
 %7D%0A%0A%0A
-    // This is the Adapter being used to display the list's data%0A    SimpleCursorAdapter mAdapter;%0A%0A    // These are the Contacts rows that we will retrieve%0A    static final String%5B%5D PROJECTION = new String%5B%5D %7BContactsContract.Data._ID,%0A            ContactsContract.Data.DISPLAY_NAME%7D;%0A%0A    // This is the select criteria%0A    static final String SELECTION = %22((%22 +%0A            ContactsContract.Data.DISPLAY_NAME + %22 NOTNULL) AND (%22 +%0A            ContactsContract.Data.DISPLAY_NAME + %22 != '' ))%22;%0A%0A%0A    // Called when a new Loader needs to be created%0A    public Loader%3CCursor%3E onCreateLoader(int id, Bundle args) %7B%0A        // Now create and return a CursorLoader that will take care of%0A        // creating a Cursor for the data being displayed.%0A        return new CursorLoader(this, ContactsContract.Data.CONTENT_URI,%0A                PROJECTION, SELECTION, null, null);%0A    %7D%0A%0A    // Called when a previously created loader has finished loading%0A    public void onLoadFinished(Loader%3CCursor%3E loader, Cursor data) %7B%0A        // Swap the new cursor in.  (The framework will take care of closing the%0A        // old cursor once we return.)%0A        mAdapter.swapCursor(data);%0A    %7D%0A%0A    // Called when a previously created loader is reset, making the data unavailable%0A    public void onLoaderReset(Loader%3CCursor%3E loader) %7B%0A        // This is called when the last Cursor provided to onLoadFinished()%0A        // above is about to be closed.  We need to make sure we are no%0A        // longer using it.%0A        mAdapter.swapCursor(null);%0A    %7D
+%0A%0A
 %0A%0A  

Remove irrelevant CursorAdapter bits