mirror of
				https://github.com/bspeice/itcs4180
				synced 2025-11-03 18:00:37 -05:00 
			
		
		
		
	Refactor existing API to use the new DatabaseHelper
This commit is contained in:
		@ -11,7 +11,7 @@ import android.widget.BaseAdapter;
 | 
			
		||||
import android.widget.ImageView;
 | 
			
		||||
import android.widget.ListView;
 | 
			
		||||
import android.widget.TextView;
 | 
			
		||||
import edu.uncc.scavenger.database.LocationDatabaseClient;
 | 
			
		||||
import edu.uncc.scavenger.database.LocationDatabaseHelper;
 | 
			
		||||
import edu.uncc.scavenger.rest.LocationClient;
 | 
			
		||||
import edu.uncc.scavenger.rest.RestLocation;
 | 
			
		||||
 | 
			
		||||
@ -26,7 +26,7 @@ public class MainActivity extends Activity {
 | 
			
		||||
		
 | 
			
		||||
		// Get our list of events loaded
 | 
			
		||||
		locationList = (ListView)findViewById(R.id.listLocations);
 | 
			
		||||
		List<RestLocation> locations = new LocationDatabaseClient().getAllLocations();
 | 
			
		||||
		List<RestLocation> locations = LocationDatabaseHelper.getInstance(this).fetchAll();
 | 
			
		||||
		if (locations != null && locations.size() > 0) {
 | 
			
		||||
			LocationAdapter mLocationAdapter = new LocationAdapter(locations);
 | 
			
		||||
			locationList.setAdapter(mLocationAdapter);
 | 
			
		||||
 | 
			
		||||
@ -1,33 +0,0 @@
 | 
			
		||||
package edu.uncc.scavenger.database;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import edu.uncc.scavenger.rest.RestLocation;
 | 
			
		||||
 | 
			
		||||
public class LocationDatabaseClient {
 | 
			
		||||
	
 | 
			
		||||
	/*
 | 
			
		||||
	 * Super high-level API for working with locations.
 | 
			
		||||
	 * Also helpful for just drawing out skeletons of code.
 | 
			
		||||
	 */
 | 
			
		||||
	
 | 
			
		||||
	public List<RestLocation> getAllLocations() {
 | 
			
		||||
		// TODO: Implement method to get all locations that exist in the database
 | 
			
		||||
		return null;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public void persistLocation(RestLocation location) {
 | 
			
		||||
		// TODO: Save a location to the database
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public void persistLocation(List<RestLocation> locations) {
 | 
			
		||||
		for (RestLocation l: locations) {
 | 
			
		||||
			persistLocation(l);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public void synchronizeLocations(List<RestLocation> locations) {
 | 
			
		||||
		// TODO: Implement method to save all values in locations, and remove values that aren't there.
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
@ -3,6 +3,7 @@ package edu.uncc.scavenger.database;
 | 
			
		||||
// Design pattern from: http://www.androiddesignpatterns.com/2012/05/correctly-managing-your-sqlite-database.html
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import edu.uncc.scavenger.rest.RestLocation;
 | 
			
		||||
import android.content.ContentValues;
 | 
			
		||||
@ -32,7 +33,7 @@ public class LocationDatabaseHelper extends SQLiteOpenHelper {
 | 
			
		||||
		super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	public LocationDatabaseHelper getInstance(Context ctx) {
 | 
			
		||||
	public static LocationDatabaseHelper getInstance(Context ctx) {
 | 
			
		||||
		if (helper == null) {
 | 
			
		||||
			helper = new LocationDatabaseHelper(ctx.getApplicationContext());
 | 
			
		||||
		}
 | 
			
		||||
@ -104,5 +105,9 @@ public class LocationDatabaseHelper extends SQLiteOpenHelper {
 | 
			
		||||
		helper.getWritableDatabase().insert(TABLE_NAME, null, values);
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	
 | 
			
		||||
	public void persistAll(List<RestLocation> locs) {
 | 
			
		||||
		for (RestLocation l : locs) {
 | 
			
		||||
			persist(l);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -6,7 +6,7 @@ import java.util.Map;
 | 
			
		||||
import java.util.concurrent.ExecutionException;
 | 
			
		||||
 | 
			
		||||
import edu.uncc.scavenger.R;
 | 
			
		||||
import edu.uncc.scavenger.database.LocationDatabaseClient;
 | 
			
		||||
import edu.uncc.scavenger.database.LocationDatabaseHelper;
 | 
			
		||||
import android.content.Context;
 | 
			
		||||
import android.os.AsyncTask;
 | 
			
		||||
import retrofit.RestAdapter;
 | 
			
		||||
@ -30,15 +30,15 @@ public class LocationClient {
 | 
			
		||||
 | 
			
		||||
	public static class LocationsDownloader extends
 | 
			
		||||
			AsyncTask<Void, Void, List<RestLocation>> {
 | 
			
		||||
		LocationService client;
 | 
			
		||||
		Context ctx;
 | 
			
		||||
 | 
			
		||||
		public LocationsDownloader(Context ctx) {
 | 
			
		||||
			client = getAdapter(ctx);
 | 
			
		||||
			this.ctx = ctx;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		@Override
 | 
			
		||||
		protected List<RestLocation> doInBackground(Void... arg0) {
 | 
			
		||||
			return client.listLocations();
 | 
			
		||||
			return getAdapter(ctx).listLocations();
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -47,7 +47,7 @@ public class LocationClient {
 | 
			
		||||
		return new LocationsDownloader(ctx) {
 | 
			
		||||
			@Override
 | 
			
		||||
			protected void onPostExecute(List<RestLocation> result) {
 | 
			
		||||
				new LocationDatabaseClient().synchronizeLocations(result);
 | 
			
		||||
				LocationDatabaseHelper.getInstance(ctx).persistAll(result);
 | 
			
		||||
			}
 | 
			
		||||
		}.execute();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user