@Override public Dialog onCreateDialog(Bundle savedInstanceState) { MyLog.i("dialogEditItem", "onCreateDialog"); // inflate the xml layout LayoutInflater inflater = getActivity().getLayoutInflater(); View view = inflater.inflate(R.layout.dialog_edit_item, null, false); // find the dialog's views txtItemName = (EditText) view.findViewById(R.id.txtItemName); txtItemNote = (EditText) view.findViewById(R.id.txtItemNote); spnGroup = (Spinner) view.findViewById(R.id.spnGroup); ckIsFavorite = (CheckBox) view.findViewById(R.id.ckIsFavorite); // get the item Group itemGroup = null; if (mItem != null) { txtItemName.setText(mItem.getItemName()); mInitialItemName = mItem.getItemName(); txtItemNote.setText(mItem.getItemNote()); ckIsFavorite.setChecked(mItem.isFavorite()); itemGroup = mItem.getGroup(); } else { MyLog.e("dialogEditItem", "onCreateDialog: No Item available!"); } // fill spnGroup List<Group> groups = Group.getAllGroups(); GroupsSpinnerArrayAdapter adapter = new GroupsSpinnerArrayAdapter(getActivity(), groups); spnGroup.setAdapter(adapter); if (itemGroup != null) { int startingPosition = getGroupStartingPosition(itemGroup, groups); spnGroup.setSelection(startingPosition); } // build the dialog mAlertDialog = new AlertDialog.Builder(getActivity()) .setTitle(mDialogTitle) .setView(view) .setPositiveButton( getActivity().getString(R.string.btnOk_text), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String proposedItemName = txtItemName.getText().toString().trim(); if (proposedItemName.isEmpty()) { // Error, the proposed name is empty! Set the text box to the // original initial Item name. txtItemName.setText(mInitialItemName); // Inform the user of the error. String title = "Error Updating Item Name"; String msg = "Unable to update the Item's name because the proposed name is empty!"; EventBus.getDefault().post(new MyEvents.showOkDialog(title, msg)); return; } // verify that there is no other item with the same proposed item name if (!Item.itemExists(proposedItemName)) { // The Item does not exist in the table // so update the item's name mItem.setItemName(proposedItemName); } else if (proposedItemName .toLowerCase() .equals(mInitialItemName.toLowerCase())) { // The item name already exists in the items table. // However, its letters are the same so there is only a // difference letter capitalization so update the Item's name mItem.setItemName(proposedItemName); } else { // Error, the proposed name is in use! Set the text box to the // original initial Item name. txtItemName.setText(mInitialItemName); // Inform the user of the error. String title = "Error Updating Item Name"; String msg = "Unable to update the Item's name because the proposed name \"" + proposedItemName + "\" is already in use."; EventBus.getDefault().post(new MyEvents.showOkDialog(title, msg)); return; } // update the item's note mItem.setItemNote(txtItemNote.getText().toString().trim()); // update the item's group Group selectedGroup = (Group) spnGroup.getSelectedItem(); mItem.setGroup(selectedGroup); // update the item's Favorite mItem.setFavorite(ckIsFavorite.isChecked()); // indicate that the item is dirty mItem.setItemDirty(true); // update the UI EventBus.getDefault().post(new MyEvents.updateUI(mItem.getItemID())); dismiss(); } }) .setNeutralButton( getActivity().getString(R.string.btnDelete_text), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { AlertDialog.Builder yesNoDialog = new AlertDialog.Builder(getActivity()); // set dialog title and message yesNoDialog .setTitle("Delete \"" + mItem.getItemName() + "\" ?") .setCancelable(true) .setPositiveButton( "Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { mItem.deleteEventually(); EventBus.getDefault().post(new MyEvents.updateUI(null)); dismiss(); } }) .setNegativeButton( "No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dismiss(); } }); // create alert dialog mYesNoAlertDialog = yesNoDialog.create(); mYesNoAlertDialog.setOnShowListener( new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialog) { Button btnYes = mYesNoAlertDialog.getButton(Dialog.BUTTON_POSITIVE); btnYes.setTextSize(18); Button btnNo = mYesNoAlertDialog.getButton(Dialog.BUTTON_NEGATIVE); btnNo.setTextSize(18); } }); // show it mYesNoAlertDialog.show(); } }) .setNegativeButton( getActivity().getString(R.string.btnCancel_text), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { dismiss(); } }) .create(); return mAlertDialog; }
@Override public View getItemView(Item item, View view, ViewGroup parent) { ShoppingListViewHolder holder; if (view == null) { view = mInflater.inflate(R.layout.row_shopping_list, parent, false); holder = new ShoppingListViewHolder(); holder.tvSelectedItem = (TextView) view.findViewById(R.id.tvSelectedItem); view.setTag(holder); } else { holder = (ShoppingListViewHolder) view.getTag(); } TextView tvSelectedItem = holder.tvSelectedItem; String selectedItemText = item.getItemName(); if (!item.getItemNote().isEmpty()) { selectedItemText = selectedItemText + " (" + item.getItemNote() + ")"; } tvSelectedItem.setText(selectedItemText); if (item.isStruckOut()) { setStrikeOut(tvSelectedItem); } else { setNoStrikeOut(tvSelectedItem); } // save the item so it can be retrieved later tvSelectedItem.setTag(item); tvSelectedItem.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { TextView tv = (TextView) v; final Item item = (Item) v.getTag(); // toggle the item's struckOut attribute Item.setStruckOut(mContext, item, !item.isStruckOut()); // save the now dirty item to the local datastore // item.pinInBackground(); // set the TextView's attributes to either strikeout or normal as appropriate if (item.isStruckOut()) { setStrikeOut(tv); } else { setNoStrikeOut(tv); } } }); tvSelectedItem.setOnLongClickListener( new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { Item item = (Item) v.getTag(); MyLog.i( "ShoppingListAdapter", "tvSelectedItem OnLongClick: Item " + item.getItemName()); EventBus.getDefault().post(new MyEvents.showEditItemDialog(item.getItemID())); return true; } }); return view; }