razituli

HargaAdapter

Dec 16th, 2021
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. package com.example.aplikasipasarikan.harga;
  2.  
  3. import static android.widget.TextView.*;
  4.  
  5. import android.content.Context;
  6. import android.text.Editable;
  7. import android.text.TextWatcher;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.Filter;
  12. import android.widget.Filterable;
  13. import android.widget.ImageView;
  14. import android.widget.TextView;
  15.  
  16. import androidx.annotation.NonNull;
  17. import androidx.recyclerview.widget.RecyclerView;
  18.  
  19. import com.bumptech.glide.Glide;
  20. import com.example.aplikasipasarikan.Dashboard;
  21. import com.example.aplikasipasarikan.R;
  22.  
  23. import java.text.NumberFormat;
  24. import java.util.ArrayList;
  25. import java.util.Locale;
  26.  
  27. public class HargaAdapter extends RecyclerView.Adapter<HargaAdapter.ViewHolder> implements Filterable {
  28. private Context mContext;
  29. private ArrayList<HargaModel> hargaModels;
  30. private ArrayList<HargaModel> hargaModelsFull;
  31. private HargaAdapter.OnItemClickListener mlistener;
  32. private int lastPosition = -1;
  33.  
  34. public HargaAdapter(HargaActivity context, ArrayList<HargaModel> hargaModelArrayList) {
  35. mContext = context;
  36. hargaModels = hargaModelArrayList;
  37. hargaModelsFull = new ArrayList<>(hargaModelArrayList);
  38.  
  39. }
  40.  
  41.  
  42. public void HargaAdapterFull(ArrayList<HargaModel> hargaModelArrayList) {
  43. hargaModelsFull.clear();
  44. hargaModelsFull = new ArrayList<>(hargaModelArrayList);
  45. notifyDataSetChanged();
  46. }
  47.  
  48. public void OnItemClickListener (HargaAdapter.OnItemClickListener listener){
  49. mlistener = listener;
  50. }
  51.  
  52. @Override
  53. public Filter getFilter() {
  54. return hargaFilter;
  55. }
  56.  
  57. private Filter hargaFilter = new Filter() {
  58. @Override
  59. protected FilterResults performFiltering(CharSequence constraint) {
  60. ArrayList<HargaModel> filteredList = new ArrayList<>();
  61.  
  62. if (constraint == null || constraint.length() == 0){
  63. filteredList.addAll(hargaModelsFull);
  64. } else {
  65. String filterPattern = constraint.toString().toLowerCase().trim();
  66.  
  67. for (HargaModel item : hargaModelsFull){
  68. if (item.getTitle_jenis_ikan().toLowerCase().contains(filterPattern) || item.getTitle_instansi().toLowerCase().contains(filterPattern) || item.getTitle_instansi().toLowerCase().contains(filterPattern)){ //sesuaikan dengan field yang akan di cari, sesuai dengan getter di class item
  69. filteredList.add(item);
  70. }
  71. }
  72. }
  73.  
  74. FilterResults results = new FilterResults();
  75. results.values = filteredList;
  76.  
  77. return results;
  78.  
  79. }
  80.  
  81. @Override
  82. protected void publishResults(CharSequence constraint, FilterResults results) {
  83. hargaModels.clear();
  84. hargaModels.addAll((ArrayList)results.values);
  85. notifyDataSetChanged();
  86.  
  87. }
  88. };
  89.  
  90. public interface OnItemClickListener{
  91. void onItemClick(String id, String nama);
  92. }
  93.  
  94. @NonNull
  95. @Override
  96. public HargaAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  97. View v = LayoutInflater.from(mContext).inflate(R.layout.item_list_harga, parent, false);
  98. return new ViewHolder(v);
  99. }
  100.  
  101. @Override
  102. public void onBindViewHolder(@NonNull HargaAdapter.ViewHolder holder, int position) {
  103. final HargaModel currentItem = hargaModels.get(position);
  104.  
  105.  
  106.  
  107. String title_jenis_ikan = currentItem.getTitle_jenis_ikan();
  108. String title_instansi = currentItem.getTitle_instansi();
  109. String gambar = currentItem.getGambar();
  110. String eceran = currentItem.getEceran();
  111. String created_modified = currentItem.getCreated_modified();
  112.  
  113. String [] date = created_modified.split("-");
  114.  
  115. Glide.with(mContext)
  116. .load(currentItem.getGambar())
  117. .into(holder.img_item_photo);
  118. holder.txtname.setText(title_jenis_ikan);
  119. holder.txtharga.setText(eceran);
  120. holder.txtkabkota.setText(title_instansi);
  121. holder.txttgl.setText(date[2].substring(0,2)+"-"+date[1]+"-"+date[0]+ ""+date[2].substring(2,10));
  122.  
  123. holder.itemView.setOnClickListener(new View.OnClickListener() {
  124. @Override
  125. public void onClick(View view) {
  126. // mListener.onItemClick(id, nama);
  127. }
  128. });
  129.  
  130. }
  131.  
  132. private String getCapsSentences(String text){
  133. if (text.equals("") || text.isEmpty()){
  134. return "";
  135. }
  136. else{
  137. String output = "";
  138.  
  139. // Make array of word and cap
  140. String[] textArray = text.trim().split("\\s+");
  141. for (int i = 0; i < textArray.length; i++){
  142. textArray[i] = textArray[i].substring(0,1).toUpperCase() + textArray[i].substring(1);
  143. }
  144.  
  145. // convert the array to string
  146. for(int i = 0; i < textArray.length; i++){
  147. output = output + textArray[i] + " ";
  148. }
  149.  
  150. return output.trim();
  151. }
  152. }
  153.  
  154. @Override
  155. public int getItemCount() {
  156. return hargaModels.size();
  157. }
  158.  
  159.  
  160.  
  161. public class ViewHolder extends RecyclerView.ViewHolder {
  162. ImageView
  163. img_item_photo;
  164.  
  165. TextView
  166. txtname, txtharga, txtkabkota, txttgl;
  167. public ViewHolder(@NonNull View itemView) {
  168. super(itemView);
  169. txtname = itemView.findViewById(R.id.txtname);
  170. txtharga = itemView.findViewById(R.id.txtharga);
  171. txtkabkota = itemView.findViewById(R.id.txtkabkota);
  172. txttgl = itemView.findViewById(R.id.txttgl);
  173. img_item_photo = itemView.findViewById(R.id.img_item_photo);
  174.  
  175. txtharga.addTextChangedListener(new TextWatcher() {
  176. String setTextInputEditText = txtharga.getText().toString().trim();
  177. @Override
  178. public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  179.  
  180. }
  181.  
  182. @Override
  183. public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  184. if (!charSequence.toString().equals(setTextInputEditText)){
  185. txtharga.removeTextChangedListener(this);
  186. String replace = charSequence.toString().replaceAll("[Rp. ]","");
  187. if (!replace.isEmpty()){
  188. setTextInputEditText = formatRupiah(Double.parseDouble(replace));
  189. }
  190. else {
  191. setTextInputEditText="";
  192. }
  193. txtharga.setText(setTextInputEditText);
  194. txtharga.addTextChangedListener(this);
  195. }
  196.  
  197. }
  198.  
  199. @Override
  200. public void afterTextChanged(Editable editable) {
  201.  
  202. }
  203. });
  204. }
  205.  
  206. private String formatRupiah(Double number){
  207. Locale locale = new Locale("IND","ID");
  208. NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
  209. String formatRupiah = numberFormat.format(number);
  210. String[] split= formatRupiah.split(",");
  211. int length= split[0].length();
  212. return split[0].substring(0,2)+". "+split[0].substring(2,length);
  213. }
  214.  
  215. }
  216.  
  217.  
  218. }
  219.  
Add Comment
Please, Sign In to add comment