Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Array Adapters
- // Now let's add some data and a default adapter to adapt the data for the list view.
- // All right. Wait, adapter?
- // What am I talking about?
- // 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?
- // A common pattern in Android is to use a special component called an Adapter to adapt data for the view.
- // We set the Data for the Adapter and it makes the data look nice and puts it in the List View.
- // This is another variation on the MVC pattern!
- //Android provides a number of different adapters we can use as list adapters.
- // Lets take a look at one called ArrayAdapter that's used to well,[laughs] adapt data from arrays.
- // Documentation page: http://developer.android.com/reference/android/widget/ArrayAdapter.html
- package com.teamtreehouse.stormy.UI;
- import android.app.ListActivity;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import com.teamtreehouse.stormy.R;
- public class DailyForecastActivity extends ListActivity { //change 'ActionBarActivity' to 'ListActivity'
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_daily_forecast);
- // So our real data is going to be an array of dat objects with details about the weather
- // 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
- // This will illustrate how the main components of a ListView work.
- // And then we can move onto more advanced uses.
- String[] daysOfTheWeek = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
- //use the array adapter:
- 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
- //^ for the parameters: (context, sampleLayout_resource_id, Array we want to adapt);
- // All we have left to do is set this new adapter as the adapter for our List View.
- // Set adapter as the default adapter for DailyForecastActivity using the setListAdapter() method
- setListAdapter(adapter); //so once again, all the hard work is taken care for us when we use the Android system component for list
- }
- }
- // https://teamtreehouse.com/library/android-lists-and-adapters/standard-listviews/using-a-default-adapter
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement