0

Java内で作成されたテーブル行で膨らんだテーブルレイアウトがあります。これで、各行に RadioButton が必要になります。クリックするたびにテーブル行を追加するボタンがあります。行を追加するときは、ラジオ ボタンを 1 つだけオンにする必要があります。ご覧のとおり、RadioButtons は実行時にオンザフライで追加されます。だから私が尋ねるのは、RadioButton の選択を一意に保つ方法です。ラジオ ボタンで 1 つの行だけをチェックする必要があります。

これは、この ASCII アート スケッチで私が言いたいことです。

TableRow EditText (RadioButton) Spinner ../TableRow

TableRow EditText (RadioButton) Spinner ../TableRow

TableRow EditText (RadioButton) Spinner ../TableRow

TableRow EditText (RadioButton) Spinner ../TableRow

TableRow EditText (RadioButton) Spinner ../TableRow

私が恐れているのは、RadioButtons がグループ化されないことです。それが私が探しているものです: スケッチのように RadioButtons をグループ化します。

4

1 に答える 1

0

私が実際にやろうとしているのは、Sqlite SQL 文 "Create Table" の独自のフォームを作成することです。フォームが 1 つのテーブル フィールドのみをプライマリとして受け入れるようにすることにしました。sqlite が複合 (複数列) 主キーを作成できることは、後になって初めて知りました。とにかく、StackOverFlow で共有するためにこれを行っても問題ありません。

まあ.. Map データ構造で TableRow-view-ids と RadioButtons-view-ids を追跡することで解決しました。選択した RadioButton の値を返すには、各 RadioButton の onClickListener イベント/メソッドによって設定される Activity クラスのグローバル/メンバー変数を使用する必要がありました。

もちろん、TableRow と RadioButton のビュー ID ジェネレーター メソッドを見つける必要がありました。ここの StackOverFlow にあります。すべての TableRow とそこにあるすべての RadioButton に対して、ビュー ID ジェネレーターを使用してビュー ID を設定します。このようにして、ディスプレイ上のすべての TableRow とすべての RadioButton を追跡できます。

//put this class in a separate Java source code file.
//so you can access it any time from anywhere in your project.
public class UtilityClass {

    private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);


    @SuppressLint("NewApi")
    public static int generateViewId() {

        if (Build.VERSION.SDK_INT < 17) {
            for (;;) {
                final int result = sNextGeneratedId.get();
                // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
                int newValue = result + 1;
                if (newValue > 0x00FFFFFF)
                    newValue = 1; // Roll over to 1, not 0.
                if (sNextGeneratedId.compareAndSet(result, newValue)) {
                    return result;
                }
            }
        } else {
            return View.generateViewId();
        }

    }
}

これは私のFAKE RadioGroup です。はじめましょう..

そこで、この XML レイアウトから始めます。混乱しないように、残りのウィジェットを削除して簡単にします。XML のコメントを読んでください。

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!--Button to add a new TableRow at runtime every time it gets clicked -->
    <Button
        android:id="@+id/add_new_field_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/add_new_field"
        android:textSize="12sp" />

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

        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TableLayout
                android:id="@+id/table_fields_tablelayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

             <!-- New Dynamically added TableRows will be added to below this TableRow @+id/field_headers_tablerow -->
                <TableRow
                    android:id="@+id/field_headers_tablerow"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

            <!-- Both TextViews here represent table headers only-->

            <!--Below this TextView there will be table field names -->

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="10dp"
                        android:text="@string/field_name"
                        android:textSize="12sp" />


            <!--Below this TextView there will be RadioButtons -->

                     <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="10dp"
                        android:text="@string/primary_key"
                        android:textSize="12sp" />

                </TableRow>

            </TableLayout>
        </ScrollView>
    </HorizontalScrollView>

</TableLayout>

次に、Java コードに進みます。コメントを読んでください。変数名の多くは、できれば明確です。それらにコメントを付ける必要はありません。

/**
 * 
 */
package net.superlinux.sqlitestudio;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TableLayout;
import android.widget.TableRow;

/**
 * @author oracle
 *
 */
public class CreateTableForm extends Activity {

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */

    /*
     * The Create Table form layout to create an SQLite table and to generate the SQL "Create Table" statement
     * 
     * TableRow EditText (RadioButton) Spinner ../TableRow
     * TableRow EditText (RadioButton) Spinner ../TableRow
     * TableRow EditText (RadioButton) Spinner ../TableRow
     * TableRow EditText (RadioButton) Spinner ../TableRow
     * .
     * .
     * .
     * TableRow EditText (RadioButton) Spinner ../TableRow
     * 
     * (RadioButton) is 
     */
    Button add_new_field_button;
    TableLayout table_fields_tablelayout;

    //this will hold tablerow_view_id of the corresponding checked radiobutton 
    int checked_primary_key_radio_button_view_id=0;

    //the mapping is the generated tablerow_view_id vs it's corresponding field name values.
    //each programmatically generated <TableRow> will mean a table field in the SQL sentence of 'Create Table ...' 
    Map<Integer, String> field_names_list=new HashMap<Integer, String>();

    //each primary key is having a radio button. it will have a generated viewid. 
    //this view id is set as the value side of the Map field_names_is_primary_key_radiobuttons_view_ids whose key is tablerow_view_id
    //this is used to set the

    Map<Integer, Integer> field_names_is_primary_key_radiobuttons_view_ids=new HashMap<Integer, Integer>();


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_table_form); // TODO Auto-generated
                                                    // method stub
         add_new_field_button=(Button)findViewById(R.id.add_new_field_button);

         table_fields_tablelayout=(TableLayout)findViewById(R.id.table_fields_tablelayout);

         add_new_field_button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                //Prepare a TableRow to be inflated.
                TableRow tr=new TableRow(CreateTableForm.this);
                tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                final int tablerow_view_id=UtilityClass.generateViewId();
                tr.setId(tablerow_view_id);

                //Adding field name
                final EditText field_name = new EditText(CreateTableForm.this);
                field_name.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                field_name.setTextSize(12);
                field_name.setHint(R.string.field_name);
                field_name.addTextChangedListener(new TextWatcher() {

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        // TODO Auto-generated method stub
                    field_names_list.put(tablerow_view_id, field_name.getText().toString());
                    }

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        // TODO Auto-generated method stub

                    }
                });
                tr.addView(field_name);


                //Create a new RadioButton
                RadioButton primary_key_radiobutton=new RadioButton(CreateTableForm.this);
                primary_key_radiobutton.setChecked(false);
                primary_key_radiobutton.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

                //generate the RadioButton a unique viewId
                int radiobutton_view_id=UtilityClass.generateViewId();
                primary_key_radiobutton.setId(radiobutton_view_id);

                //use this Map to say that this RadioButton belongs to this current TableRow 
                // this is done by referring to both using the view ids of each
                field_names_is_primary_key_radiobuttons_view_ids.put(tablerow_view_id, radiobutton_view_id);

                primary_key_radiobutton.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        //checked_primary_key_radio_button_view_id is a (global/Activity class member) variable. 
                        //And this area of the code is added to every RadioButton in the layout to set the event onClickListener.
                        //so whenever any of the RadioButton's gets clicked it will change the variable checked_primary_key_radio_button_view_id 
                        //and remeber that this will be a common value to the whole activity. 
                        //so this is how you get the return value of the selected RadioButton
                        checked_primary_key_radio_button_view_id=tablerow_view_id;

                        //this is used to monitor which radio is for which table row
                        Log.e("radio button at tablerow=", ""+tablerow_view_id);

                        //this is where the fake RadioGroup happens
                        //we uncheck all the rest of RadioButton's by using their view ids. we skip the current one. 
                        for (int tablerow_view_id_as_key : field_names_is_primary_key_radiobuttons_view_ids.keySet()){
                            int radiobutton_view_id_as_value=field_names_is_primary_key_radiobuttons_view_ids.get(tablerow_view_id_as_key);
                            if (v.getId()!=radiobutton_view_id_as_value){       
                                ((RadioButton)findViewById(radiobutton_view_id_as_value)).setChecked(false);
                                }
                        }

                    }
                });
                tr.addView(primary_key_radiobutton);


                table_fields_tablelayout.addView(tr);               

            }
        });
    }

}
于 2014-10-01T15:48:40.670 に答える