1

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

Fundhor
  • 3,369
  • 1
  • 25
  • 47
User3
  • 2,465
  • 8
  • 41
  • 84
  • Your question is answered here: [Why are only final variables accessible in anonymous class?](http://stackoverflow.com/questions/4732544/why-are-only-final-variables-accessible-in-anonymous-class) – Jan Dörrenhaus Nov 27 '15 at 10:08
  • Going through it now – User3 Nov 27 '15 at 10:35

2 Answers2

1

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);

kiran boghra
  • 3,662
  • 2
  • 18
  • 23
  • That is exactly my question. I am not using it this way, but it works! – User3 Nov 27 '15 at 10:09
  • check this line of your code you are using final View addView = layoutInflater.inflate(R.layout.add_company_fragment_two_dynamic_content, null); so it takes reference of addView while adding view and you are using the same object while removing view so it has reference of object while adding view. – kiran boghra Nov 27 '15 at 10:18
  • What happens when I add a second view? I agree it has something to do with that `final` keyword, but how is this handled is the question. . – User3 Nov 27 '15 at 10:34
  • addView is ViewGroup I think LinearLayout for you which contains childviews so when you remove addView all child of addView is automatically remove. you adding only one view addView with multiple childs. – kiran boghra Nov 27 '15 at 10:47
  • I am adding multiple views, please read the question carefully. – User3 Nov 27 '15 at 10:48
  • Now I got your question clearly Java is OOP so when you use public void addView() method remove_dynamic holds reference of addView , and when second time you call this method another instance of addView and remove_dynamic will be generated so for every viewGroup you are adding, remove_dynamic has reference of associated addView. so it removed its own associated addView. – kiran boghra Nov 27 '15 at 11:02
  • Yes, how and where and when is that reference stored? I have added an image to better explain the scenario. – User3 Nov 27 '15 at 11:06
0

It's working due to this line

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

onClickListener() does not keep track of anything.

Nigam Patro
  • 2,760
  • 1
  • 18
  • 33