From 54d5abb5417f9ec1880d37c0a772be449f4af332 Mon Sep 17 00:00:00 2001 From: jacques Date: Sun, 26 Jun 2022 11:56:06 +0200 Subject: [PATCH] Change iteraror to a for loop --- .../foucry/pilldroid/DrugListActivity.java | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java index f711576..b0846da 100644 --- a/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/DrugListActivity.java @@ -51,9 +51,9 @@ import net.foucry.pilldroid.models.Medicine; import net.foucry.pilldroid.models.Prescription; import java.text.SimpleDateFormat; +import java.util.Comparator; import java.util.Date; import java.util.List; -import java.util.ListIterator; import java.util.Locale; /** @@ -280,19 +280,28 @@ public class DrugListActivity extends AppCompatActivity { } public void constructDrugsList() { - PrescriptionsDAO prescriptionsDAO = prescriptions.getPrescriptionsDAO(); prescriptionList = prescriptionsDAO.getAllMedics(); - ListIterator listIterator = prescriptionList.listIterator(); Prescription currentPrescription; - while (listIterator.hasNext()) { - currentPrescription = listIterator.next(); + // Sorting list by dateEndOfStock + prescriptionList.sort(new Comparator<>() { + @Override + public int compare(Prescription lhs, Prescription rhs) { + if (lhs.getDateEndOfStock().compareTo(rhs.getDateEndOfStock()) != 0) + return lhs.getDateEndOfStock().compareTo(rhs.getDateEndOfStock()); + else + return (int) (lhs.getStock() - rhs.getStock()); + } + }); + // Move Prescription with take==0 to the end of the list + for (int i=0 ; i < prescriptionList.size(); i++ ){ + currentPrescription = prescriptionList.get(i); if (currentPrescription.getTake() == 0) { prescriptionList.remove(currentPrescription); - prescriptionList.add(prescriptionList.size()-1, currentPrescription); + prescriptionList.add(prescriptionList.size(), currentPrescription); } } @@ -492,6 +501,7 @@ public class DrugListActivity extends AppCompatActivity { } @Override + @SuppressWarnings("deprecation") public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) { int position = viewHolder.getBindingAdapterPosition(); @@ -510,8 +520,6 @@ public class DrugListActivity extends AppCompatActivity { startActivityForResult(intent, CUSTOMIZED_REQUEST_CODE); } - - Snackbar.make(recyclerView, prescription.getName(), Snackbar.LENGTH_LONG).setAction(R.string.Undo, new View.OnClickListener() { @Override @@ -581,7 +589,7 @@ public class DrugListActivity extends AppCompatActivity { * SimpleItemRecyclerViewAdapter */ public class RecyclerViewAdapter extends - RecyclerView.Adapter { + RecyclerView.Adapter { private final List mValues; @@ -627,7 +635,6 @@ public class DrugListActivity extends AppCompatActivity { holder.mContentView.setText(mValues.get(position).getName()); holder.mEndOfStock.setText(dateEndOfStock); - // Test to change background programmatically if (mValues.get(position).getTake() == 0) { holder.mView.setBackgroundResource(R.drawable.gradient_bg); @@ -672,7 +679,6 @@ public class DrugListActivity extends AppCompatActivity { } }); } - } @Override