Android:动态地向Container视图添加视图

时间:2017-01-13 12:23:35

标签: android android-arrayadapter android-spinner

我正在动态创建行项目,当我点击按钮时,我会对edittexts行进行充气并将其添加到我的容器中。我在行中有微调器,对于特定的微调器,我正在设置一个数组适配器。

问题是当尝试使用微调器添加另一行时我需要使用相同的数组适配器集来设置第二行微调器,但这会产生一个问题,这会扰乱第一个微调器,因为它们都来自相同的xml文件。 / p>

如何为微调器提供多个id并将它们视为不同的微调器。

 addviewbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater layoutInflater =
                        (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                addView = layoutInflater.inflate(R.layout.quote_row, null);
                container.addView(addView);
                for (int i = 0; i < container.getChildCount(); i++) {
                    View view = container.getChildAt(i);


                    spinner = (Spinner) view.findViewById(R.id.product);

                    String[] items = new String[]{"1", "2", "three"};
                   ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, items);
                    spinner.setAdapter(adapter);

                }
            }
        });

quote_row.xml

<RelativeLayout 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">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="1">

            <TextView
                android:id="@+id/TextView04h" android:text="1"
                android:layout_weight="0.1"
                android:textColor="#000000"
                android:padding="10dp" android:gravity="center|center_vertical"
                android:layout_margin="2dp"
                android:layout_width="0dp"
                android:layout_height="wrap_content"/>

            <EditText
                android:id="@+id/quantity"
                android:layout_weight="0.1"
                android:textColor="#000000"
                android:padding="10dp" android:gravity="center"
                android:background="@drawable/edit_text_border"
                android:layout_margin="2dp"
                android:layout_width="0dp"
                android:layout_height="wrap_content"/>

            <Spinner
                android:id="@+id/product"
                android:layout_weight="0.4"
                android:textColor="#000000"
                android:padding="10dp" android:gravity="center"
                android:background="@drawable/edit_text_border"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="2dp"
                android:layout_width="0dp"
                android:layout_height="44dp"/>
            <EditText
                android:id="@+id/unitprice"
                android:layout_weight="0.2"
                android:textColor="#000000"
                android:padding="10dp" android:gravity="center"
                android:background="@drawable/edit_text_border"
                android:layout_margin="2dp"
                android:layout_width="0dp"
                android:layout_height="wrap_content"/>
            <EditText
                android:id="@+id/exactprice"
                android:layout_weight="0.2"
                android:textColor="#000000"
                android:padding="10dp" android:gravity="center"
                android:background="@drawable/edit_text_border"
                android:layout_margin="2dp"
                android:layout_width="0dp"
                android:layout_height="wrap_content"/>
        </TableRow>



    </TableLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

csentence= input("Please enter a sentance, ") 
split_sentence=sentence.split
words=[ ]

for x, y in enumerate(words,1):
    if split_sentence .count(y)>1:
        words.append(sentence_split.index(y+1))
    else:
        words.append(x+1)

print(words)
相关问题