1
/*
2
* Copyright 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.adaptertransition;
18
19
import android.content.Context;
20
import android.view.LayoutInflater;
21
import android.view.View;
22
import android.view.ViewGroup;
23
import android.widget.AbsListView;
24
import android.widget.AdapterView;
25
import android.widget.BaseAdapter;
26
import android.widget.ImageView;
27
import android.widget.TextView;
28
import android.widget.Toast;
29
30
/**
31
* This class provides data as Views. It is designed to support both ListView and GridView by
32
* changing a layout resource file to inflate.
33
*/
34
public class MeatAdapter extends BaseAdapter implements AbsListView.OnItemClickListener {
35
36
private final LayoutInflater mLayoutInflater;
37
private final int mResourceId;
38
39
/**
40
* Create a new instance of {@link MeatAdapter}.
41
*
42
* @param inflater The layout inflater.
43
* @param resourceId The resource ID for the layout to be used. The layout should contain an
44
* ImageView with ID of "meat_image" and a TextView with ID of "meat_title".
45
*/
46
public MeatAdapter(LayoutInflater inflater, int resourceId) {
47
mLayoutInflater = inflater;
48
mResourceId = resourceId;
49
}
50
51
@Override
52
public int getCount() {
53
return Meat.MEATS.length;
54
}
55
56
@Override
57
public Meat getItem(int position) {
58
return Meat.MEATS[position];
59
}
60
61
@Override
62
public long getItemId(int position) {
63
return Meat.MEATS[position].resourceId;
64
}
65
66
@Override
67
public View getView(int position, View convertView, ViewGroup parent) {
68
final View view;
69
final ViewHolder holder;
70
if (null == convertView) {
71
view = mLayoutInflater.inflate(mResourceId, parent, false);
72
holder = new ViewHolder();
73
assert view != null;
74
holder.image = (ImageView) view.findViewById(R.id.meat_image);
75
holder.title = (TextView) view.findViewById(R.id.meat_title);
76
view.setTag(holder);
77
} else {
78
view = convertView;
79
holder = (ViewHolder) view.getTag();
80
}
81
Meat meat = getItem(position);
82
holder.image.setImageResource(meat.resourceId);
83
holder.title.setText(meat.title);
84
return view;
85
}
86
87
@Override
88
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
89
ViewHolder holder = (ViewHolder) view.getTag();
90
Context context = view.getContext();
91
if (null != holder && null != holder.title && null != context) {
92
Toast.makeText(context, context.getString(R.string.item_clicked,
93
holder.title.getText()), Toast.LENGTH_SHORT).show();
94
}
95
}
96
97
private static class ViewHolder {
98
public ImageView image;
99
public TextView title;
100
}
101
102
}