Created
November 14, 2014 05:33
-
-
Save ryanmr/da6db36319668121e908 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ifupdown.odin.adapters; | |
import android.content.Context; | |
import android.support.v7.widget.RecyclerView; | |
import android.util.Log; | |
import android.util.SparseBooleanArray; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.CheckBox; | |
import android.widget.CompoundButton; | |
import android.widget.TextView; | |
import com.ifupdown.odin.R; | |
import com.ifupdown.odin.models.Contact; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
/** | |
* Created by ryanrampersad on 11/13/14. | |
*/ | |
public class ContactsListAdapter extends RecyclerView.Adapter<ContactsListAdapter.ViewHolder> { | |
public static final String TAG = "ContactsListAdapter"; | |
public class ViewHolder extends RecyclerView.ViewHolder { | |
int index; | |
TextView mTextViewName; | |
CheckBox mCheckBox; | |
String mName; | |
public ViewHolder(View v) { | |
super(v); | |
mTextViewName = (TextView)v.findViewById(R.id.contact_list_item_name); | |
mCheckBox = (CheckBox)v.findViewById(R.id.contact_list_item_checkbox); | |
mCheckBox.setChecked( mSelectedItems.get(index, false) ); | |
mTextViewName.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// Log.d(TAG, mName + " was clicked"); | |
mCheckBox.performClick(); | |
} | |
}); | |
mCheckBox.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// Log.d(TAG, mName + " was clicked"); | |
} | |
}); | |
mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | |
Log.d(TAG, mName + " was changed to " + (isChecked ? "on" : "off") ); | |
if ( isChecked ) { | |
mSelectedItems.put(index, true); | |
} else { | |
mSelectedItems.delete(index); | |
} | |
Log.d(TAG, mSelectedItems.toString()); | |
} | |
}); | |
} | |
} | |
private SparseBooleanArray mSelectedItems = new SparseBooleanArray(); | |
private List<Contact> mContacts; | |
private List<String> mPeople; | |
private Context mContext; | |
public ContactsListAdapter(Context context, List<Contact> items) { | |
this.mContext = context; | |
// this represents the list of actual contacts in this group already | |
this.mContacts = items; | |
// load the contacts list into this array | |
// in real life, this not what we would or should be doing though | |
this.mPeople = new ArrayList<String>(Arrays.asList(context.getResources().getStringArray(R.array.contacts))); | |
// mDB = new DatabaseHelper(context); | |
} | |
@Override | |
public ContactsListAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { | |
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_contact_list, viewGroup, false); | |
ContactsListAdapter.ViewHolder vh = new ContactsListAdapter.ViewHolder(v); | |
return vh; | |
} | |
@Override | |
public void onBindViewHolder(ContactsListAdapter.ViewHolder viewHolder, int i) { | |
// TODO: check the checkbox if the contact is in this group | |
viewHolder.index = i; | |
viewHolder.mName = mPeople.get(i); | |
viewHolder.mTextViewName.setText(mPeople.get(i)); | |
viewHolder.mCheckBox.setChecked( mSelectedItems.get(i, false) ); | |
// Log.d(TAG, "Contact = " + viewHolder.mName); | |
} | |
@Override | |
public int getItemCount() { | |
return mPeople.size(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment