Last active
October 25, 2018 00:51
-
-
Save alfianyusufabdullah/8a125877a0913f0a5ceb16350766c530 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class KamusAdapter extends RecyclerView.Adapter<KamusAdapter.KamusHolder>{ | |
private ArrayList<KamusModel> dataKamus; | |
private Context context; | |
private LayoutInflater mInflater; | |
public KamusAdapter(Context context, ArrayList<KamusModel> dataKamus){ | |
this.context = context; | |
this.dataKamus = dataKamus; | |
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
} | |
private ArrayList<KamusModel> getKamusModels (){ | |
return dataKamus; | |
} | |
void setKamusModels(ArrayList<KamusModel> dataKamus){ | |
this.dataKamus = new ArrayList<>(); | |
this.dataKamus.addAll(dataKamus); | |
notifyDataSetChanged(); | |
} | |
public KamusModel getItem(int position){ | |
return kamusModelList.get(position); | |
} | |
@NonNull | |
@Override | |
public KamusHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | |
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_kata_row, parent, false); | |
return new KamusHolder(view); | |
} | |
public void addItem(ArrayList<KamusModel> dataKamus){ | |
this.dataKamus = dataKamus; | |
notifyDataSetChanged(); | |
} | |
public void clear() { | |
this.dataKamus.clear(); | |
notifyDataSetChanged(); | |
} | |
@Override | |
public void onBindViewHolder(@NonNull KamusHolder kamusHolder, int position) { | |
final KamusModel kamusModel = getKamusModels().get(position); | |
kamusHolder.tvKata.setText(kamusModel.getKata()); | |
// kamusHolder.tvArti.setText(kamusModel.getArti()); | |
kamusHolder.tvKata.setOnClickListener(new CustomOnItemClickListener(position, new CustomOnItemClickListener.OnItemClickCallback() { | |
@Override | |
public void onItemClicked(View view, int position) { | |
MainActivity myActivity = (MainActivity)context; | |
Fragment fragment = new FragmentDetail(); | |
Bundle bundle =new Bundle(); | |
bundle.putString(FragmentDetail.EXTRA_KATA, kamusModel.getKata()); | |
bundle.putString(FragmentDetail.EXTRA_ARTI, kamusModel.getArti()); | |
fragment.setArguments(bundle); | |
if (fragment != null) { | |
myActivity.getSupportFragmentManager() | |
.beginTransaction() | |
.replace(R.id.content_main, fragment) | |
.addToBackStack(null) | |
.commit(); | |
} | |
} | |
})); | |
} | |
@Override | |
public int getItemViewType(int position) { | |
return 0; | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public int getItemCount() { | |
return dataKamus.size(); | |
} | |
public static class KamusHolder extends RecyclerView.ViewHolder{ | |
private TextView tvKata, tvArti; | |
public KamusHolder(View itemView){ | |
super(itemView); | |
tvKata = (TextView)itemView.findViewById(R.id.txt_kata); | |
// tvArti = (TextView)itemView.findViewById(R.id.txt_arti); | |
} | |
} | |
public void filter(String charText){ | |
charText = charText.toLowerCase(Locale.getDefault()); | |
dataKamus = new ArrayList<>(): | |
if (charText.length()==0){ | |
dataKamus = new ArrayList<>(); | |
}else { | |
for (KamusModel wp : kamusModels){ | |
if (wp.getKata().toLowerCase(Locale.getDefault()).contains(charText)){ | |
dataKamus.add(wp); | |
} | |
} | |
} | |
notifyDataSetChanged(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment