0

I'm trying to add a new food with some add ons, basically everytime the add field button is press it will create two edittext which is the name and price of the add on, there will be a scenario whereby users wants to delete the edittext name and price. I've successfully created two edittexts also setting them with the IDs that i'm giving them in integer. But I'm having problem with deleting the two edittext with a button whenever i click the delete field button.


// Add On
        addOnLayout.removeAllViews();
        addFields.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddOnCounters++;

                etFoodName = new EditText(addfoodAddOn.this);
                etFoodName.setId(ETFoodID);

                etFoodName.setHint("Enter your AddOn Name");

                etPrice = new EditText(addfoodAddOn.this);
                etPrice.setId(ETPriceID);
                etPrice.setHint("Enter your AddOn Price");

                Log.d("-------","-------------------what is edittext name id " + ETFoodID);
                Log.d("-------","-------------------what is edittext price id " + ETPriceID);

                ETFoodID++;
                ETPriceID++;


                addOnLayout.addView(etFoodName);
                addOnLayout.addView(etPrice);





            }
        });

        removeFields.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(AddOnCounters>=1 && ETFoodID > 0 && ETPriceID > 10){


                    addOnLayout.removeViewAt(ETFoodID);
                    addOnLayout.removeViewAt(ETPriceID);
                    ETFoodID--;
                    ETPriceID--;

                }
            }
        });

I'm expecting the result to be whenever i click the delete field button, it will delete the very bottom last two edittexts which is the name and price.

fdelafuente
  • 1,114
  • 1
  • 13
  • 24
Justin
  • 63
  • 1
  • 1
  • 7

3 Answers3

0

After the button click just do this:

addFields.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              etPrice.setText("")
              etFoodName.setText("");
     }
}
coroutineDispatcher
  • 7,718
  • 6
  • 30
  • 58
0

Replace your listener

removeFields.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(AddOnCounters>=1 && ETFoodID > 0 && ETPriceID > 10){
                View v1 = addOnLayout.findViewById(ETFoodID);
                View v2 = addOnLayout.findViewById(ETPriceID);
                addOnLayout.removeView(v1);
                addOnLayout.removeView(v2);
                ETFoodID--;
                ETPriceID--;

            }
        }
    });
Rajat Mehra
  • 1,462
  • 14
  • 19
0

Remove it from its parent.

removeFields.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeFromParent(etFoodName);
                removeFromParent(etPrice);
            }
        });

static void removeFromParent(View view) {
    ((ViewGroup)view.getParent()).removeView(view);
}
Israel dela Cruz
  • 794
  • 1
  • 5
  • 11