User3
User3

Reputation: 2535

How does onClickListener keep track of the view to be removed

I am following a tutorial here - Now instead of adding a textView I have the following method as a replacement:

public void addView(LinearLayout container, String[] spin_array, String hint, int inputType, int tagger) {

        LayoutInflater layoutInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        final View addView = layoutInflater.inflate(R.layout.add_company_fragment_two_dynamic_content, null);

        final Spinner spin_dynamic = (Spinner) addView.findViewById(R.id.email_spinner1);
        EditText edt_dynamic = (EditText) addView.findViewById(R.id.edittext_email1);
        ImageView remove_dynamic = (ImageView) addView.findViewById(R.id.btn_remove);


        edt_dynamic.setHint(hint);
        setUpSpinners(spin_array, spin_dynamic);
        edt_dynamic.setTag(container.getTag() + "edt" + tagger);
        edt_dynamic.setInputType(inputType);
        edt_dynamic.setTypeface(roboto_condenced_light);
        spin_dynamic.setTag(container.getTag() + "spin" + tagger);

        idsMap.put(spin_dynamic.getTag().toString(), edt_dynamic.getTag().toString());

        // get height from dimens
        int height = (int) getResources().getDimension(R.dimen.lin_height);
        // set this height
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height);

        // we are only concerned about top margin here.
        layoutParams.setMargins(0, (int) getResources().getDimension(R.dimen.topMargin), 0, 0);
        container.addView(addView, 1, layoutParams);

        remove_dynamic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Remove View (LinearLayout) and also remove the key from idsMap so that we dont get a NullPointer later
                ((LinearLayout) addView.getParent()).removeView(addView);
                idsMap.remove(spin_dynamic.getTag().toString());
            }
        });
    }

The concern here is that I can call the above function umpteen times, to insert a view row in a LinearLayout.

I fail to understand how the addView variable is kept track of, when we try to remove a view from the container:

remove_dynamic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Remove View (LinearLayout) and also remove the key from idsMap so that we dont get a NullPointer later
                    ((LinearLayout) addView.getParent()).removeView(addView);
                    idsMap.remove(spin_dynamic.getTag().toString());
                }
            });

How does the onClickListener know the addView instance to be removed here, if I insert four views how does teh onClick exactly know which one to remove?

There are no errors, everything is working as it should. But why is it working?

Following is an image representing this behavior:

enter image description here

Upvotes: 1

Views: 118

Answers (2)

kiran boghra
kiran boghra

Reputation: 3822

you have to use the same viewobject while removing view for example you have added three view in ViewGroup

addView1
addView2
addView3

so when you want to remove fistview i.e. addView1 you have to use that object ((LinearLayout) addView.getParent()).removeView(addView1);

Upvotes: 1

Nigam Patro
Nigam Patro

Reputation: 2770

It's working due to this line

((LinearLayout) addView.getParent()).removeView(addView);

onClickListener() does not keep track of anything.

Upvotes: 0

Related Questions