Thursday, June 30, 2011

Address Panel Post 2: RelativeLayout Class (java)

In post 1 I showed you the XML files that define the layout for the control. In this post I am going to show you the java file behind the layout. It's named UserAddress.java. UserAddress is a subclass of RelativeLayout. It is responsible for loading all the data from the database when the form is entered. It also contains the code to save the values to the database.


package ewe.custom.controls.address;

import ewe.custom.controls.R;
import ewe.custom.controls.database.MyDb;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Spinner;

/**
 * @author - http://droidcontrols.blogspot.com
 * This is a view that contains the standard fields for address entry.  
 */
/**
 * @author http://droidcontrols.blogspot.com
 *
 */
public class UserAddress extends RelativeLayout {
 Button btnSave; //save button
 EditText addr1; //address line one input
 EditText addr2; //address line two input
 EditText city; //city input
 Spinner state; // state input
 EditText zip; // zip code input
 MyDb mdb; //TODO change to your database if you don't want to use the one i provided in blog 3

 //list of states
 private static final String[] states = { "Alabama", "Alaska", "Arizona",
   "Arkansas", "California", "Colorado", "Connecticut", "Delaware",
   "Distric of Columbia", "Florida", "Georgia", "Hawaii", "Idaho",
   "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana",
   "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota",
   "Mississippi", "Missouri", "Montana", "Nevada", "New Hampshire",
   "New Jersey", "New Mexico", "New York", "North Carolina",
   "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania",
   "Rhode Island", "South Carolina", "South Dakota", "Tennessee",
   "Texas", "Utah", "Vermont", "Virginia", "Washington", "Wisconsin",
   "Wyoming" };

 /**
  * constructor #1
  * @param context
  */
 public UserAddress(Context context) {
  super(context);
  // First constructor
  inflateLayout(context);
  initializeControls(context);
  loadValuesFromDatabase(context);
 }

 /**
  * constructor #2
  * @param context
  * @param attrs
  */
 public UserAddress(Context context, AttributeSet attrs) {
  super(context, attrs);
  //Second constructor - used most often
  inflateLayout(context);
  initializeControls(context);
  loadValuesFromDatabase(context);
 }

 /**
  * constructor #3
  * @param context
  * @param attrs
  * @param defStyle
  */
 public UserAddress(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  // Third constructor
  inflateLayout(context);
  initializeControls(context);
  loadValuesFromDatabase(context);
 }

 /**
  * Inflates the main xml file addressform.xml (See blog post #1)
  * @param context
  */
 private void inflateLayout(Context context) {
  LayoutInflater vi = (LayoutInflater) context
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  //inflate addressform.xml
  View v = vi.inflate(R.layout.addressform, this);

 }

 /**
  * This method will load associate the view objects with the views in the xml file. 
  * It also populates the spinner.
  */
 private void initializeControls(Context context) {
  //point to the views in the xml file
  addr1 = (EditText) findViewById(R.id.et_addr1);
  addr2 = (EditText) findViewById(R.id.et_addr2);
  city = (EditText) findViewById(R.id.et_city);
  state = (Spinner) findViewById(R.id.sp_state);
  zip = (EditText) findViewById(R.id.et_zip);
  btnSave = (Button) findViewById(R.id.btn_addr_save);

  //set the array adapter to the spinner
  //will display the dates in the String[] states
  //the spinner_entry.xml is an xml file whose root element is a single textview
  ArrayAdapter<String> mySpinnerAdapter = new ArrayAdapter<String>(
    context, R.layout.spinner_entry, states);
  state.setAdapter(mySpinnerAdapter);

  //what to do when the save button is clicked
  btnSave.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View arg0) {
    //get the index of the item selected in the dropdown
    String lsstate = Integer.toString(state
      .getSelectedItemPosition());
    //TODO implement how your own database saves the information
    mdb.saveAddress(addr1.getText().toString().trim(), addr2
      .getText().toString().trim(), city.getText().toString()
      .trim(), lsstate, zip.getText().toString().trim());
   }

  });
 }

 /**
  * @param context from activity that contains the view
  * 
  * This will load the data from the database if the address was previously saved
  */
 private void loadValuesFromDatabase(Context context) {

  //TODO implement using your applications SQL database
  mdb = new MyDb(context);
  mdb.open();

  //if you don't include this if statement the code may crash when 
  //in the designer/graphical view of the xmlfile
  if (!this.isInEditMode()) {

   //this is a method defined in our database used to get the bundle(hashtable) containing the user's 

saved address
   Bundle bAddr = mdb.getAddressBundle();

   if (bAddr != null) {
    if (bAddr.getString("addr1") != null) {
     //sets the value of the EditText containing address line 1
     addr1.setText(bAddr.getString("addr1"));
    }
    if (bAddr.getString("addr2") != null) {
     addr2.setText(bAddr.getString("addr2"));
    }
    if (bAddr.getString("city") != null) {
     city.setText(bAddr.getString("city"));
    }
    if (bAddr.getString("state") != null) {
     //sets the value of the City Spinner 
     state.setSelection(Integer.parseInt(bAddr
       .getString("state")));
    }
    if (bAddr.getString("zip") != null) {
     zip.setText(bAddr.getString("zip"));
    }
   }

  }

 }
}



If you have your own database then you will need to create a method to retrieve and save the address. You can use the methods I have written in the next post. If you don't have a database set up just use the one I provide in post 3. #TODO add links

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home