package com.sgrailways.giftidea;

import android.R;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import com.google.inject.Inject;
import roboguice.fragment.RoboListFragment;

import static android.provider.BaseColumns._ID;
import static com.sgrailways.giftidea.Database.RecipientsTable.NAME;
import static com.sgrailways.giftidea.Database.RecipientsTable.TABLE_NAME;

public class RecipientsList extends RoboListFragment {
    @Inject Database database;

    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        SQLiteDatabase rdb = database.getReadableDatabase();
        Cursor cursor = rdb.query(TABLE_NAME, new String[]{_ID, NAME}, null, null, null, null, NAME + " ASC");
        ListAdapter adapter = new SimpleCursorAdapter(
                this.getActivity(),
                R.layout.simple_list_item_1,
                cursor,
                new String[]{NAME},
                new int[]{R.id.text1}
        );
        setListAdapter(adapter);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override public void onResume() {
        SQLiteDatabase rdb = database.getReadableDatabase();
        ((SimpleCursorAdapter)getListAdapter()).swapCursor(rdb.query(TABLE_NAME, new String[]{_ID, NAME}, null, null, null, null, NAME + " ASC"));
        super.onResume();
    }
}

@@ -808,180 +808,8 @@
 ) %7B%0A
-        SQLiteDatabase rdb = database.getReadableDatabase();%0A        Cursor cursor = rdb.query(TABLE_NAME, new String%5B%5D%7B_ID, NAME%7D, null, null, null, null, NAME + %22 ASC%22);%0A
     
@@ -962,16 +962,18 @@
   cursor
+()
 ,%0A      
@@ -1202,24 +1202,157 @@
 nResume() %7B%0A
+        ((SimpleCursorAdapter)getListAdapter()).swapCursor(cursor());%0A        super.onResume();%0A    %7D%0A%0A    private Cursor cursor() %7B%0A
         SQLi
@@ -1412,59 +1412,15 @@
     
-((SimpleCursorAdapter)getListAdapter()).swapCursor(
+return 
 rdb.
@@ -1504,35 +1504,8 @@
 SC%22)
-);%0A        super.onResume()
 ;%0A  

remove dups