1

i'm trying to add several LinearLayouts with 2 Buttons and one Spinner. If i add this LinearLayout by clicking a button in the activity it works perfect, but if i want the activity to add this like two or three times in calling the method in the onCreate Method, nothing will shown.

Here's my Code:

  private void insert() {

        ll = new LinearLayout(this);

        spinner = new Spinner(this);
        ArrayAdapter<CharSequence> adapter;

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        Button badd = new Button(this);
        badd.setText("+");
        Button bdel = new Button(this);
        bdel.setText("-");


        ll.addView(bdel);
        ll.addView(spinner);
        ll.addView(badd);
        scrollerLL.addView(ll);

     }

EDIT:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_config);

    dbHelper = new DatabaseHelper(this);

    durchflussmenge = new ArrayList<Double>();

    bSave = (Button) findViewById(R.id.bSave);

    editV = (EditText)findViewById(R.id.editV);
    spinnerDruck = (Spinner)findViewById(R.id.spinnerDruck);
    editZB = (EditText)findViewById(R.id.editZB);

    labelErg = (TextView)findViewById(R.id.labelErg);

    addNozzleList = new ArrayList<LinearLayout>();

    spinnerDT = (Spinner)findViewById(R.id.spinnerDT);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.dt_entries, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerDT.setAdapter(adapter);
    spinnerDT.setPrompt("DT auswählen ...");


    spinnerDruck.setAdapter(adapterDruck);

    bSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (status == 0) { 
                if (!addNozzleList.isEmpty() && !editV.getText().toString().matches("") && !editZB.getText().toString().matches(""))
                    initiatePopupWindow();
            }
        }
    });

    scrollerLL = (LinearLayout)findViewById(R.id.addNozzleLL);

    btnAddNoozle = (Button)findViewById(R.id.btnAddNozzle);
    btnAddNoozle.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            insert();

        }

    });

    spinnerDT.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (!addNozzleList.isEmpty()) {
                int length = scrollerLL.getChildCount();
                scrollerLL.removeViews(1, length - 1);
                addNozzleList.clear();
                durchflussmenge.clear();
                labelErg.setText("0.0");
            }

            if(spinnerDT.getSelectedItem().toString().equals("ATR")) {
                String[] field = CreateConfigActivity.this.getResources().getStringArray(R.array.druck_atr);
                adapterDruck.clear();
                for(int i = 0; i < field.length; i++)
                    adapterDruck.add(field[i]);
                adapterDruck.notifyDataSetChanged();
                spinnerDruck.setSelection(0);
                Log.d("SpinnerChange", "ATR");

            }else{
                String[] field = CreateConfigActivity.this.getResources().getStringArray(R.array.druck_iso);
                adapterDruck.clear();
                for(int i = 0; i < field.length; i++)
                    adapterDruck.add(field[i]);
                adapterDruck.notifyDataSetChanged();
                spinnerDruck.setSelection(0);
                Log.d("SpinnerChange", "ISO");
            }
        }

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

        }
    });




    Bundle extras = getIntent().getExtras();
    if(extras == null) {
        status = 0;
    }else {
        status = 1;
        fillFields(extras.getString("NozzleConfigName"));
    }

}

private void fillFields(String nozzleConfigName) {
    nozzleConfig = dbHelper.getNozzleConfig(nozzleConfigName);
    nozzles = dbHelper.getNozzles(nozzleConfig.getId());

    if(!nozzleConfig.getType().equals("ISO"))
        spinnerDT.setSelection(1);
    editV.setText(nozzleConfig.getKmh()+"");
    editZB.setText(nozzleConfig.getBreite()+"");
    labelErg.setText(nozzleConfig.getAusbringmenge() + "");
    int spinnerDruckPosition = adapterDruck.getPosition(nozzleConfig.getDruck()+"");
    spinnerDruck.setSelection(spinnerDruckPosition);

    for(Nozzle n:nozzles) {
        insert();
        findViewById(R.id.createConfigRoot).invalidate();
    }

}

private void insert() {
    ll = new LinearLayout(this);

    spinnerNozzle = new Spinner(this);
    ArrayAdapter<CharSequence> adapter;

    if(spinnerDT.getSelectedItem().toString().equals("ATR")) {
        adapter = ArrayAdapter.createFromResource(this, R.array.nozzle_atr, android.R.layout.simple_spinner_item);
    }else{
        adapter = ArrayAdapter.createFromResource(this, R.array.nozzle_iso, android.R.layout.simple_spinner_item);
    }
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerNozzle.setAdapter(adapter);

    Button badd = new Button(this);
    badd.setText("+");
    Button bdel = new Button(this);
    bdel.setText("-");

    spinnerNozzle.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (!editV.getText().toString().matches("") && !editZB.getText().toString().matches("")) {
                //Toast.makeText(getApplicationContext(), "change", Toast.LENGTH_SHORT).show();
                if (spinnerNozzle.getSelectedItem().toString().equals("-005") || spinnerNozzle.getSelectedItem().toString().equals("weiss")) {
                    calculateAusbringmenge(spinnerNozzle.getSelectedItem().toString(), false);
                } else {
                    // vorheriges Element aus ArrayList löschen
                    calculateAusbringmenge(spinnerNozzle.getSelectedItem().toString(), true);
                }

            }
        }

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



    ll.addView(bdel);
    ll.addView(spinnerNozzle);
    ll.addView(badd);
    scrollerLL.addView(ll);
    addNozzleList.add(ll);

    badd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LinearLayout parent = (LinearLayout) v.getParent();
            Spinner spinnerValue = (Spinner) parent.getChildAt(1);

            copyNozzle(spinnerValue.getSelectedItemPosition());
        }
    });

    bdel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            LinearLayout parent = (LinearLayout) v.getParent();
            scrollerLL.removeView(parent);
            Log.d("addNozzleList.size()", parent + "");
            addNozzleList.remove(addNozzleList.size() - 1);
            Log.d("addNozzleList.size()", addNozzleList.size() + "");
            Toast.makeText(getApplicationContext(), "dasda", Toast.LENGTH_SHORT).show();
        }
    });

}
3
  • 1
    put your complete activity class here? Commented Mar 22, 2016 at 6:05
  • Show your complete code Commented Mar 22, 2016 at 6:05
  • if you want to show layout then you need to call setContentView(scrollerLL) in your activity. Commented Mar 22, 2016 at 6:06

2 Answers 2

1

My Code works. The Views were added right but in another method i deleted them directly after the initialization, so my own fail :P

Sign up to request clarification or add additional context in comments.

Comments

0

You need to set LayoutParameters to your LinearLayout as well as Buttons.

LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
ll..setLayoutParams(params);

and same for buttons.

LayoutParams BtnParams = new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
badd.setLayoutParams(BtnParams);
bdel.setLayoutParams(BtnParams);

I hope this will help you out.

12 Comments

Why should the same code work in a onClick Method of a Button but not in the onCreate of the Activity? It is exactly the same code :/
not now, unfortunately i can't try it before i am back home :/ But why should there be a difference?
see.. when you define your layouts in XML there you are giving hight and width to your view, and it is compulsory to give this parameters to view. now if you are adding view programmatically you also have to add those parameters programmatically . otherwise they won't show. as they don't have hight and width.
ok, you dont get my problem :/ situation: i got some Views (Buttons, EditTexts, ...) in my Activity initiated through XML layout file. One of these Buttons call in the onClick method the same method "insert" like in the onCreate of myActivity. In the onCreate method i could determine that the new LinearLayout is added as a child, but it will not shown. If i call this "insert" Method by clicking the button it works perfectly, the same code! i dont get the difference :/
well I don't know whats the problem then. if you can post your code then I might help you out.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.