com.android.projectz.teamrocket.thebusapp.adapters.CustomListSettingMain.java Source code

Java tutorial

Introduction

Here is the source code for com.android.projectz.teamrocket.thebusapp.adapters.CustomListSettingMain.java

Source

package com.android.projectz.teamrocket.thebusapp.adapters;
/*
 Copyright (C) 2016-2017  TeamRocket
    
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.projectz.teamrocket.thebusapp.R;
import com.android.projectz.teamrocket.thebusapp.settings.SettingGeneralActivity;
import com.android.projectz.teamrocket.thebusapp.util.SharedPreferencesUtils;

/**
 * CustomListSettingMain:
 * questa classe permette una customizzazione della classica lista offerta da android
 * attraverso delle modifiche alla classe adapter.
 * <p>
 * Created by simone98dm on 08/12/16.
 */

public class CustomListSettingMain extends ArrayAdapter<String> {
    private final Activity context;
    private final String[] text;
    private final Drawable[] imageId;
    private final Resources.Theme theme;

    /**
     * costruttore con i parametri necessari
     *
     * @param context
     * @param web
     * @param imageId
     */
    public CustomListSettingMain(Activity context, String[] web, Drawable[] imageId) {
        super(context, R.layout.list_main, web);
        this.context = context;
        this.text = web;
        this.imageId = imageId;
        theme = context.getTheme();
    }

    /**
     * adattatore per visualizzare la lista con i determinati parametri:
     * 1.testo
     * 2.immagine
     *
     * @param position
     * @param view
     * @param parent
     * @return
     */
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_main, null, true);

        TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
        txtTitle.setText(text[position]);

        Drawable colorImg = null;
        if (SharedPreferencesUtils.getSelectedTheme(context).equals("AppTheme")) {
            colorImg = changeColorOfIcons(imageId[position], R.color.iconLight);
        } else {
            colorImg = changeColorOfIcons(imageId[position], R.color.iconDark);
        }

        ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
        imageView.setImageDrawable(colorImg);

        return rowView;
    }

    public Drawable changeColorOfIcons(Drawable image, int color) {
        image = DrawableCompat.wrap(image);
        DrawableCompat.setTint(image, context.getResources().getColor(color));
        return image;
    }
}