5

私のアプリは調査の作成に関するものです。そのためには、ユーザーは新しいフィールドを動的に作成できる必要があります。

そのため、新しいアンケートを作成すると、アクティビティは 2 つのボタン (追加と削除) のみで表示されます。ユーザーが追加ボタンをクリックすると、EditText (質問を記述できるようにするため)、Spinner (ユーザーがフィールドのタイプを選択できるようにするため: text、RadioButton、CheckBox など)、および別の EditText (該当する場合、利用可能なさまざまな回答の選択肢が書かれています)。

これを実現するために、これらのウィジェットを含む XML ファイルを作成しました。ユーザーが追加ボタンをクリックするたびに、XML が拡張されます。最初にボタンを押したときは機能しますが、その後... 何も起こりません。

ループで行われない限り、親で同じ子を2回膨らませることはできないとどこかで読みました。

だから、私の質問は次のとおりです。

-> どうすればこれを回避し、目的の効果を得ることができますか?

-> これがループに適用されないのはなぜですか?

ヘルプやヒントをいただければ幸いです。繰り返しになりますが、コミュニティが私に与えてくれたすべてのサポートに感謝します。

注: Android のドキュメントでは、インフレータのクローン作成について説明していますが、そこにある情報は不足しており、それに関する他の情報は見つかりませんでした。

ソースコード:

XML - メインのコインテイナー:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:id="@+id/wrapper"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />

    <Button
        android:id="@+id/mds_new_addfield_button"
        android:tag="new"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add Field"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
    />
    <Button
        android:id="@+id/mds_new_deletefield_button"
        android:tag="delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete Field"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
    />

</RelativeLayout>

XML -膨張:

<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
>
    <TableRow>
        <TextView
            android:id="@+id/mdsnew_field_tv"
            style="@style/dsn_field"
            android:text="Field Name    " 
        />

        <TextView
            android:id="@+id/mdsnew_type_tv"
            style="@style/dsn_field"
            android:text="Type of Field   " 
        />

        <TextView
            android:id="@+id/mdsnew_choices_tv"
            style="@style/dsn_field"
            android:text="Choices"  
        />
    </TableRow>

    <TableRow>

        <EditText
            android:id="@+id/mdsnew_fieldname_et"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Field Name"
            android:textSize="18sp"
        />

        <Spinner
            android:id="@+id/mdsnew_type_sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />

        <EditText
            android:id="@+id/mdsnew_choices_et"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Choices    "
            android:textSize="18sp"
        />

    </TableRow>
</TableLayout>

追加ボタン:

LinearLayout myRoot = (LinearLayout) findViewById(R.id.wrapper);

LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

inflater.inflate(R.layout.mds_new_row, myRoot);
4

1 に答える 1

13

あなたの親は LinearLayout であり、垂直方向を意図しているように見えますが、これを指定していません。向きを設定してみて、それが役立つかどうかを確認してください。

于 2010-11-29T01:56:01.077 に答える