Changes

Jump to: navigation, search

Teams Winter 2011/team2/lab5

6,258 bytes added, 19:58, 16 April 2011
Start Application
== Start Application ==
1. start a New Android Project by choosing File -> New -> Android Project<br/>2. Type Project Name and Properties of App ( for convenience you can choose create project from existing Sample) <br/>
[[Image:Androidset1.png ]]
 <br/>3. Eclipse SDK give developers a GUI interface to design Application easy. by click on res->layout->main.xml developer can design the Application<br/>
[[Image:AndSetup2.png]]
4.
<br/>
4.Now we can write code of classes
 
DatabaseHelper.java
<syntaxhighlight lang="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;
}
}
 
</syntaxhighlight>
<br/>
 
Park.java
<syntaxhighlight lang="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);
}
}
}
 
</syntaxhighlight>
<br/>
1
edit

Navigation menu