• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 Google Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.wearable.speedtracker.ui;
18 
19 import android.content.Context;
20 import android.support.wearable.view.WearableListView;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.TextView;
25 
26 import com.example.android.wearable.speedtracker.R;
27 
28 /**
29  * A {@link android.support.wearable.view.WearableListView.Adapter} that is used to populate the
30  * list of speeds.
31  */
32 public class SpeedPickerListAdapter extends WearableListView.Adapter {
33 
34     private int[] mDataSet;
35     private final Context mContext;
36     private final LayoutInflater mInflater;
37 
SpeedPickerListAdapter(Context context, int[] dataset)38     public SpeedPickerListAdapter(Context context, int[] dataset) {
39         mContext = context;
40         mInflater = LayoutInflater.from(context);
41         mDataSet = dataset;
42     }
43 
44     public static class ItemViewHolder extends WearableListView.ViewHolder {
45 
46         private TextView mTextView;
47 
ItemViewHolder(View itemView)48         public ItemViewHolder(View itemView) {
49             super(itemView);
50             // find the text view within the custom item's layout
51             mTextView = (TextView) itemView.findViewById(R.id.name);
52         }
53     }
54 
55     /**
56      * Create new views for list items (invoked by the WearableListView's layout manager)
57      */
58     @Override
onCreateViewHolder(ViewGroup parent, int viewType)59     public WearableListView.ViewHolder onCreateViewHolder(ViewGroup parent,
60             int viewType) {
61         // Inflate our custom layout for list items
62         return new ItemViewHolder(mInflater.inflate(R.layout.speed_picker_item_layout, null));
63     }
64 
65     /**
66      * Replaces the contents of a list item. Instead of creating new views, the list tries to
67      * recycle existing ones. This is invoked by the WearableListView's layout manager.
68      */
69     @Override
onBindViewHolder(WearableListView.ViewHolder holder, int position)70     public void onBindViewHolder(WearableListView.ViewHolder holder,
71             int position) {
72         // retrieve the text view
73         ItemViewHolder itemHolder = (ItemViewHolder) holder;
74         TextView view = itemHolder.mTextView;
75         // replace text contents
76         view.setText(mContext.getString(R.string.speed_for_list, mDataSet[position]));
77         // replace list item's metadata
78         holder.itemView.setTag(position);
79     }
80 
81     /**
82      * Return the size of the data set (invoked by the WearableListView's layout manager).
83      */
84     @Override
getItemCount()85     public int getItemCount() {
86         return mDataSet.length;
87     }
88 
89 }
90