Advertisement
yo2man

Creating a custom adapter

Jul 17th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. // Creating a custom adapter
  2.  
  3. // Right now the xml would look pretty good on most devices.
  4. // But we can't test it until we create a custom adapter that allows us to attach
  5. // our data to each item.
  6.  
  7. // Now, instead of using a simple collection of strings,
  8. // we can use a collection of objects and adapt them for the new layout.
  9. // We do this by extending one of the base adapter classes from the Android SDK.
  10. // Extending an existing adaptor allows us to tap into certain functionality
  11. // that we don't want to build from the ground up.
  12. // Our new custom adapter will focus on how to fill the layout with
  13. // the appropriate data.
  14. // We're basically mapping from our model to our view.
  15.  
  16. Pic:
  17. 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
  18. // Now, we will create the adapter that maps each piece of data from the Day class to a field in the layout.
  19.  
  20. // Our adapter will be a customized version of the array adapter we are already using.
  21. // And how do we customize existing classes in Java?
  22. // We subclass them.
  23.  
  24. //Let's start by creating a new package to hold our adapters.
  25. // Right-click on our main stormy package and select New > Package and type in adapters.
  26. // So let's start with a new file.
  27. // Right-click > New > Java Class.
  28. // And let's call this the DayAdapter and click Enter.
  29. // And the very first thing we want to do is extend one of the base adapters.
  30. // then Alt+Enter to implement methods and @Override.
  31.  
  32. package com.teamtreehouse.stormy.adapters;
  33.  
  34. import android.content.Context;
  35. import android.view.View;
  36. import android.view.ViewGroup;
  37. import android.widget.BaseAdapter;
  38.  
  39. import com.teamtreehouse.stormy.weather.Day;
  40.  
  41. /**
  42.  * Created by Fu on 7/16/2015.
  43.  */
  44. public class DayAdapter extends BaseAdapter {
  45.  
  46.     //Step 1. This adapter needs to know the context and the data its going to map:
  47.     private Context mContext; //lets create Context and call it mContext
  48.     private Day[] mDays;  //and lets create a private Day array and call it mDays
  49.  
  50.     //Step 2. Now lets create a constructor that allows us to set these two values.
  51.     public DayAdapter(Context context, Day[] days){
  52.         mContext = context;
  53.         mDays = days;
  54.     }
  55.  
  56.     //Step 3. Next, move through these methods and implement them one by one
  57.  
  58.     @Override
  59.     //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.
  60.     public int getCount() {
  61.         return mDays.length; //so we can use the property: mDays.length
  62.     }
  63.  
  64.     @Override
  65.     //getItem gets the item for the adapter given a certain position.
  66.     public Object getItem(int position) {
  67.         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.
  68.     }
  69.  
  70.     @Override
  71.     //we aren't going to use ItemId. This method is used to Tag items for easy reference.
  72.     public long getItemId(int position) {
  73.         return 0;
  74.     }
  75.  
  76.     @Override
  77.     //getView is where the mapping occurs.
  78.     public View getView(int position, View convertView, ViewGroup parent) {
  79.         return null;
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement