Difference between revisions of "Teams Winter 2011/team2/lab5"
Line 277: | Line 277: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<br/> | <br/> | ||
− | + | == Running Application == | |
+ | For running the Application we have to click on run and it will run Android SDK and runn application | ||
[[Image:007.jpg]] | [[Image:007.jpg]] | ||
<br/> | <br/> | ||
<br/> | <br/> | ||
[[Image:android2.jpg ]] | [[Image:android2.jpg ]] |
Revision as of 21:17, 16 April 2011
Contents
Team 2 -ECL500 Android Application Development with Eclipse - Lab 5
For this lab, we will create a small Android application called Parking that will search in a database and shows number of cars based on searched brand and model.
Prepare Environment
1. First we have to install Android SDk. the latest version is in AnDroidSDK.
- we can install different virtual machines with this SDK manager
2. Then we have to prepare Eclipse for developing Android by going to Help -> install new software. and add https://dl-ssl.google.com/android/eclipse/ repository.
3. Then in Windows-> preference -> Android we have to set location of SDK that we installed at step 1
Start Application
1. start a New Android Project by choosing File -> New -> Android Project
2. Type Project Name and Properties of App ( for convenience you can choose create project from existing Sample)
3. Eclipse SDK give developers a GUI interface to design Application easy. by click on res->layout->main.xml developer can design the Application
4.Now we can write code of classes
DatabaseHelper.java
package Parking.Garage;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.hardware.SensorManager;
import android.provider.SyncStateContract.Constants;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "car_db";
private static final String BRAND = "brand";
private static final String MODEL = "model";
private static final String COUNT = "count";
public DatabaseHelper(Context context) {
super(context, getDatabaseName(), null, 1);
// TODO Auto-generated constructor stub
//openNewDb();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//db.execSQL("DROP TABLE Cars;");
db.execSQL("CREATE TABLE Cars (car_id INTEGER PRIMARY KEY AUTOINCREMENT, brand TEXT, model TEXT, count INTEGER);");
ContentValues cv = new ContentValues();
cv.put(getBrand(), "Bentley");
cv.put(getModel(), "Continental GT");
cv.put(COUNT, 1);
db.insert("Cars", getBrand(), cv );
cv.put(getBrand(), "Honda");
cv.put(getModel(), "Accord");
cv.put(COUNT, 23);
db.insert("Cars", getBrand(), cv );
cv.put(getBrand(), "Honda");
cv.put(getModel(), "Civic");
cv.put(COUNT, 35);
db.insert("Cars", getBrand(), cv );
cv.put(getBrand(), "Honda");
cv.put(getModel(), "Prelude");
cv.put(COUNT, 40);
db.insert("Cars", getBrand(), cv );
cv.put(getBrand(), "Mazda");
cv.put(getModel(), "5");
cv.put(COUNT, 7);
db.insert("Cars", getBrand(), cv );
cv.put(getBrand(), "Mazda");
cv.put(getModel(), "3");
cv.put(COUNT, 5);
db.insert("Cars", getBrand(), cv );
cv.put(getBrand(), "Mazda");
cv.put(getModel(), "626");
cv.put(COUNT, 2);
db.insert("Cars", getBrand(), cv );
cv.put(getBrand(), "Mazda");
cv.put(getModel(), "323");
cv.put(COUNT, 6);
db.insert("Cars", getBrand(), cv );
cv.put(getBrand(), "Nissan");
cv.put(getModel(), "Maxima");
cv.put(COUNT, 90);
db.insert("Cars", getBrand(), cv );
cv.put(getBrand(), "Nissan");
cv.put(getModel(), "Altima");
cv.put(COUNT, 1);
db.insert("Cars", getBrand(), cv );
cv.put(getBrand(), "Nissan");
cv.put(getModel(), "Sentra");
cv.put(COUNT, 3);
db.insert("Cars", getBrand(), cv );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
android.util.Log.w("Cars","Upgrading database, which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS Cars");
onCreate(db);
}
public SQLiteDatabase openNewDb()
{
SQLiteDatabase db = this.getWritableDatabase();
return db;
}
public ArrayList<String> getCar(SQLiteDatabase db, String userBrand, String userModel){
//db = getWritableDatabase();
ArrayList<String> list = new ArrayList<String>();
String brandTemp = "";
String modelTemp = "";
int countTemp =0;
String[] inputs = {userModel, userBrand};
Cursor c = db.rawQuery("SELECT brand, model, count FROM Cars WHERE " + MODEL + "= ? AND " + BRAND + " = ?", inputs );
c.moveToFirst();
while(!c.isAfterLast())
{
brandTemp = c.getString(0);
modelTemp = c.getString(1);
countTemp = c.getInt(2);
c.moveToNext();
}
c.close();
list.add(brandTemp);
list.add(modelTemp);
list.add(countTemp + "");
return list;
}
public static String getBrand() {
return BRAND;
}
public static String getModel() {
return MODEL;
}
public static String getDatabaseName() {
return DATABASE_NAME;
}
}
Park.java
package Parking.Garage;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Park extends Activity implements OnClickListener {
Button btnSearch;
Button btnCreate;
EditText editModel, editBrand;
DatabaseHelper carModel = new DatabaseHelper(this);
SQLiteDatabase db;
ArrayList<String> cars = new ArrayList<String>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSearch = (Button) findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(this);
btnCreate = (Button) findViewById(R.id.btnCreate);
btnCreate.setOnClickListener(this);
editBrand = (EditText) findViewById(R.id.editBrand);
editModel = (EditText) findViewById(R.id.editModel);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String brand = editBrand.getText().toString();
String model = editModel.getText().toString();
if(v == btnSearch)
{
if(brand.isEmpty()||model.isEmpty())
{
new AlertDialog.Builder(this)
.setTitle("Empty Fields")
.setMessage("All fields are mandatory")
.setNeutralButton("Close", null)
.show();
}
else
{
db = carModel.openNewDb();
cars = carModel.getCar(db, brand, model);
int a = cars.size();
if(a == 0)
{
editBrand.setText("nothing");
}
else
{
String carBrand = cars.get(0);
String carModell = cars.get(1);
String carCount = cars.get(2);
new AlertDialog.Builder(this)
.setTitle("Car Information")
.setMessage("Brand: " + carBrand + "\n" +
"Model: " + carModell + "\n" +
"Count: " + carCount )
.setNeutralButton("Done", null)
.show();
}
}
}
else if (v == btnCreate)
{
editBrand.setText(brand);
editModel.setText(model);
}
}
}
Running Application
For running the Application we have to click on run and it will run Android SDK and runn application