Skip to content

Most visited

Recently visited

navigation
SpeedTracker / Wearable / src / com.example.android.wearable.speedtracker / ui /

SpeedPickerListAdapter.java

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
 
38
    public SpeedPickerListAdapter(Context context, int[] dataset) {
39
        mContext = context;
40
        mInflater = LayoutInflater.from(context);
41
        mDataSet = dataset;
42
    }
43
 
44
    /**
45
     * Displays all possible speed limit choices.
46
     */
47
    public static class ItemViewHolder extends WearableListView.ViewHolder {
48
 
49
        private TextView mTextView;
50
 
51
        public ItemViewHolder(View itemView) {
52
            super(itemView);
53
            // find the text view within the custom item's layout
54
            mTextView = (TextView) itemView.findViewById(R.id.name);
55
        }
56
    }
57
 
58
    /**
59
     * Create new views for list items (invoked by the WearableListView's layout manager)
60
     */
61
    @Override
62
    public WearableListView.ViewHolder onCreateViewHolder(ViewGroup parent,
63
            int viewType) {
64
        // Inflate our custom layout for list items
65
        return new ItemViewHolder(mInflater.inflate(R.layout.speed_picker_item_layout, null));
66
    }
67
 
68
    /**
69
     * Replaces the contents of a list item. Instead of creating new views, the list tries to
70
     * recycle existing ones. This is invoked by the WearableListView's layout manager.
71
     */
72
    @Override
73
    public void onBindViewHolder(WearableListView.ViewHolder holder,
74
            int position) {
75
        // retrieve the text view
76
        ItemViewHolder itemHolder = (ItemViewHolder) holder;
77
        TextView view = itemHolder.mTextView;
78
        // replace text contents
79
        view.setText(mContext.getString(R.string.speed_for_list, mDataSet[position]));
80
        // replace list item's metadata
81
        holder.itemView.setTag(position);
82
    }
83
 
84
    /**
85
     * Return the size of the data set (invoked by the WearableListView's layout manager).
86
     */
87
    @Override
88
    public int getItemCount() {
89
        return mDataSet.length;
90
    }
91
 
92
}
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)