添加&将行删除到片段中的TableLayout

时间:2014-09-08 13:46:52

标签: android android-fragments android-tablelayout

我在根据所选项目添加/删除表单元素时遇到问题。

以下是我的聆听者类中rtSpinner元素的内容:

     selectedRamp = rtSpinner.getSelectedItemPosition();
     rampImage = (ImageView) getActivity().findViewById(R.id.rampImageView);
     dimTable = (TableLayout) getActivity().findViewById(R.id.dimTable);

            switch (selectedRamp)

            {
                case 0: // one ramp, one crossing
                    rampImage.setImageResource(R.drawable.oneramponecross);
                    rampDimYY = (EditText) getActivity().findViewById(R.id.rampDimYY);
                    rampDimZZ = (EditText) getActivity().findViewById(R.id.rampDimZZ);
                    rowYY = (TableRow) getActivity().findViewById(R.id.tableRowYY);
                    rowZZ = (TableRow) getActivity().findViewById(R.id.tableRowZZ);
                    rowYY.addView(rampDimYY);
                    rowZZ.addView(rampDimZZ);
                    dimTable.addView(rowYY);
                    dimTable.addView(rowZZ);
                    break;

正如您所看到的,dimTable是TableLayout,每个维度都有自己的TableRow,一旦选择了特定项目,我就会尝试添加它。我打开特定片段时遇到错误,这里是logcat:

09-08 09:40:53.107    2944-2944/com.josh.cs_4401app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.josh.cs_4401app, PID: 2944
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
        at android.view.ViewGroup.addView(ViewGroup.java:3415)
        at android.view.ViewGroup.addView(ViewGroup.java:3360)
        at android.view.ViewGroup.addView(ViewGroup.java:3336)
        at com.josh.cs_4401app.RampTypeChooserFragment$1.onItemSelected(RampTypeChooserFragment.java:92)
        at android.widget.AdapterView.fireOnSelected(AdapterView.java:893)
        at android.widget.AdapterView.access$200(AdapterView.java:48)
        at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

我可以看到第92行的addView出现错误(dimTable.addView(rowYY);)但我不确定如何解释/修复它。

到目前为止,我已经引用了add view dynamically in a fragmentHow to dynamically add a TableRow in a Fragment?,但我觉得它们可能不适合片段使用。

编辑:

好的,我发现了我的问题。我试图将字段设置为XML布局中定义的预先存在的字段,但它们必须在片段类中创建。我删除了XML的东西,并将我的课改成了这个,它起作用了:

selectedRamp = rtSpinner.getSelectedItemPosition();
            rampImage = (ImageView) getActivity().findViewById(R.id.rampImageView);
            dimTable = (TableLayout) getActivity().findViewById(R.id.dimTable);

            switch (selectedRamp)

            {
                case 0: // one ramp, one crossing
                    rampImage.setImageResource(R.drawable.oneramponecross);
                    TextView rowYYview = new TextView(getActivity());
                    TextView rowZZview = new TextView(getActivity());
                    rowYYview.setText("YY: ");
                    rowZZview.setText("ZZ: ");
                    rowYYview.setTextSize(20);
                    rowZZview.setTextSize(20);
                    EditText rampDimYY = new EditText(getActivity());
                    EditText rampDimZZ =  new EditText(getActivity());
                    TableRow rowYY = new TableRow(getActivity());
                    TableRow rowZZ = new TableRow(getActivity());
                    rowYY.addView(rampDimYY);
                    rowYY.addView(rowYYview);
                    rowZZ.addView(rampDimZZ);
                    rowZZ.addView(rowZZview);
                    dimTable.addView(rowYY);
                    dimTable.addView(rowZZ);
                    break;

0 个答案:

没有答案