Wednesday, 30 January 2013

Detail Form Supports Add and Edit | Android Tutorial pdf

Detail Form Supports Add and Edit

Last, but certainly not least, we need to have DetailForm properly do useful work when the Save button is clicked. Specifically, we need to either insert or update the database. It would also be nice if we dismissed the DetailForm at that point and returned to the main LunchList activity.
To accomplish this, we first need to add an update() method to RestaurantHelper that can perform a database update:
public void update(String id, String name, String address,
String type, String notes) {
ContentValues cv=new ContentValues();
String[] args={id};
cv.put("name", name);
cv.put("address", address);
cv.put("type", type);
cv.put("notes", notes);
getWritableDatabase().update("restaurants", cv, "_ID=?",
args);
}

Then, we need to adjust our onSave listener object in DetailForm to call the right method (save() or update()) and finish() our activity:
private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
String type=null;
switch (types.getCheckedRadioButtonId()) {
case R.id.sit_down:
type="sit_down";
break;
case R.id.take_out:
type="take_out";
break;
case R.id.delivery:
type="delivery";
break;
}
if (restaurantId==null) {
helper.insert(name.getText().toString(),
address.getText().toString(), type,
notes.getText().toString());
}
else {
helper.update(restaurantId, name.getText().toString(),
address.getText().toString(), type,
notes.getText().toString());
}
finish();
}
};

At this point, you should be able to recompile and reinstall the application.
When you first bring up the application, it will no longer show the tabs:
                                                    The new-and-improved LunchList
However, it will have an "add" menu option:

 The LunchList options menu, with Add
If you choose the "add" menu option, it will bring up a blank DetailForm:
                                                     The DetailForm activity
If you fill out the form and click Save, it will return you to the LunchList and immediately shows the new restaurant (courtesy of our using a managed Cursor in LunchList):
                                                   The LunchList with an added Restaurant
If you click an existing restaurant, it will bring up the DetailForm for that object:
                                                  The DetailForm on an existing RestaurantMaking changes and clicking Save will update the database and list:
                                                 The LunchList with an edited Restaurant
Here is one implementation of LunchList that incorporates all of this tutorial's changes:
package apt.tutorial;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class LunchList extends ListActivity {
public final static String ID_EXTRA="apt.tutorial._ID";
Cursor model=null;
RestaurantAdapter adapter=null;
RestaurantHelper helper=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
helper=new RestaurantHelper(this);
model=helper.getAll();
startManagingCursor(model);
adapter=new RestaurantAdapter(model);
setListAdapter(adapter);
}
@Override
public void onDestroy() {
super.onDestroy();
helper.close();
}
@Override
public void onListItemClick(ListView list, View view,
int position, long id) {
Intent i=new Intent(LunchList.this, DetailForm.class);
i.putExtra(ID_EXTRA, String.valueOf(id));
startActivity(i);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.option, menu);
return(super.onCreateOptionsMenu(menu));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==R.id.add) {
startActivity(new Intent(LunchList.this, DetailForm.class));
return(true);
}
return(super.onOptionsItemSelected(item));
}
class RestaurantAdapter extends CursorAdapter {
RestaurantAdapter(Cursor c) {
super(LunchList.this, c);
}
@Override
public void bindView(View row, Context ctxt,
Cursor c) {
RestaurantHolder holder=(RestaurantHolder)row.getTag();
holder.populateFrom(c, helper);
}
@Override
public View newView(Context ctxt, Cursor c,
ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
RestaurantHolder holder=new RestaurantHolder(row);
row.setTag(holder);
return(row);
}
}
static class RestaurantHolder {
private TextView name=null;
private TextView address=null;
private ImageView icon=null;
RestaurantHolder(View row) {
name=(TextView)row.findViewById(R.id.title);
address=(TextView)row.findViewById(R.id.address);
icon=(ImageView)row.findViewById(R.id.icon);
}
void populateFrom(Cursor c, RestaurantHelper helper) {
name.setText(helper.getName(c));
address.setText(helper.getAddress(c));
if (helper.getType(c).equals("sit_down")) {
icon.setImageResource(R.drawable.ball_red);
}
else if (helper.getType(c).equals("take_out")) {
icon.setImageResource(R.drawable.ball_yellow);
}
else {
icon.setImageResource(R.drawable.ball_green);
}
}
}
}

Here is one implementation of DetailForm that works with the revised LunchList:
package apt.tutorial;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
public class DetailForm extends Activity {
EditText name=null;
EditText address=null;
EditText notes=null;
RadioGroup types=null;
RestaurantHelper helper=null;
String restaurantId=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_form);
helper=new RestaurantHelper(this);
name=(EditText)findViewById(R.id.name);
address=(EditText)findViewById(R.id.addr);
notes=(EditText)findViewById(R.id.notes);
types=(RadioGroup)findViewById(R.id.types);
Button save=(Button)findViewById(R.id.save);
save.setOnClickListener(onSave);
restaurantId=getIntent().getStringExtra(LunchList.ID_EXTRA);
if (restaurantId!=null) {
load();
}
}
@Override
public void onDestroy() {
super.onDestroy();
helper.close();
}
private void load() {
Cursor c=helper.getById(restaurantId);
c.moveToFirst();
name.setText(helper.getName(c));
address.setText(helper.getAddress(c));
notes.setText(helper.getNotes(c));
if (helper.getType(c).equals("sit_down")) {
types.check(R.id.sit_down);
}
else if (helper.getType(c).equals("take_out")) {
types.check(R.id.take_out);
}
else {
types.check(R.id.delivery);
}
c.close();
}
private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
String type=null;
switch (types.getCheckedRadioButtonId()) {
case R.id.sit_down:
type="sit_down";
break;
case R.id.take_out:
type="take_out";
break;
case R.id.delivery:
type="delivery";
break;
}
if (restaurantId==null) {
helper.insert(name.getText().toString(),
address.getText().toString(), type,
notes.getText().toString());
}
else {
helper.update(restaurantId, name.getText().toString(),
address.getText().toString(), type,
notes.getText().toString());
}
finish();
}
};
}

And, here is an implementation of RestaurantHelper with the changes needed by DetailForm:
package apt.tutorial;
import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
class RestaurantHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="lunchlist.db";
private static final int SCHEMA_VERSION=1;
public RestaurantHelper(Context context) {
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE restaurants (_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT, address TEXT, type TEXT, notes TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// no-op, since will not be called until 2nd schema
// version exists
}
public Cursor getAll() {
return(getReadableDatabase()
.rawQuery("SELECT _id, name, address, type, notes FROM restaurants
ORDER BY name",
null));
}
public Cursor getById(String id) {
String[] args={id};
return(getReadableDatabase()
.rawQuery("SELECT _id, name, address, type, notes FROM restaurants
WHERE _ID=?",
args));
}
public void insert(String name, String address,
String type, String notes) {
ContentValues cv=new ContentValues();
cv.put("name", name);
cv.put("address", address);
cv.put("type", type);
cv.put("notes", notes);
getWritableDatabase().insert("restaurants", "name", cv);
}
public void update(String id, String name, String address,
String type, String notes) {
ContentValues cv=new ContentValues();
String[] args={id};
cv.put("name", name);
cv.put("address", address);
cv.put("type", type);
cv.put("notes", notes);
getWritableDatabase().update("restaurants", cv, "_ID=?",
args);
}
public String getName(Cursor c) {
return(c.getString(1));
}
public String getAddress(Cursor c) {
return(c.getString(2));
}
public String getType(Cursor c) {
return(c.getString(3));
}
public String getNotes(Cursor c) {
return(c.getString(4));
}
}

No comments: