Reputation: 297
I try to delete all programmatically added buttons, but I've got a small problem.
Here is my code:
buttons = new Button[contacts.length()];
delete_button = new Button(getActivity());
delete_button.setText(String.valueOf(medicine_id));
delete_button.setId(medicine_id);
buttons[i] = delete_button;
layout.addView(delete_button);
First of all, I search in my database for specific products. I can get 0,1,2,3...n elements. And for these elements I need to create a delete button.
I create buttons in a loop and add them to Button[] table. Then I set text and id and these buttons show up on my view. The problem is when I want to remove them from my view.
Searching for a new product removes previously created buttons. But when I have a product which has e.g. two elements and then search for a product which has one element, there is only one button removed from my view.
Here's my remove code:
if (buttons != null) {
for (Button s : buttons) {
layout.removeView(s);
}
}
https://i.sstatic.net/RldJJ.png
https://i.sstatic.net/fz3nN.png
https://i.sstatic.net/Vz2zA.png
As you can see, on third picture one button wasn't removed from my view - button with number 46. What should I change?
EDIT: I see that the last element from Button[] "buttons" is removed. Why all elements aren't removed?
Upvotes: 1
Views: 1055
Reputation: 402
As per my understanding, I am only giving you example. You can remove your views like
Android remove view from parent
View myView = findViewById(R.id.hiddenLayout);
ViewGroup parent = (ViewGroup) myView.getParent();
parent.removeView(myView);
Android remove all child views
LinearLayout formLayout = (LinearLayout)findViewById(R.id.formLayout);
formLayout.removeAllViews();
Upvotes: 2