lundi 4 mai 2015

Having trouble removing and then adding views programatically to FrameLayout

im having trouble removing a LinearLayout from a FrameLayout to then add a new LinearLayout that is populated with EditText generated programatically.

The first time that te view is loaded, i catch a reference to the FrameLayout using findViewById(), and then i make a new LinearLayout programatically and add a number of EditText to it. It looks like this

formPlace = (FrameLayout) findViewById(R.id.form_place);

LinearLayout linearView = new LinearLayout(this);
linearView = makeEditText(arrayItems,category);
linearView.setBackgroundColor(Color.GRAY);
linearView.setOrientation(LinearLayout.VERTICAL);

formPlace.addView(linearView);

Next is the function that makes the EditTexts:

private LinearLayout makeEditText(ArrayList<String[]> arrayString, int position){
        LinearLayout view = new LinearLayout(this);
        arrayInputs = new ArrayList<>();
        String[] arrayHints = arrayString.get(position);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        for (int i=0;i<arrayHints.length;i++){
            EditText editText = new EditText(this);
            Log.i("myApp","tantas veces" + i);

            editText.setId(i);
            editText.setTextColor(Color.BLACK);
            editText.setHint(arrayHints[i]);
            editText.setHintTextColor(Color.GRAY);


            editText.setLayoutParams(params);

            view.addView(editText);
            arrayInputs.add(editText);

        }
        LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        buttonParams.gravity = Gravity.RIGHT;

        android.widget.Button save = new android.widget.Button(this);
        save.setText("Guardar");
        save.setLayoutParams(buttonParams);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("myApp", "guardar");
                saveClicked(category);
            }
        });

        view.addView(save);

        android.widget.Button cancel = new android.widget.Button(this);
        cancel.setText("cancelar");
        cancel.setLayoutParams(buttonParams);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("myApp", "cancelar");
                cancelClicked();
            }
        });

        view.addView(cancel);
        return view;
    }

Then, when a spinner event is called, i catch the reference of the FrameLayout and pass .removeAllViews(). This works as intended, and my pevious view is deleted. The problem comes when i try to add new views to the FrameLayout in the makeEditText line:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {

                if(firstTime==false) {
                    FormActivity.this.formPlace.removeAllViews();

                    LinearLayout auxLinearView = new LinearLayout(FormActivity.this);
                    auxLinearView = makeEditText(arrayItems, position);
                    FormActivity.this.formPlace.addView(auxLinearView);
                }
                firstTime =false;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }

        }); 

Aucun commentaire:

Enregistrer un commentaire