com.klinker.android.twitter.adapters.CategoriesArrayAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.klinker.android.twitter.adapters.CategoriesArrayAdapter.java

Source

/*
 * Copyright 2013 Luke Klinker
 *
 * 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 com.klinker.android.twitter.adapters;

import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.klinker.android.twitter.R;
import com.klinker.android.twitter.settings.AppSettings;
import com.klinker.android.twitter.ui.drawer_activities.discover.people.PeopleSearch;
import com.klinker.android.twitter.ui.drawer_activities.discover.trends.SearchedTrendsActivity;

import java.util.ArrayList;

import twitter4j.Category;
import twitter4j.ResponseList;
import twitter4j.User;

public class CategoriesArrayAdapter extends ArrayAdapter<User> {

    protected Context context;

    private ResponseList<Category> categories;

    private LayoutInflater inflater;
    private AppSettings settings;

    public static class ViewHolder {
        public TextView text;
    }

    public CategoriesArrayAdapter(Context context, ResponseList<Category> categories) {
        super(context, R.layout.tweet);

        this.context = context;
        this.categories = categories;

        settings = AppSettings.getInstance(context);
        inflater = LayoutInflater.from(context);

    }

    @Override
    public int getCount() {
        try {
            return categories.size();
        } catch (Exception e) {
            return 0;
        }
    }

    public View newView(ViewGroup viewGroup) {
        View v;
        final ViewHolder holder;

        v = inflater.inflate(R.layout.text, viewGroup, false);

        holder = new ViewHolder();

        holder.text = (TextView) v.findViewById(R.id.text);

        // sets up the font sizes
        holder.text.setTextSize(24);

        v.setTag(holder);
        return v;
    }

    public void bindView(final View view, Context mContext, final Category category) {
        final ViewHolder holder = (ViewHolder) view.getTag();

        holder.text.setText(category.getName());

        holder.text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent search = new Intent(context, PeopleSearch.class);
                search.putExtra("slug", category.getSlug());
                context.startActivity(search);
            }
        });

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v;
        if (convertView == null) {

            v = newView(parent);

        } else {
            v = convertView;

            final ViewHolder holder = (ViewHolder) v.getTag();
        }

        bindView(v, context, categories.get(position));

        return v;
    }
}