Advertisement
AleksandarH

ClothesDialogFragment

Dec 12th, 2023
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. package com.example.a20621646_k2;
  2.  
  3. import android.os.Bundle;
  4.  
  5. import androidx.fragment.app.DialogFragment;
  6.  
  7. import android.util.Log;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.Toast;
  14.  
  15. public class ClothesDialogFragment extends DialogFragment {
  16.  
  17.     public interface OnClothesUpdatedListener {
  18.         void onClothesUpdated(Clothes updatedClothes);
  19.     }
  20.  
  21.     private Clothes clothes;
  22.     private OnClothesUpdatedListener listener;
  23.  
  24.     public ClothesDialogFragment(Clothes clothes) {
  25.         this.clothes = clothes;
  26.     }
  27.  
  28.     public void setListener(OnClothesUpdatedListener listener) {
  29.         this.listener = listener;
  30.     }
  31.  
  32.     @Override
  33.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  34.         View view = inflater.inflate(R.layout.fragment_clothes_dialog, container, false);
  35.  
  36.         EditText priceEditText = view.findViewById(R.id.priceEditText);
  37.         EditText quantityEditText = view.findViewById(R.id.quantityEditText);
  38.         EditText countryEditText = view.findViewById(R.id.countryEditText);
  39.         Button saveButton = view.findViewById(R.id.saveButton);
  40.         Button cancelButton = view.findViewById(R.id.cancelButton);
  41.  
  42.         saveButton.setOnClickListener(v -> {
  43.             String newPriceText = priceEditText.getText().toString();
  44.             String newQuantityText = quantityEditText.getText().toString();
  45.             String newCountry = countryEditText.getText().toString();
  46.  
  47.             if (newPriceText.isEmpty() || newQuantityText.isEmpty() || newCountry.isEmpty()) {
  48.                 Toast.makeText(getActivity(), "Please fill in all fields", Toast.LENGTH_SHORT).show();
  49.                 Log.d("20621639", "Save button clicked: Some fields are empty");
  50.             } else {
  51.                 double newPrice = Double.parseDouble(newPriceText);
  52.                 int newQuantity = Integer.parseInt(newQuantityText);
  53.  
  54.                 clothes.setPrice(newPrice);
  55.                 clothes.setQuantity(newQuantity);
  56.                 clothes.setOriginCountry(newCountry);
  57.                 Log.d("20621639", "Save button clicked: Clothes updated");
  58.                 dismiss();
  59.             }
  60.         });
  61.  
  62.         cancelButton.setOnClickListener(v -> dismiss());
  63.         Log.d("20621639", "Cancel button clicked");
  64.         return view;
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement