Skip to content

Most visited

Recently visited

navigation
RecyclerView / src / com.example.android.recyclerview /

CustomAdapter.java

1
/*
2
* Copyright (C) 2014 The Android Open Source Project
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.recyclerview;
18
 
19
import com.example.android.common.logger.Log;
20
 
21
import android.support.v7.widget.RecyclerView;
22
import android.view.LayoutInflater;
23
import android.view.View;
24
import android.view.ViewGroup;
25
import android.widget.TextView;
26
 
27
/**
28
 * Provide views to RecyclerView with data from mDataSet.
29
 */
30
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
31
    private static final String TAG = "CustomAdapter";
32
 
33
    private String[] mDataSet;
34
 
36
    /**
37
     * Provide a reference to the type of views that you are using (custom ViewHolder)
38
     */
39
    public static class ViewHolder extends RecyclerView.ViewHolder {
40
        private final TextView textView;
41
 
42
        public ViewHolder(View v) {
43
            super(v);
44
            // Define click listener for the ViewHolder's View.
45
            v.setOnClickListener(new View.OnClickListener() {
46
                @Override
47
                public void onClick(View v) {
48
                    Log.d(TAG, "Element " + getAdapterPosition() + " clicked.");
49
                }
50
            });
51
            textView = (TextView) v.findViewById(R.id.textView);
52
        }
53
 
54
        public TextView getTextView() {
55
            return textView;
56
        }
57
    }
59
 
60
    /**
61
     * Initialize the dataset of the Adapter.
62
     *
63
     * @param dataSet String[] containing the data to populate views to be used by RecyclerView.
64
     */
65
    public CustomAdapter(String[] dataSet) {
66
        mDataSet = dataSet;
67
    }
68
 
70
    // Create new views (invoked by the layout manager)
71
    @Override
72
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
73
        // Create a new view.
74
        View v = LayoutInflater.from(viewGroup.getContext())
75
                .inflate(R.layout.text_row_item, viewGroup, false);
76
 
77
        return new ViewHolder(v);
78
    }
80
 
82
    // Replace the contents of a view (invoked by the layout manager)
83
    @Override
84
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
85
        Log.d(TAG, "Element " + position + " set.");
86
 
87
        // Get element from your dataset at this position and replace the contents of the view
88
        // with that element
89
        viewHolder.getTextView().setText(mDataSet[position]);
90
    }
92
 
93
    // Return the size of your dataset (invoked by the layout manager)
94
    @Override
95
    public int getItemCount() {
96
        return mDataSet.length;
97
    }
98
}
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 one-minute survey?
Help us improve Android tools and documentation.