user3213110
user3213110

Reputation: 199

How to dynamically remove random views in Android?

My app so far..

So here is what my app looks like so far. Every time I click the "+" button I go into another activity where I enter description, date and time and dynamically create a horizontal LinearLayout. With the X button to the left I'm deleting said layouts with this code (I know it's not the best way but it works for me so far):

        final Task toBeRemoved = x;

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myLayout.removeView((ViewGroup) v.getParent());
                Task.tasks.remove(toBeRemoved);
            }
        });

..while iterating through each element in a list where I store my values in my OnCreate method.

What I want to do now is make it so I can remove them with the assistance of the checkboxes and the "Clear" button as well.

I have added each layout dynamically, though, so I can't think of any way for me to determine which one I've checked for deletion. They have no id, they can't be stored anywhere so I can iterate through them, as far as I know. What can I do in this situation?

Upvotes: 0

Views: 148

Answers (3)

Vlad
Vlad

Reputation: 910

Here's a solution that I consider pretty cool.

Create a widget on your own.It's easy.

So, you would have a class that extends ...probably LinearLayout.Depends on your needs.

You create the button,the editText, the textview and the check box, than you add functionality like you want.

When the Check box is pressed, I suppose you want the X circle button to get activated and when you press it, it deletes the whole "thing".This way you won't even need the clear button.You will still need the add button though, so you can add how many of those items you need.

It's easy,practical and makes the code reusable, which is something I look for always.

If you need more help, I can help you in a bit more detail ,but I can't always be around so I am sorry if I will respond a bit slow.

Upvotes: 0

Gabe Sechan
Gabe Sechan

Reputation: 93561

A couple of ways. One would be to set listeners on each new checkbox and keep a Set of views with a checked state (add to the Set when checked, remove when unchecked). Then when clear is pressed, you remove all views in that Set.

The other way is to lopp through all the child views of the parent layout above the dynamically. For each one, find the checkbox child via findViewById, and see if its check. Remove it if it is. This is computationally expensive if you have lots of complex views

I prefer method 1 myself, but either works.

Upvotes: 2

Blackbelt
Blackbelt

Reputation: 157437

I have added each layout dynamically, though, so I can't think of any way for me to determine which one I've checked for deletion

that's not true. You can call setId( ) or setTag() while you are adding each layout,

They have no id, they can't be stored anywhere so I can iterate through them, as far as I know. What can I do in this situation?

now they have one. You can either use findViewById or findViewWithTag

Upvotes: 0

Related Questions