razituli

DataHargaAdapter

Dec 16th, 2021
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.20 KB | None | 0 0
  1. package com.example.aplikasipasarikan.CRUD.Harga;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.text.Editable;
  8. import android.text.TextWatcher;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.ImageView;
  13. import android.widget.LinearLayout;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. import androidx.annotation.NonNull;
  18. import androidx.recyclerview.widget.RecyclerView;
  19.  
  20. import com.androidnetworking.AndroidNetworking;
  21. import com.androidnetworking.common.Priority;
  22. import com.androidnetworking.error.ANError;
  23. import com.androidnetworking.interfaces.JSONObjectRequestListener;
  24. import com.bumptech.glide.Glide;
  25. import com.chauthai.swipereveallayout.SwipeRevealLayout;
  26. import com.chauthai.swipereveallayout.ViewBinderHelper;
  27. import com.example.aplikasipasarikan.R;
  28. import com.google.android.material.dialog.MaterialAlertDialogBuilder;
  29.  
  30. import org.json.JSONException;
  31. import org.json.JSONObject;
  32.  
  33. import java.text.DateFormat;
  34. import java.text.NumberFormat;
  35. import java.text.SimpleDateFormat;
  36. import java.util.ArrayList;
  37. import java.util.Calendar;
  38. import java.util.Locale;
  39.  
  40. public class DataHargaAdapter extends RecyclerView.Adapter<DataHargaAdapter.ViewHolder> {
  41. private Context mContext;
  42. private ArrayList<DataHargaModel> dataHargaModels;
  43. private ProgressDialog progressDialog;
  44. private ViewBinderHelper viewBinderHelper = new ViewBinderHelper();
  45.  
  46. public DataHargaAdapter(Context mContext, ArrayList<DataHargaModel> dataHargaModels) {
  47. this.mContext = mContext;
  48. this.dataHargaModels = dataHargaModels;
  49. }
  50.  
  51. @NonNull
  52. @Override
  53. public DataHargaAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  54. View v = LayoutInflater.from(mContext).inflate(R.layout.item_tampil_data, parent, false);
  55. return new ViewHolder(v);
  56. }
  57.  
  58. @Override
  59. public void onBindViewHolder(@NonNull DataHargaAdapter.ViewHolder holder, int position) {
  60. DataHargaModel item = dataHargaModels.get(position);
  61.  
  62. String [] date = item.getCreated_modified().split("-");
  63.  
  64. Glide.with(mContext)
  65. .load(item.getGambar())
  66. .into(holder.img_item_photo);
  67. holder.txtname.setText(item.getTitle_jenis_ikan());
  68. holder.txtstatus.setText(item.getNm_status());
  69. holder.txtharga.setText(item.getEceran());
  70. holder.txtkabkota.setText(item.getTitle_instansi());
  71.  
  72. holder.txttgl.setText(date[2].substring(0,2)+"-"+date[1]+"-"+date[0]+" "+date[2].substring(2,10));
  73.  
  74. viewBinderHelper.bind(holder.swipeRevealLayout,item.getId_harga_ikan());
  75.  
  76. holder.layoutDelete.setOnClickListener(new View.OnClickListener() {
  77. @Override
  78. public void onClick(View v) {
  79. new MaterialAlertDialogBuilder(mContext)
  80. .setTitle("Konfirmasi Hapus")
  81. .setMessage("Data akan dihapus, lanjutkan?")
  82. .setPositiveButton("Lanjutkan", new DialogInterface.OnClickListener() {
  83. @Override
  84. public void onClick(DialogInterface dialog, int i) {
  85. holder.deleteData();
  86. dataHargaModels.remove(position);
  87. notifyItemRemoved(position);
  88. notifyItemRangeChanged(position,dataHargaModels.size());
  89. notifyDataSetChanged();
  90.  
  91. }
  92. })
  93. .setNegativeButton("Batal", new DialogInterface.OnClickListener() {
  94. @Override
  95. public void onClick(DialogInterface dialog, int i) {
  96.  
  97. }
  98. })
  99. .show();
  100. }
  101. });
  102.  
  103. holder.item = item;
  104.  
  105. holder.layoutEdit.setOnClickListener(new View.OnClickListener() {
  106. @Override
  107. public void onClick(View v) {
  108. Intent kirimData = new Intent(mContext,UpdateHarga.class);
  109. kirimData.putExtra("title_instansi", item.getTitle_instansi());
  110. kirimData.putExtra("title_jenis_ikan", item.getTitle_jenis_ikan());
  111. kirimData.putExtra("eceran", item.getEceran());
  112. mContext.startActivity(kirimData);
  113. }
  114. });
  115.  
  116.  
  117. holder.item = item;
  118.  
  119.  
  120. }
  121.  
  122. @Override
  123. public int getItemCount() {
  124. return dataHargaModels.size();
  125. }
  126.  
  127. public class ViewHolder extends RecyclerView.ViewHolder {
  128. ImageView img_item_photo;
  129. TextView txtname,txtkabkota,txtharga,txttgl,txtstatus;
  130. private SwipeRevealLayout swipeRevealLayout;
  131. private LinearLayout layoutDelete,layoutEdit;
  132. DataHargaModel item;
  133.  
  134. public ViewHolder(@NonNull View itemView) {
  135. super(itemView);
  136.  
  137. img_item_photo = itemView.findViewById(R.id.img_item_photo);
  138. txtname = itemView.findViewById(R.id.txtname);
  139. txtkabkota = itemView.findViewById(R.id.txtkabkota);
  140. txtharga = itemView.findViewById(R.id.txtharga);
  141. txttgl = itemView.findViewById(R.id.txttgl);
  142. txtstatus = itemView.findViewById(R.id.txtstatus);
  143. swipeRevealLayout = itemView.findViewById(R.id.swipereveallayout);
  144. layoutDelete = itemView.findViewById(R.id.layoutDelete);
  145. layoutEdit = itemView.findViewById(R.id.layoutEdit);
  146.  
  147. Calendar calendar = Calendar.getInstance();
  148. String currentDate = DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());
  149. txttgl.setText(currentDate);
  150.  
  151.  
  152. txtharga.addTextChangedListener(new TextWatcher() {
  153. String setTextInputEditText = txtharga.getText().toString().trim();
  154. @Override
  155. public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  156.  
  157. }
  158.  
  159. @Override
  160. public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
  161. if (!charSequence.toString().equals(setTextInputEditText)){
  162. txtharga.removeTextChangedListener(this);
  163. String replace = charSequence.toString().replaceAll("[Rp. ]","");
  164. if (!replace.isEmpty()){
  165. setTextInputEditText = formatRupiah(Double.parseDouble(replace));
  166. }
  167. else {
  168. setTextInputEditText="";
  169. }
  170. txtharga.setText(setTextInputEditText);
  171. txtharga.addTextChangedListener(this);
  172. }
  173.  
  174. }
  175.  
  176. @Override
  177. public void afterTextChanged(Editable editable) {
  178.  
  179. }
  180. });
  181. }
  182.  
  183. private String formatRupiah(Double number){
  184. Locale locale = new Locale("IND","ID");
  185. NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
  186. String formatRupiah = numberFormat.format(number);
  187. String[] split= formatRupiah.split(",");
  188. int length= split[0].length();
  189. return split[0].substring(0,2)+". "+split[0].substring(2,length);
  190.  
  191. }
  192.  
  193.  
  194.  
  195. public void deleteData(){
  196. progressDialog = new ProgressDialog(mContext);
  197. progressDialog.setCancelable(false);
  198. progressDialog.setMessage("Loading...");
  199. progressDialog.show();
  200. AndroidNetworking.post("https://testing.sumbarprov.go.id/pasarikan/api/delete_harga_ikan?id_harga_ikan={id}")
  201. .addPathParameter("id", item.getId_harga_ikan())
  202. .setTag("test")
  203. .setPriority(Priority.MEDIUM)
  204. .build()
  205. .getAsJSONObject(new JSONObjectRequestListener() {
  206. @Override
  207. public void onResponse(JSONObject response) {
  208. try {
  209. response.getString("status").equalsIgnoreCase("success");
  210. Toast.makeText(mContext, "Data Berhasil Dihapus", Toast.LENGTH_SHORT).show();
  211. progressDialog.dismiss();
  212.  
  213. }catch (JSONException e){
  214. Toast.makeText(mContext, "Data Gagal Dihapus " + e.getMessage(), Toast.LENGTH_SHORT).show();
  215. progressDialog.dismiss();
  216. }
  217. }
  218. @Override
  219. public void onError(ANError error) {
  220. Toast.makeText(mContext, "Data gagal Dihapus " + error.getMessage(), Toast.LENGTH_SHORT).show();
  221. progressDialog.dismiss();
  222. }
  223. });
  224.  
  225.  
  226. }
  227.  
  228.  
  229.  
  230.  
  231.  
  232. }
  233.  
  234.  
  235.  
  236.  
  237. }
  238.  
Add Comment
Please, Sign In to add comment