sx.tfr.openraid.Adapters.EventListAdapter.java Source code

Java tutorial

Introduction

Here is the source code for sx.tfr.openraid.Adapters.EventListAdapter.java

Source

/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Thomas F. Roseman
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package sx.tfr.openraid.Adapters;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;

import sx.tfr.openraid.EventObject.Event;
import sx.tfr.openraid.R;

public class EventListAdapter extends BaseAdapter {

    Gson gson;
    Context context;
    ArrayList<Event> eventArrayList;
    private static LayoutInflater inflater = null;

    /**
     * @param context   Frame of reference for the last activity
     * @param events    An arraylist of single fully populated events
     */
    public EventListAdapter(Context context, ArrayList<Event> events) {
        this.context = context;
        this.eventArrayList = events;

        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    /**
     * @return Lenght of the json object
     */
    @Override
    public int getCount() {
        //Return length of json object
        return eventArrayList.size();
    }

    /**
     * @param position The position of the selected object as an int
     *
     * @return Json object at the requested position. Has several options: the single eventObject,
     * and error for json exception or a complete fail
     */
    @Override
    public Object getItem(int position) {
        return eventArrayList.get(position);
    }

    /**
     * @param position The position of the selected object as an int
     *
     * @return ID of the expected object in a long
     */
    @Override
    public long getItemId(int position) {
        return Long.parseLong(eventArrayList.get(position).getEvent_id());
    }

    /**
     * @param position    You guessed it. Requested position
     * @param convertView The actual list item
     * @param parent      Parent Object
     *
     * @return
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (vi == null)
            vi = inflater.inflate(R.layout.event_item, null);

        TextView eventTitle = (TextView) vi.findViewById(R.id.eventName);
        TextView eventDate = (TextView) vi.findViewById(R.id.eventDate);
        TextView eventFaction = (TextView) vi.findViewById(R.id.eventFaction);
        TextView eventType = (TextView) vi.findViewById(R.id.eventType);
        ImageView eventZone = (ImageView) vi.findViewById(R.id.eventImage);
        ImageView factionImage = (ImageView) vi.findViewById(R.id.factionImage);

        eventTitle.setText(eventArrayList.get(position).getName());
        eventDate.setText(eventArrayList.get(position).getLocal_start());
        eventFaction.setText(eventArrayList.get(position).getFaction());
        eventType.setText(eventArrayList.get(position).getCrowd());

        if (eventArrayList.get(position).getFaction().equals("horde")) {
            factionImage.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.horde));
        } else {
            factionImage.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.alliance));
        }

        switch (eventArrayList.get(position).getLocation()) {
        case "Temple of Ahn'Qiraj":
            eventZone.setImageBitmap(
                    BitmapFactory.decodeResource(context.getResources(), R.drawable.classic_aq40_round));
            break;
        case "Blackwing Lair":
            eventZone.setImageBitmap(
                    BitmapFactory.decodeResource(context.getResources(), R.drawable.classic_bwl_round));
            break;
        case "Molten Core":
            eventZone.setImageBitmap(
                    BitmapFactory.decodeResource(context.getResources(), R.drawable.classic_mc_round));
            break;
        case "Ruins of Ahn'Qiraj":
            eventZone.setImageBitmap(
                    BitmapFactory.decodeResource(context.getResources(), R.drawable.classic_aq40_round));
            break;

        case "Black Temple":
            eventZone.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.bc_bt_round));
            break;
        case "Gruul's Lair":
            eventZone.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.bc_gru_round));
            break;
        case "Karazhan":
            eventZone.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.bc_kar_round));
            break;
        case "Hellfire Citadel: Magtheridon's Lair":
            eventZone.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.bc_mag_round));
            break;
        case "Cavers of Time: Hyjal Summit":
            eventZone.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.bc_mh_round));
            break;
        case "Coilfang Reservoir: Serpentshrine Cavern":
            eventZone.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.bc_ssc_round));
            break;
        case "Sunwell Plateau":
            eventZone.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.bc_swp_round));
            break;
        case "Tempest Key: The Eye":
            eventZone.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.bc_tk_round));
            break;

        case "The Nexus: The Eye of Eternity":
            eventZone.setImageBitmap(
                    BitmapFactory.decodeResource(context.getResources(), R.drawable.wolk_eoe_round));
            break;
        case "Icecrown Citadel":
            eventZone.setImageBitmap(
                    BitmapFactory.decodeResource(context.getResources(), R.drawable.wolk_icc_round));
            break;
        case "Naxxramas":
            eventZone.setImageBitmap(
                    BitmapFactory.decodeResource(context.getResources(), R.drawable.wolk_naxx_round));
            break;
        case "Onyxia's Lair":
            eventZone.setImageBitmap(
                    BitmapFactory.decodeResource(context.getResources(), R.drawable.wolk_ony_round));
            break;
        case "Wyrmrest Temple: The Obsidian Sanctum":
            eventZone
                    .setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.wolk_os_round));
            break;
        case "Wyrmrest Temple: The Ruby Sanctum":
            eventZone
                    .setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.wolk_rs_round));
            break;
        case "Crusaders' Coliseum: Trial of the Crusader":
            eventZone.setImageBitmap(
                    BitmapFactory.decodeResource(context.getResources(), R.drawable.wolk_toc_round));
            break;
        case "Ulduar":
            eventZone.setImageBitmap(
                    BitmapFactory.decodeResource(context.getResources(), R.drawable.wolk_uld_round));
            break;
        case "Vault of Archavon":
            eventZone.setImageBitmap(
                    BitmapFactory.decodeResource(context.getResources(), R.drawable.wolk_voa_round));
            break;

        case "Blackwing Descent":
            eventZone
                    .setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.cata_bd_round));
            break;
        case "Baradin Hold":
            eventZone
                    .setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.cata_bh_round));
            break;
        case "The Bastion of Twilight":
            eventZone.setImageBitmap(
                    BitmapFactory.decodeResource(context.getResources(), R.drawable.cata_bot_round));
            break;
        case "Dragon Soul":
            eventZone
                    .setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.cata_ds_round));
            break;
        case "Firelands":
            eventZone
                    .setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.cata_fl_round));
            break;
        case "Throne of the Four Winds":
            eventZone.setImageBitmap(
                    BitmapFactory.decodeResource(context.getResources(), R.drawable.cata_tfw_round));
            break;

        case "Heart of Fear":
            eventZone.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.mp_hf_round));
            break;
        case "Mogu'shan Vaults":
            eventZone.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.mp_mv_round));
            break;
        case "Siege of Orgrimmar":
            eventZone.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.mp_soo_round));
            break;
        case "Terrace of Endless Spring":
            eventZone.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.mp_tes_round));
            break;
        case "Throne of Thunder":
            eventZone.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.mp_tot_round));
            break;

        case "Blackrock Foundry":
            eventZone.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.wod_bf_round));
            break;
        case "Highmaul":
            eventZone.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.wod_hm_round));
            break;

        default:
            eventZone.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.unknown));
            break;
        }

        //Populate the items as needed
        return vi;
    }

    public void populate() {

    }
}