Android在OnCreate中以编程方式插入LinearLayout Views而不显示

时间:2016-03-22 06:02:29

标签: android android-layout

我正在尝试使用2个按钮和一个微调器添加多个LinearLayouts。如果我通过单击活动中的按钮来添加此LinearLayout,它可以完美地运行,但如果我希望活动在调用onCreate方法中的方法时添加两次或三次,则不会显示任何内容

这是我的代码:

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

     }

编辑:

@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();
        }
    });

}

2 个答案:

答案 0 :(得分:0)

您需要将LayoutParameters设置为LinearLayout以及Buttons

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

和按钮相同。

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

我希望这会帮助你。

答案 1 :(得分:0)

我的代码有效。视图是正确添加但在另一种方法中我在初始化后直接删除它们,所以我自己失败了:P