title: Coding the correct way. Android onCreate and initialization
body: <p>Im developing an app tat deals with getting JSON data from a server parsing it and setting adapters to view the data in a listview. What I really want is to know if im starting the activity off the right way. I have some repeated code in setting the adapters for the different spinners and also setting their onClick listeners. How would i go about consolidating this code so it doesnt end up in a big mess. Ive found out that Its hard for me to continue coding my other activities because Im running into issues like this. Thanks for any help offered. </p>
  
  <pre><code>package com.bde.dgcr;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.*;
  
  public class CourseSearch extends Activity {
  
  private String citySearch;
  private String stateSearch;
  private String countrySearch;
  private String mode;
  
  public void onCreate(Bundle savedInstanceState) {
      // call Activity onCreate method to take care of all initial setup before we do
      // any other customizing of the method
      super.onCreate(savedInstanceState);
      //setting the current view to the mail xml file.
      setContentView(R.layout.main);
      // here we are creating new spinners and connecting their adapters to them. This needs to be
      // made into its own function. Any code that you are repeating should be made into its own function.
      Spinner countriesSpinner = (Spinner) findViewById(R.id.country);
  
      ArrayAdapter&lt;CharSequence&gt; adapter = ArrayAdapter.createFromResource(
              this, R.array.countries_array, android.R.layout.simple_spinner_item);
      adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      countriesSpinner.setAdapter(adapter);
      countriesSpinner.setOnItemSelectedListener(new onCountrySelectedListener());
      //state spinner
      Spinner stateSpinner = (Spinner) findViewById(R.id.state);
      ArrayAdapter&lt;CharSequence&gt; state_adapter = ArrayAdapter.createFromResource(
              this, R.array.states_array, android.R.layout.simple_spinner_item);
      state_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
      stateSpinner.setAdapter(state_adapter);
      stateSpinner.setOnItemSelectedListener(new onStateSelectedListener());
  }
  
  
  public void searchByLoc(View search) {
      final EditText cityField = (EditText) findViewById(R.id.cityName);
      citySearch = cityField.getText().toString();
  
      if (citySearch != null)
          setMode("findloc");
      Intent grabberintent = new Intent(CourseSearch.this, JsonGrabber.class);
      grabberintent.putExtra("city", citySearch);
      grabberintent.putExtra("state", stateSearch);
      grabberintent.putExtra("country", countrySearch);
      grabberintent.putExtra("mode", mode);
      this.startActivity(grabberintent);
  }
  
  public String getCitySearch() {
      return citySearch;
  }
  
  public String getStateSearch() {
      return stateSearch;
  }
  
  public String getCountrySearch() {
      return countrySearch;
  }
  
  public void setMode(String m) {
      mode = m;
  }
  
  public String getMode() {
      return mode;
  }
  
  class onCountrySelectedListener implements AdapterView.OnItemSelectedListener {
  
      public void onItemSelected(AdapterView&lt;?&gt; parent,
                                 View view, int pos, long id) {
          Toast.makeText(parent.getContext(), "The Country is " +
                  parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
          countrySearch = parent.getItemAtPosition(pos).toString();
      }
  
      public void onNothingSelected(AdapterView parent) {
          // Do nothing.
      }
  }
  
  class onStateSelectedListener implements AdapterView.OnItemSelectedListener {
      @Override
      public void onItemSelected(AdapterView&lt;?&gt; parent,
                                 View view, int pos, long id) {
          Toast.makeText(parent.getContext(), "The State is " +
                  parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
          stateSearch = parent.getItemAtPosition(pos).toString();
      }
  
      public void onNothingSelected(AdapterView parent) {
          // Do nothing.
      }
  }
  
  }
  </code></pre>
  
  <p>Here is the xml code 
      
      
  
  <pre><code>    &gt;
  
  &lt;LinearLayout
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="48dp"
  
          android:background="@drawable/header_bg_hdpi"
          android:tileMode="repeat"
          android:gravity="center_vertical"
  
          &gt;
      &lt;TextView
              android:text="@string/title_search"
              style="@style/TitleText"
              android:gravity="left"
              android:layout_weight="1"
              &gt;
      &lt;/TextView&gt;
  
  
      &lt;ImageView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/dgcr_logo"
              android:gravity="right"
  
              android:layout_weight="1"
  
  
              /&gt;
  
  &lt;/LinearLayout&gt;
  
  
  
  &lt;TextView
          android:id="@+id/searchTitle"
          android:text="Search for Course By Location"
          android:layout_height="wrap_content"
          android:layout_width="fill_parent"/&gt;
  &lt;EditText
          android:id="@+id/cityName"
          android:autoText="true"
          android:layout_height="wrap_content"
          android:layout_width="fill_parent"
          android:inputType="textAutoComplete"
          android:hint="City Name"&gt;
  
  
  &lt;/EditText&gt;
  
  &lt;TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="10dip"
          android:text="@string/states_prompt"
          /&gt;
  &lt;Spinner
          android:id="@+id/state"
          android:layout_height="wrap_content"
          android:layout_width="fill_parent"
          android:prompt="@string/states_prompt"
  
          &gt;
  &lt;/Spinner&gt;
  &lt;TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginTop="10dip"
          android:text="@string/countries_prompt"
          /&gt;
  &lt;Spinner
          android:id="@+id/country"
          android:layout_height="wrap_content"
          android:layout_width="fill_parent"
          android:prompt="@string/countries_prompt"
  
          &gt;
  &lt;/Spinner&gt;
  /&gt;
  &lt;LinearLayout
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"&gt;
      &lt;Button
              android:text="Search Address"
              android:id="@+id/search"
              android:background="@drawable/orange_gradient_button"
              style="@style/ButtonText"
              android:onClick="searchByLoc"
          android:layout_weight="1"&gt;
      &lt;/Button&gt;
  
      &lt;Button
              android:text="Search Map"
              android:id="@+id/button2"
              android:background="@drawable/orange_gradient_button"
              style="@style/ButtonText"
          android:layout_weight="1"&gt;
      &lt;/Button&gt;
  &lt;/LinearLayout&gt;
  &lt;ImageView
          android:gravity=""
          android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:layout_alignParentBottom="true"
          android:src="@drawable/footer_hdpi"&gt;
  &lt;/ImageView&gt;
  
  
  
  &lt;/LinearLayout&gt;
  </code></pre>