| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 
 | package com.example.mon.app;
 
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
 
public class FlightBook extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor>
{
	private ListView ReservationList = null;
	private Cursor c = null;
	private SimpleCursorAdapter adapter = null;
	private ReservationsBDD rdb = new ReservationsBDD(this);
 
	private static final int LOADER_ID = 0;
	private static final String[] PROJECTION = new String[] {MaBaseSQLite.COL_ID, MaBaseSQLite.getColReference(), MaBaseSQLite.getColLastname()};
	private LoaderManager.LoaderCallbacks<Cursor> mCallbacks;
 
    @SuppressWarnings("deprecation")
	@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flight_book);
 
        ReservationList = (ListView) findViewById(R.id.flightList);
 
        //Initialiser la base de données
        rdb.open();
 
        String[] from = { MaBaseSQLite.getColReference(), MaBaseSQLite.getColLastname() };
        int[] to = { R.id.reference_entry, R.id.lastname_entry };
 
        //TODO: NE PAS UTILISER LES METHODES DECONSEILLEES PAR ECLIPSE
        Cursor cursor = managedQuery(ReservationProvider.CONTENT_URI, PROJECTION, null, null, null);
	    adapter = new SimpleCursorAdapter(this, R.layout.flight_book_list, cursor, from, to);
 
        ReservationList.setAdapter(adapter);
        mCallbacks = this;
 
        //ReservationList.setAdapter(mAdapter);
        getSupportLoaderManager().initLoader(LOADER_ID, null, mCallbacks);
 
        //Gérer le clic simple sur un élément de la listView
        ReservationList.setOnItemClickListener(new OnItemClickListener() 
        {
        	long selectedItemID = 0;
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
			{
				//Requêter la réservation par son ID
				Reservation res = rdb.getReservationWithID(id);
 
		        //Afficher l'activité ReservationInfo
		        Intent myIntent = new Intent(view.getContext(), ReservationInfo.class);
		        myIntent.putExtra("ReservationReference", res.getReference());
		        myIntent.putExtra("LastName", res.getLastname());
		        startActivityForResult(myIntent, 0);
			}
        });
 
        //Gestion du clic long sur un élément de la liste
        ReservationList.setOnItemLongClickListener (new OnItemLongClickListener() 
        {
        	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) 
        	{
        		return onLongListItemClick(view, position, id);
        	}
 
	        protected boolean onLongListItemClick(final View v, final int pos, long id) 
	        {
	           final String str=ReservationList.getItemAtPosition(pos).toString();
	           Log.i("ListView", "onLongListItemClick string=" + str);
	           AlertDialog.Builder builder = new AlertDialog.Builder(FlightBook.this);
	           builder.setMessage("Delete this entry from Flight Book?")
	           .setCancelable(false)
	           .setPositiveButton("Yes", new DialogInterface.OnClickListener() 
	           {
	        	   public void onClick(DialogInterface dialog, int id) 
	        	   {
	        		   Log.i("ID",""+pos);
 
	        		   //TODO: UTILISER LE CURSORLOADER SI POSSIBLE!
	        		   rdb.removeReservationWithID((int)adapter.getItemId(pos));
 
	        		   //TODO: Gérer la mise à jour de la liste dans l'affichage en temps réel
 
	        		   //Refresh ListView automatically
	        		   //Cursor c = rdb.queueAll();
	        	       //startManagingCursor(c);
	        	       //mAdapter.changeCursor(c);
 
			           /*Cursor c = getContentResolver().query(Uri.withAppendedPath(ReservationProvider.CONTENT_URI, String.valueOf(id)), PROJECTION, null, null, null);
			           if (c.moveToFirst()) 
			           {
			               String reservationUrl = c.getString(0);
			               //tutSelectedListener.onTutSelected(reservationUrl);
			           }*/
	        	   }
	           })
	           .setNegativeButton("No", new DialogInterface.OnClickListener() 
	           {
	               public void onClick(DialogInterface dialog, int id) 
	               {
	                   dialog.cancel();
	               }
	           });
	           AlertDialog alert = builder.create();
	           alert.show();
	           return true;
            }
        });
    }
 
    public void onPause()
    {
    	super.onPause();
    }
 
    public void onDestroy()
    {
    	super.onDestroy();
    	rdb.close();
    }
 
    public void onStart()
    {
    	super.onStart();
    }
 
    public void onStop()
    {
    	super.onStop();
    }
 
    public void onResume(Bundle savedInstanceState) 
    {
    	super.onResume();
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        getMenuInflater().inflate(R.menu.activity_flight_book, menu);
        return true;
    }
 
	public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) 
	{
		// TODO Auto-generated method stub
		return new CursorLoader(FlightBook.this, ReservationProvider.CONTENT_URI, PROJECTION, null, null, null);
	}
 
	//@Override -> won't work, says "must override a superclass method"
	public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) 
	{
		adapter.swapCursor(cursor);
	}
 
	//@Override -> won't work, says "must override a superclass method"
	public void onLoaderReset(Loader<Cursor> loader) 
	{
		adapter.swapCursor(null);
	}
} | 
Partager