Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Creating a custom adapter
- // Right now the xml would look pretty good on most devices.
- // But we can't test it until we create a custom adapter that allows us to attach
- // our data to each item.
- // Now, instead of using a simple collection of strings,
- // we can use a collection of objects and adapt them for the new layout.
- // We do this by extending one of the base adapter classes from the Android SDK.
- // Extending an existing adaptor allows us to tap into certain functionality
- // that we don't want to build from the ground up.
- // Our new custom adapter will focus on how to fill the layout with
- // the appropriate data.
- // We're basically mapping from our model to our view.
- Pic:
- http://content.screencast.com/users/yofu1234/folders/Jing/media/aababbf1-d235-4a87-894e-c0f8270ebc55/Now,%20we%20will%20create%20the%20adapter%20that%20maps%20each%20piece%20of%20data%20from%20the%20Day%20class.png
- // Now, we will create the adapter that maps each piece of data from the Day class to a field in the layout.
- // Our adapter will be a customized version of the array adapter we are already using.
- // And how do we customize existing classes in Java?
- // We subclass them.
- //Let's start by creating a new package to hold our adapters.
- // Right-click on our main stormy package and select New > Package and type in adapters.
- // So let's start with a new file.
- // Right-click > New > Java Class.
- // And let's call this the DayAdapter and click Enter.
- // And the very first thing we want to do is extend one of the base adapters.
- // then Alt+Enter to implement methods and @Override.
- package com.teamtreehouse.stormy.adapters;
- import android.content.Context;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import com.teamtreehouse.stormy.weather.Day;
- /**
- * Created by Fu on 7/16/2015.
- */
- public class DayAdapter extends BaseAdapter {
- //Step 1. This adapter needs to know the context and the data its going to map:
- private Context mContext; //lets create Context and call it mContext
- private Day[] mDays; //and lets create a private Day array and call it mDays
- //Step 2. Now lets create a constructor that allows us to set these two values.
- public DayAdapter(Context context, Day[] days){
- mContext = context;
- mDays = days;
- }
- //Step 3. Next, move through these methods and implement them one by one
- @Override
- //getCount, gets us the count of items in the array that this adapter is using. So that is just the count of the mDays array.
- public int getCount() {
- return mDays.length; //so we can use the property: mDays.length
- }
- @Override
- //getItem gets the item for the adapter given a certain position.
- public Object getItem(int position) {
- return mDays[position]; // So we'll do mDays and the item at position. <- get data from the mDays array again like in the previous method.
- }
- @Override
- //we aren't going to use ItemId. This method is used to Tag items for easy reference.
- public long getItemId(int position) {
- return 0;
- }
- @Override
- //getView is where the mapping occurs.
- public View getView(int position, View convertView, ViewGroup parent) {
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement