将视图添加到linearlayout AlertDialog“你必须在孩子的父母上调用removeView()”

时间:2015-09-12 10:10:54

标签: android android-linearlayout alertdialog

麻烦的是,当我尝试将一个视图(innerLinearLayout)添加到linLayout时,我收到一个错误“你必须首先在孩子的父母上调用removeView()”。但是如果我调用linLayout.removeAllViews(),则会删除所有视图。但我想在主布局中添加多个innerLayouts。 innerLayout包含imageView和EditText。

 private void showDialog() throws IOException, GeneralSecurityException, ClassNotFoundException {
    final LinearLayout linLayout=new LinearLayout(ViewPagerSampleActivity.this);
    final Spinner spinner=new Spinner(ViewPagerSampleActivity.this);
    linLayout.addView(spinner);

    db = dbHelper.getWritableDatabase();
    final Cursor c = db.rawQuery("SELECT categories FROM CATEGORIES_TABLE", null);
    int i = 0;
    final String categoriesArray[] = new String[c.getCount()];
    while (c.moveToNext()) {
        categoriesArray[i] = c.getString(c.getColumnIndex("categories"));
        i++;
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categoriesArray);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    AlertDialog.Builder alert=new AlertDialog.Builder(ViewPagerSampleActivity.this);
    alert.setView(linLayout);
    alert.setTitle("Add category");
    alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int whichButton) {
            try {
                positiveButtonClickListener(spinner.getSelectedItem().toString());
            } catch (GeneralSecurityException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    });
    alert.setNegativeButton("Отмена", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            linLayout.removeAllViews();
        }
    });

    alert.show();

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
            ImageView imageView=new ImageView(getApplicationContext());
            imageView.setImageResource(R.drawable.save);
            final Cursor c = db.rawQuery("SELECT * FROM '" + myName + "'", null);

            final String mas[] = c.getColumnNames();
            c.close();
            LinearLayout innerLinearLayout=new LinearLayout(getApplicationContext());

            innerLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
            for (int m = 1; m < mas.length; m++) {
                StringBuilder stringBuilder = new StringBuilder(mas[m]);
                for (int z = 0; z < stringBuilder.length(); z++) {
                    if (stringBuilder.charAt(z) == '_') stringBuilder.setCharAt(z, ' ');
                }
                EditText editText = new EditText(getApplicationContext());
                editText.setTextColor(Color.parseColor("#000000"));
                editText.setHintTextColor(Color.parseColor("#727272"));
                list.add(editText);
                list.get(m - 1).setHint(stringBuilder.toString());
                innerLinearLayout.removeAllViews();
                innerLinearLayout.addView(list.get(m - 1));
                innerLinearLayout.addView(imageView);
                linLayout.addView(innerLinearLayout); //here is the problem
            }
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });
    alert.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            linLayout.removeAllViews();

        }
    });
}

请帮帮我们。

1 个答案:

答案 0 :(得分:0)

我已经解决了。问题是每个视图必须具有唯一的ID。 Desicion是为周期中的每个视图设置ID。像

editText.setId(View.generateViewId());

对于布局:

LinearLayout innerLinearLayout=new LinearLayout(getApplicationContext());
                    innerLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
                    innerLinearLayout.setId(View.generateViewId());