Skip to content

Most visited

Recently visited

navigation
RecipeAssistant / Application / src / com.example.android.wearable.recipeassistant /

RecipeListAdapter.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.wearable.recipeassistant;
18
 
19
import android.content.Context;
20
import android.database.DataSetObserver;
21
import android.graphics.Bitmap;
22
import android.util.Log;
23
import android.view.LayoutInflater;
24
import android.view.View;
25
import android.view.ViewGroup;
26
import android.widget.ImageView;
27
import android.widget.ListAdapter;
28
import android.widget.TextView;
29
 
30
import org.json.JSONArray;
31
import org.json.JSONException;
32
import org.json.JSONObject;
33
 
34
import java.util.ArrayList;
35
import java.util.List;
36
 
37
public class RecipeListAdapter implements ListAdapter {
38
    private String TAG = "RecipeListAdapter";
39
 
40
    private class Item {
41
        String title;
42
        String name;
43
        String summary;
44
        Bitmap image;
45
    }
46
 
47
    private List<Item> mItems = new ArrayList<Item>();
48
    private Context mContext;
49
    private DataSetObserver mObserver;
50
 
51
    public RecipeListAdapter(Context context) {
52
        mContext = context;
53
        loadRecipeList();
54
    }
55
 
56
    private void loadRecipeList() {
57
        JSONObject jsonObject = AssetUtils.loadJSONAsset(mContext, Constants.RECIPE_LIST_FILE);
58
        if (jsonObject != null) {
59
            List<Item> items = parseJson(jsonObject);
60
            appendItemsToList(items);
61
        }
62
    }
63
 
64
    private List<Item> parseJson(JSONObject json) {
65
        List<Item> result = new ArrayList<Item>();
66
        try {
67
            JSONArray items = json.getJSONArray(Constants.RECIPE_FIELD_LIST);
68
            for (int i = 0; i < items.length(); i++) {
69
                JSONObject item = items.getJSONObject(i);
70
                Item parsed = new Item();
71
                parsed.name = item.getString(Constants.RECIPE_FIELD_NAME);
72
                parsed.title = item.getString(Constants.RECIPE_FIELD_TITLE);
73
                if (item.has(Constants.RECIPE_FIELD_IMAGE)) {
74
                    String imageFile = item.getString(Constants.RECIPE_FIELD_IMAGE);
75
                    parsed.image = AssetUtils.loadBitmapAsset(mContext, imageFile);
76
                }
77
                parsed.summary = item.getString(Constants.RECIPE_FIELD_SUMMARY);
78
                result.add(parsed);
79
            }
80
        } catch (JSONException e) {
81
            Log.e(TAG, "Failed to parse recipe list: " + e);
82
        }
83
        return result;
84
    }
85
 
86
    private void appendItemsToList(List<Item> items) {
87
        mItems.addAll(items);
88
        if (mObserver != null) {
89
            mObserver.onChanged();
90
        }
91
    }
92
 
93
    @Override
94
    public int getCount() {
95
        return mItems.size();
96
    }
97
 
98
    @Override
99
    public Object getItem(int position) {
100
        return mItems.get(position);
101
    }
102
 
103
    @Override
104
    public long getItemId(int position) {
105
        return 0;
106
    }
107
 
108
    @Override
109
    public int getItemViewType(int position) {
110
        return 0;
111
    }
112
 
113
    @Override
114
    public View getView(int position, View convertView, ViewGroup parent) {
115
        View view = convertView;
116
        if (view == null) {
117
            LayoutInflater inf = LayoutInflater.from(mContext);
118
            view = inf.inflate(R.layout.list_item, null);
119
        }
120
        Item item = (Item) getItem(position);
121
        TextView titleView = (TextView) view.findViewById(R.id.textTitle);
122
        TextView summaryView = (TextView) view.findViewById(R.id.textSummary);
123
        ImageView iv = (ImageView) view.findViewById(R.id.imageView);
124
 
125
        titleView.setText(item.title);
126
        summaryView.setText(item.summary);
127
        if (item.image != null) {
128
            iv.setImageBitmap(item.image);
129
        } else {
130
            iv.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_noimage));
131
        }
132
        return view;
133
    }
134
 
135
    @Override
136
    public int getViewTypeCount() {
137
        return 1;
138
    }
139
 
140
    @Override
141
    public boolean hasStableIds() {
142
        return false;
143
    }
144
 
145
    @Override
146
    public boolean isEmpty() {
147
        return mItems.isEmpty();
148
    }
149
 
150
    @Override
151
    public void registerDataSetObserver(DataSetObserver observer) {
152
        mObserver = observer;
153
    }
154
 
155
    @Override
156
    public void unregisterDataSetObserver(DataSetObserver observer) {
157
        mObserver = null;
158
    }
159
 
160
    @Override
161
    public boolean areAllItemsEnabled() {
162
        return true;
163
    }
164
 
165
    @Override
166
    public boolean isEnabled(int position) {
167
        return true;
168
    }
169
 
170
    public String getItemName(int position) {
171
        return mItems.get(position).name;
172
    }
173
}
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

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.