MedocAdapterMenu.java 2.96 KB
package com.example.martin.projetv5;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by martin on 12/04/2017.
 */

public class MedocAdapterMenu extends BaseAdapter {
    public List<Medicament> mListM;
    public Context mContext;
    public LayoutInflater mInflater;

    public MedocAdapterMenu(Context context, List<Medicament> aListM){
        mContext = context;
        mListM = aListM;
        mInflater = LayoutInflater.from(mContext);
    }

    public int getCount(){
        return mListM.size();
    }

    public Object getItem(int position){
        return mListM.get(position);
    }

    public long getItemId(int position){
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        RelativeLayout layoutItem;

        if (convertView == null) {
            layoutItem = (RelativeLayout) mInflater.inflate(R.layout.medoc, parent, false);
        } else {
            layoutItem = (RelativeLayout) convertView;
        }

        TextView nom1 = (TextView)layoutItem.findViewById(R.id.nom1);
        TextView nom2 = (TextView)layoutItem.findViewById(R.id.nom2);
        ImageView logo = (ImageView)layoutItem.findViewById(R.id.logo);
        ImageButton modif = (ImageButton) layoutItem.findViewById(R.id.boutonModif);

        nom1.setText(mListM.get(position).getNom1());
        nom2.setText(mListM.get(position).getNom2());

        File imgFile = new  File(mListM.get(position).getLogo());
        if(imgFile.exists()){
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            logo.setImageBitmap(myBitmap);
        }

        modif.setTag(position);
        modif.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                Integer position = (Integer)v.getTag();
                sendListener(mListM.get(position),position);
            }
        });

        return layoutItem;
    }

    public interface MedocAdapterListener{
        void onClickButton(Medicament item, int position);
    }

    private ArrayList<MedocAdapterMenu.MedocAdapterListener> mListListener = new ArrayList<MedocAdapterListener>();

    public void addListener(MedocAdapterMenu.MedocAdapterListener aListener){
        mListListener.add(aListener);
    }

    private void sendListener(Medicament item, int position){
        for(int i = mListListener.size()-1; i>=0; i--){
            mListListener.get(i).onClickButton(item, position);
        }
    }
}