动态添加新的微调器

时间:2014-08-07 04:08:57

标签: android android-spinner

我有一个旋转器,并且有一个旋转器右侧的按钮。单击该按钮时,将在现有的微调器下创建新的微调器。它是在右侧的删除按钮创建的。

所以我的问题是: -

1)如果我添加了两个或三个新的微调器,我怎么能得到新微调器的值。因为如果我添加了多个新的微调器。该值将被全部替换。

2)当我点击删除按钮时,如何删除新微调器的值。

以下是我的代码:

btnAddRow.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                        final View addView = layoutInflater.inflate(R.layout.add_row, null);
                        spBidang2 = (Spinner) addView.findViewById(R.id.spSpecification2);

                        Button btnRemRow = (Button) addView.findViewById(R.id.btnRemRow);
                        btnRemRow.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                ((LinearLayout)addView.getParent()).removeView(addView);
                                spBidang2.setVisibility(View.GONE);
                            }
                        });

                        container.addView(addView);
                    }
                });

对于xml: -

<LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="5dp"
                        android:orientation="horizontal"
                        android:weightSum="2" >

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="1.25"
                            android:orientation="vertical" >

                            <TextView
                                android:layout_width="fill_parent"
                                android:layout_height="36dp"
                                android:background="#206531"
                                android:gravity="center"
                                android:text="Pengkhususan"
                                android:textColor="#ffffff" />

                            <Space
                                android:layout_width="match_parent"
                                android:layout_height="match_parent" />
                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="fill_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="0.75"
                            android:orientation="vertical"
                            android:weightSum="2" >

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_weight="0.5" >

                                <Spinner
                                    android:id="@+id/spSpecification"
                                    android:layout_width="fill_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_marginLeft="5dp"
                                    android:layout_weight="0.5"
                                    android:background="@drawable/field_shape"
                                    android:entries="@array/pengkhususan"
                                    android:singleLine="false" />

                                <Button
                                    android:id="@+id/btnAddRow"
                                    style="?android:attr/buttonStyleSmall"
                                    android:layout_width="fill_parent"
                                    android:layout_height="wrap_content"
                                    android:layout_weight="1.5"
                                    android:text="+" />
                            </LinearLayout>

                            <LinearLayout
                                android:id="@+id/container"
                                android:layout_width="fill_parent"
                                android:layout_height="match_parent"
                                android:orientation="vertical" >
                            </LinearLayout>
                        </LinearLayout>

这是新的微调器行的xml: -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="2" >

    <Spinner
        android:id="@+id/spSpecification2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_weight="0.5"
        android:background="@drawable/field_shape"
        android:entries="@array/pengkhususan"
        android:singleLine="false" />

    <Button
        android:id="@+id/btnRemRow"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.5"
        android:text="-" />

</LinearLayout>

3 个答案:

答案 0 :(得分:1)

为了获得新的微调器的价值,请使用ArrayList的{​​{1}} 例如,
Spinners

然后当您创建新ArrayList<Spinner> spinners = new ArrayList<Spinner>();时将其添加到列表中 即spinner

你可以通过
获得价值 spinners.add(newSpinner)

答案 1 :(得分:0)

更好地使用您需要的所有微调器创建布局,并使其可见性消失。只制作最初需要的,隐藏其他人,当你需要添加新行时,让它可见。通过这种方式,您可以使用其id访问微调器的值,甚至在单击右侧清除按钮时使其消失。

希望这会有所帮助:)

答案 2 :(得分:0)

参考:

http://www.edumobile.org/android/android-development/dynamic-spinner/

此示例将说明如何在运行时在android中创建带有列表视图的微调器。在此示例中,您可以在单击按钮时在运行时将微调器的内容分配给另一个微调器。

希望这可以帮到你!