com.licenta.android.licenseapp.contacts.GuardiansAdapter.java Source code

Java tutorial

Introduction

Here is the source code for com.licenta.android.licenseapp.contacts.GuardiansAdapter.java

Source

package com.licenta.android.licenseapp.contacts;

import android.app.Fragment;
import android.content.Context;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.facebook.login.widget.ProfilePictureView;
import com.licenta.android.licenseapp.R;
import com.licenta.android.licenseapp.model.Contact;

import io.realm.Realm;
import io.realm.RealmBaseAdapter;
import io.realm.RealmResults;

/**
 * Created by oana.ilovan on 05.03.2016.
 */
public class GuardiansAdapter extends RealmBaseAdapter<Contact> implements ListAdapter {

    private Realm realm;
    private ListFragment fragment;

    public GuardiansAdapter(Context context, ListFragment fragment, Realm realm, RealmResults<Contact> realmResults,
            boolean automaticUpdate) {
        super(context, realmResults, automaticUpdate);

        this.realm = realm;
        this.fragment = fragment;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_contact, parent, false);
            viewHolder = new ViewHolder();

            viewHolder.clickableArea = (LinearLayout) convertView.findViewById(R.id.clickable_area);
            viewHolder.contactIcon = (ImageView) convertView.findViewById(R.id.contact_icon);
            viewHolder.contactIconFb = (ProfilePictureView) convertView.findViewById(R.id.fb_contact_icon);
            viewHolder.contactName = (TextView) convertView.findViewById(R.id.contact_name);
            viewHolder.contactPhoneNo = (TextView) convertView.findViewById(R.id.contact_phone_no);
            viewHolder.contactEmail = (TextView) convertView.findViewById(R.id.contact_email);
            viewHolder.contactIsSelected = (CheckBox) convertView.findViewById(R.id.contact_is_selected);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final Contact item = realmResults.get(position);

        //String userId = "111034575957111";//"928327317237123";
        if (item.isFacebook()) {
            viewHolder.contactIconFb.setProfileId(item.getId());
            viewHolder.contactIcon.setVisibility(View.GONE);
        } else {
            viewHolder.contactIconFb.setVisibility(View.GONE);
        }
        viewHolder.contactName.setText(item.getName());
        viewHolder.contactPhoneNo.setText(item.getPhoneNumber());
        viewHolder.contactEmail.setText(item.getEmail());

        viewHolder.clickableArea.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (fragment instanceof GuardiansFragment) {
                    ((GuardiansFragment) fragment).onListItemClick(fragment.getListView(), null, position, 0);
                }
            }
        });

        viewHolder.contactIsSelected.setChecked(item.isActive());
        viewHolder.contactIsSelected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                realm.beginTransaction();
                item.setActive(viewHolder.contactIsSelected.isChecked());
                realm.commitTransaction();
            }
        });

        return convertView;
    }

    public RealmResults<Contact> getRealmResults() {
        return realmResults;
    }

    public static class ViewHolder {
        LinearLayout clickableArea;
        ImageView contactIcon;
        ProfilePictureView contactIconFb;
        TextView contactName;
        TextView contactPhoneNo;
        TextView contactEmail;
        CheckBox contactIsSelected;
    }
}