/* * Copyright 2015 sourcestream GmbH * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package de.sourcestream.movieDB.adapter; import de.sourcestream.movieDB.model.GenresModel; import de.sourcestream.movieDB.R; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; import java.util.ArrayList; /** * Genres adapter. Used to load genres information in the genres list. */ public class GenresAdapter extends ArrayAdapter<GenresModel> { private ArrayList<GenresModel> genresList; private LayoutInflater vi; private int Resource; private ViewHolder holder; public GenresAdapter(Context context, int resource, ArrayList<GenresModel> objects) { super(context, resource, objects); vi = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); Resource = resource; genresList = objects; } /** * Get a View that displays the data at the specified position in the data set. * * @param position The position of the item within the adapter's data set of the item whose view we want. * @param convertView The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. * @param parent The parent that this view will eventually be attached to. * @return A View corresponding to the data at the specified position. */ @Override public View getView(int position, View convertView, ViewGroup parent) { // convert view = design View v = convertView; if (v == null) { holder = new ViewHolder(); v = vi.inflate(Resource, null); holder.name = (TextView) v.findViewById(R.id.name); v.setTag(holder); } else { holder = (ViewHolder) v.getTag(); } holder.name.setText(genresList.get(position).getName()); return v; } /** * Defines genres list row elements. */ static class ViewHolder { public TextView name; } }