Created
August 11, 2015 17:27
-
-
Save rclements/5abb610f353356e09144 to your computer and use it in GitHub Desktop.
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
static class ViewHolder { | |
TextView prescriptionDosage; | |
TextView prescriptionName; | |
TextView prescriptionDoses; | |
TextView prescriptionDescription; | |
Button acceptButton; | |
Button rejectButton; | |
ImageView flagImage; | |
} | |
private class PrescriptionAdapter extends ArrayAdapter<Prescription> { | |
PrescriptionAdapter(Prescription[] prescriptions) { | |
super(PrescriptionActivity.this, R.layout.prescription_list_row, R.id.prescription_name, prescriptions); | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
final ViewHolder viewHolder; | |
if(convertView==null){ | |
LayoutInflater inflater = ((Activity) PrescriptionActivity.this).getLayoutInflater(); | |
convertView = inflater.inflate(R.layout.prescription_list_row, parent, false); | |
viewHolder = new ViewHolder(); | |
viewHolder.prescriptionName = (TextView) convertView.findViewById(R.id.prescription_name); | |
viewHolder.prescriptionDosage = (TextView)convertView.findViewById(R.id.prescription_dosage_measurement); | |
viewHolder.prescriptionDescription = (TextView)convertView.findViewById(R.id.prescription_description); | |
viewHolder.prescriptionDoses = (TextView)convertView.findViewById(R.id.prescription_doses); | |
viewHolder.acceptButton = (Button) convertView.findViewById(R.id.accept_button); | |
viewHolder.rejectButton = (Button) convertView.findViewById(R.id.reject_button); | |
convertView.setTag(viewHolder); | |
}else{ | |
viewHolder = (ViewHolder)convertView.getTag(); | |
} | |
final Prescription prescription = getItem(position); | |
if(prescription != null) { | |
viewHolder.prescriptionName.setText(prescription.name); | |
viewHolder.prescriptionDosage.setText(prescription.dosageMeasurement); | |
viewHolder.prescriptionDescription.setText(prescription.description); | |
Log.d(TAG, "remaining doses: " + prescription.getRemainingDoses()); | |
viewHolder.prescriptionDoses.setText("" + prescription.getRemainingDoses()); | |
final DatabaseHelper db = new DatabaseHelper(PrescriptionActivity.this); | |
Log.v(TAG, "Prescription ID: " + prescription._id); | |
if (db.prescriptionHasFlag(prescription._id)) { | |
viewHolder.flagImage = (ImageView) convertView.findViewById(R.id.flag_button); | |
final int yellow = Color.YELLOW; | |
viewHolder.flagImage.setBackgroundColor(yellow); | |
} | |
final int gray = Color.GRAY; | |
final int green = Color.GREEN; | |
final int red = Color.RED; | |
viewHolder.rejectButton.setBackgroundColor(gray); | |
viewHolder.acceptButton.setBackgroundColor(gray); | |
if (prescription.accepted.equals("true")) { | |
viewHolder.acceptButton.setSelected(true); | |
viewHolder.rejectButton.setSelected(false); | |
viewHolder.acceptButton.setBackgroundColor(green); | |
} else { | |
viewHolder.acceptButton.setSelected(false); | |
viewHolder.rejectButton.setSelected(true); | |
viewHolder.rejectButton.setBackgroundColor(red); | |
} | |
viewHolder.acceptButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
viewHolder.acceptButton.setSelected(!viewHolder.acceptButton.isSelected()); | |
prescription.accepted = "true"; | |
db.updatePrescription(prescription); | |
if (viewHolder.acceptButton.isSelected()) { | |
if (viewHolder.rejectButton.isSelected()) { | |
viewHolder.rejectButton.setSelected(false); | |
} | |
v.setBackgroundColor(green); | |
viewHolder.rejectButton.setBackgroundColor(gray); | |
} else { | |
v.setBackgroundColor(gray); | |
} | |
} | |
}); | |
viewHolder.rejectButton.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
viewHolder.rejectButton.setSelected(!viewHolder.rejectButton.isSelected()); | |
prescription.accepted = "false"; | |
db.updatePrescription(prescription); | |
if (viewHolder.rejectButton.isSelected()) { | |
if (viewHolder.acceptButton.isSelected()) { | |
viewHolder.acceptButton.setSelected(false); | |
} | |
v.setBackgroundColor(red); | |
viewHolder.acceptButton.setBackgroundColor(gray); | |
} else { | |
v.setBackgroundColor(gray); | |
} | |
} | |
}); | |
} | |
return convertView; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment