Java tutorial
package fragments; import hoaiquan.ho.valet.R; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager.LayoutParams; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.EditText; import android.widget.ImageView; import android.widget.RadioButton; import android.widget.TextView; import android.widget.Toast; import common.CachedData; public class ShowTicket extends DialogFragment implements OnClickListener { private boolean wasEdit = false; private TextView ticketdate; private EditText ticketSerial, ticketName, ticketModel, ticketColor, ticketLicense, ticketPark, ticketKey; private RadioButton sedanRadio, sportRadio, vanRadio, truckRadio, bikeRadio; private ImageView colorWheel, updatecheck, assigntask; private CheckBox editticket; public static final int COLOR_PICKER = 1; public static final int SAVE_PICKER = 2; @Override public void onResume() { super.onResume(); Window window = getDialog().getWindow(); window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); window.setGravity(Gravity.CENTER); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); View view = inflater.inflate(R.layout.ticket_show, container, false); ticketSerial = (EditText) view.findViewById(R.id.ticketserial); ticketName = (EditText) view.findViewById(R.id.ticketname); ticketdate = (TextView) view.findViewById(R.id.ticketdate); sedanRadio = (RadioButton) view.findViewById(R.id.sedanradio); sportRadio = (RadioButton) view.findViewById(R.id.sportradio); vanRadio = (RadioButton) view.findViewById(R.id.vanradio); truckRadio = (RadioButton) view.findViewById(R.id.truckradio); bikeRadio = (RadioButton) view.findViewById(R.id.bikeradio); ticketModel = (EditText) view.findViewById(R.id.ticketmodel); ticketColor = (EditText) view.findViewById(R.id.ticketcolor); ticketLicense = (EditText) view.findViewById(R.id.ticketlicense); ticketPark = (EditText) view.findViewById(R.id.ticketparklocation); ticketKey = (EditText) view.findViewById(R.id.ticketkeylocation); colorWheel = (ImageView) view.findViewById(R.id.colorwheel); updatecheck = (ImageView) view.findViewById(R.id.updatecheck); assigntask = (ImageView) view.findViewById(R.id.assigntask); editticket = (CheckBox) view.findViewById(R.id.editbutton); Bundle mArgs = getArguments(); int position = mArgs.getInt("position"); widgetToggle(false); updatecheck.setVisibility(View.GONE); colorWheel.setOnClickListener(this); ticketColor.setOnClickListener(this); editticket.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (editticket.isChecked()) { widgetToggle(true); //updateticket.setVisibility(View.VISIBLE); updatecheck.setVisibility(View.VISIBLE); Toast.makeText(getActivity(), "Editting Enabled", Toast.LENGTH_SHORT).show(); } else { widgetToggle(false); //updateticket.setVisibility(View.GONE); updatecheck.setVisibility(View.GONE); Toast.makeText(getActivity(), "Editting Disabled", Toast.LENGTH_SHORT).show(); } } }); updatecheck.setOnClickListener(this); String cartype; final String ticketid; if (CachedData.getCachedProdTickets().isEmpty()) { ticketid = CachedData.getCachedTickets().get(position).get("t_id"); ticketSerial.setText(CachedData.getCachedTickets().get(position).get("t_serial")); ticketName.setText(CachedData.getCachedTickets().get(position).get("t_name")); ticketdate.setText(CachedData.getCachedTickets().get(position).get("t_datein")); ticketModel.setText(CachedData.getCachedTickets().get(position).get("t_vmodel")); ticketColor.setText(CachedData.getCachedTickets().get(position).get("t_vcolor")); ticketLicense.setText(CachedData.getCachedTickets().get(position).get("t_license")); ticketPark.setText(CachedData.getCachedTickets().get(position).get("t_parklocation")); ticketKey.setText(CachedData.getCachedTickets().get(position).get("t_keylocation")); cartype = CachedData.getCachedTickets().get(position).get("t_vtype"); if (cartype.equals("sedan")) { sedanRadio.setChecked(true); } else if (cartype.equals("coupe/sport")) { sportRadio.setChecked(true); } else if (cartype.equals("van/suv")) { vanRadio.setChecked(true); } else if (cartype.equals("truck")) { truckRadio.setChecked(true); } else if (cartype.equals("bike")) { bikeRadio.setChecked(true); } else { // nothing } } else { ticketid = CachedData.getCachedProdTickets().get(position).get("t_id"); ticketSerial.setText(CachedData.getCachedProdTickets().get(position).get("t_serial")); ticketName.setText(CachedData.getCachedProdTickets().get(position).get("t_name")); ticketdate.setText(CachedData.getCachedProdTickets().get(position).get("t_datein")); ticketModel.setText(CachedData.getCachedProdTickets().get(position).get("t_vmodel")); ticketColor.setText(CachedData.getCachedProdTickets().get(position).get("t_vcolor")); ticketLicense.setText(CachedData.getCachedProdTickets().get(position).get("t_license")); ticketPark.setText(CachedData.getCachedProdTickets().get(position).get("t_parklocation")); ticketKey.setText(CachedData.getCachedProdTickets().get(position).get("t_keylocation")); cartype = CachedData.getCachedProdTickets().get(position).get("t_vtype"); if (cartype.equals("sedan")) { sedanRadio.setChecked(true); } else if (cartype.equals("coupe/sport")) { sportRadio.setChecked(true); } else if (cartype.equals("van/suv")) { vanRadio.setChecked(true); } else if (cartype.equals("truck")) { truckRadio.setChecked(true); } else if (cartype.equals("bike")) { bikeRadio.setChecked(true); } else { // nothing } } assigntask.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { StaffPicker staffpick = new StaffPicker(); Bundle pArgs = new Bundle(); pArgs.putString("ticketid", ticketid); staffpick.setArguments(pArgs); staffpick.show(getFragmentManager(), "Staff Picker"); } }); return view; } private void widgetToggle(boolean trigger) { ticketSerial.setEnabled(trigger); ticketSerial.setFocusable(trigger); ticketSerial.setFocusableInTouchMode(trigger); ticketName.setEnabled(trigger); ticketName.setFocusable(trigger); ticketName.setFocusableInTouchMode(trigger); sedanRadio.setEnabled(trigger); sportRadio.setEnabled(trigger); vanRadio.setEnabled(trigger); truckRadio.setEnabled(trigger); bikeRadio.setEnabled(trigger); ticketModel.setEnabled(trigger); ticketModel.setFocusable(trigger); ticketModel.setFocusableInTouchMode(trigger); ticketColor.setEnabled(trigger); ticketLicense.setEnabled(trigger); ticketLicense.setFocusable(trigger); ticketLicense.setFocusableInTouchMode(trigger); ticketPark.setEnabled(trigger); ticketPark.setFocusable(trigger); ticketPark.setFocusableInTouchMode(trigger); ticketKey.setEnabled(trigger); ticketKey.setFocusable(trigger); ticketKey.setFocusableInTouchMode(trigger); colorWheel.setEnabled(trigger); colorWheel.setFocusableInTouchMode(trigger); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case COLOR_PICKER: if (resultCode == Activity.RESULT_OK) { Bundle bundle = data.getExtras(); String color = bundle.getString("color", ""); ticketColor.setText(color); } else if (resultCode == Activity.RESULT_CANCELED) { ticketColor.setText(""); } break; case SAVE_PICKER: if (resultCode == Activity.RESULT_OK) { Bundle bundle = data.getExtras(); String color = bundle.getString("color", ""); ticketColor.setText(color); } else if (resultCode == Activity.RESULT_CANCELED) { ticketColor.setText(""); } break; } } @Override public void onClick(View v) { ColorPicker colorPicker = new ColorPicker(); switch (v.getId()) { case R.id.colorwheel: colorPicker.setTargetFragment(this, COLOR_PICKER); colorPicker.show(getFragmentManager(), "Color Picker"); break; case R.id.ticketcolor: colorPicker.setTargetFragment(this, COLOR_PICKER); colorPicker.show(getFragmentManager(), "Color Picker"); break; case R.id.updatecheck: String id, ticket, name, date, cartype, model, color, license, park, key; ticket = ticketSerial.getText().toString().trim(); name = ticketName.getText().toString().trim(); cartype = CarType(); model = ticketModel.getText().toString().trim(); color = ticketColor.getText().toString().trim(); license = ticketLicense.getText().toString().trim(); park = ticketPark.getText().toString().trim(); key = ticketKey.getText().toString().trim(); Bundle mArgs = getArguments(); int position = mArgs.getInt("position"); if (CachedData.getCachedProdTickets().isEmpty()) { id = CachedData.getCachedTickets().get(position).get("t_id"); } else { id = CachedData.getCachedProdTickets().get(position).get("t_id"); } Bundle args = new Bundle(); args.putString("id", id); args.putString("ticket", ticket); args.putString("name", name); args.putString("cartype", cartype); args.putString("model", model); args.putString("color", color); args.putString("license", license); args.putString("park", park); args.putString("key", key); SavePicker save = new SavePicker(); save.setArguments(args); // save.setTargetFragment(this, SAVE_PICKER); save.show(getFragmentManager(), "Save"); } } private String CarType() { String cartype; if (sedanRadio.isChecked()) { cartype = "sedan"; } else if (sportRadio.isChecked()) { cartype = "coupe/sport"; } else if (vanRadio.isChecked()) { cartype = "van/suv"; } else if (truckRadio.isChecked()) { cartype = "truck"; } else if (bikeRadio.isChecked()) { cartype = "bike"; } else { cartype = ""; } return cartype; } public void closeOut(boolean flag) { if (flag) { dismiss(); } } }