Advertisement
yo2man

ArrayAdapters

Jul 13th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. // Array Adapters
  2. // Now let's add some data and a default adapter to adapt the data for the list view.
  3. // All right. Wait, adapter?
  4. // What am I talking about?
  5. // So we have data, our Array of days of the week and we have a List View. What do we need to display the Data in the List View?
  6. // A common pattern in Android is to use a special component called an Adapter to adapt data for the view.
  7. // We set the Data for the Adapter and it makes the data look nice  and puts it in the List View.
  8. // This is another variation on the MVC pattern!
  9.  
  10. //Android provides a number of different adapters we can use as list adapters.
  11. // Lets take a look at one called ArrayAdapter that's used to well,[laughs] adapt data from arrays.
  12. // Documentation page: http://developer.android.com/reference/android/widget/ArrayAdapter.html
  13.  
  14. package com.teamtreehouse.stormy.UI;
  15.  
  16. import android.app.ListActivity;
  17. import android.support.v7.app.ActionBarActivity;
  18. import android.os.Bundle;
  19. import android.view.Menu;
  20. import android.view.MenuItem;
  21. import android.view.View;
  22. import android.widget.ArrayAdapter;
  23.  
  24. import com.teamtreehouse.stormy.R;
  25.  
  26. public class DailyForecastActivity extends ListActivity { //change 'ActionBarActivity' to 'ListActivity'
  27.  
  28.     @Override
  29.     protected void onCreate(Bundle savedInstanceState) {
  30.         super.onCreate(savedInstanceState);
  31.         setContentView(R.layout.activity_daily_forecast);
  32.  
  33.         // So our real data is going to be an array of dat objects with details about the weather
  34.         // but it takes a little more work so lets start with a mockup with an array of string where each item is simply a day of the week
  35.         // This will illustrate how the main components of a ListView work.
  36.         // And then we can move onto more advanced uses.
  37.         String[] daysOfTheWeek = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
  38.         //use the array adapter:
  39.         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, daysOfTheWeek);  //ArrayAdapter is a Generic type, it can deal with all types of data, so we put the type of data we want it to deal with in the '< >' . In this example: String datatype
  40.                 //^ for the parameters: (context, sampleLayout_resource_id, Array we want to adapt);
  41.  
  42.         // All we have left to do is set this new adapter as the adapter for our List View.
  43.         // Set adapter as the default adapter for DailyForecastActivity using the setListAdapter() method
  44.         setListAdapter(adapter);  //so once again, all the hard work is taken care for us when we use the Android system component for list
  45.     }
  46. }
  47.  
  48. // https://teamtreehouse.com/library/android-lists-and-adapters/standard-listviews/using-a-default-adapter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement