Last active
April 5, 2018 19:10
-
-
Save LAHomieJob/a2c43d49f82e6817e107f27ebc9e9fdc to your computer and use it in GitHub Desktop.
GroupVcAdapter for 'beeb' user
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.hfad.singleton.adapter; | |
import android.content.Context; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.Filter; | |
import android.widget.Filterable; | |
import com.hfad.singleton.R; | |
import com.hfad.singleton.database.GroupVc; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class GroupsVcAdapter extends | |
RecyclerView.Adapter<GroupsViewHolder> implements Filterable{ | |
private LayoutInflater mInflater; | |
private List<GroupVc> mGroupsVc; | |
private List<GroupVc> filteredGroupsVc; | |
private OnItemClicked onClick; | |
private String lastFilter = ""; | |
public GroupsVcAdapter(Context context, OnItemClicked onClick) { | |
mInflater = LayoutInflater.from(context); | |
this.onClick = onClick; | |
// init the lists | |
mGroupsVc = new ArrayList<>(); | |
filteredGroupsVc = new ArrayList<>(); | |
} | |
public List<GroupVc> getFilteredGroupsVc() { | |
return filteredGroupsVc; | |
} | |
public List<GroupVc> getmGroupsVc() { | |
return mGroupsVc; | |
} | |
@Override | |
public GroupsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View itemView = mInflater.inflate(R.layout.layout_list_of_groups, parent, false); | |
return new GroupsViewHolder(itemView, onClick); | |
} | |
@Override | |
public void onBindViewHolder(final GroupsViewHolder holder, int position) { | |
if (filteredGroupsVc != null) { | |
GroupVc current = filteredGroupsVc.get(position); | |
holder.getNameView().setText(current.getNameGroup()); | |
} else { | |
holder.getNameView().setText(R.string.nogroups); | |
} | |
} | |
public void setGroupsVc(List<GroupVc> mGroupsVc) { | |
this.mGroupsVc = mGroupsVc; | |
notifyDataSetChanged(); | |
getFilter().filter(lastFilter); | |
notifyDataSetChanged(); | |
} | |
@Override | |
public int getItemCount() { | |
return filteredGroupsVc != null ? filteredGroupsVc.size() : 0; | |
} | |
@Override | |
public Filter getFilter() { | |
return new Filter() { | |
@Override | |
protected void publishResults(CharSequence constraint, FilterResults results) { | |
filteredGroupsVc = (List<GroupVc>) results.values; | |
notifyDataSetChanged(); | |
} | |
@Override | |
protected FilterResults performFiltering(CharSequence constraint) { | |
lastFilter = constraint.toString(); | |
filteredGroupsVc = null; | |
if (lastFilter.length() == 0) { | |
filteredGroupsVc.addAll(mGroupsVc); | |
} else { | |
filteredGroupsVc = getFilteredResults(lastFilter.toLowerCase()); | |
} | |
FilterResults results = new FilterResults(); | |
results.values = filteredGroupsVc; | |
results.count = filteredGroupsVc.size(); | |
return results; | |
} | |
}; | |
} | |
protected List<GroupVc> getFilteredResults(String constraint) { | |
List<GroupVc> results = new ArrayList<>(); | |
for (GroupVc item : mGroupsVc) { | |
if (item.getNameGroup().toLowerCase().contains(constraint)) { | |
results.add(item); | |
} | |
} | |
return results; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've changed it according to your recomendations