1

これが私の問題です。レイアウトを設定し、テーブルレイアウト(スクロールビューではそれ自体)に行を追加する簡単なアクティビティがあります。これらのテーブル行には、テキストフィールドとトグルボタンを備えたカスタムレイアウトがあります。

各トグルボタンにはデータベースから取得した値があり、最初にアクティビティを作成したときの値はOKです。しかし、デバイスを回転させてから向きを変えると、すべてのトグルボタンが「false」の値になります。Logcatで設定した値を出力しましたが、値は適切な値です(データベース内の値)。

欲しいレイアウトのようなものが別のレイアウトの後ろに隠れていると思いましたが、いくつかのテストを行ったところ、テキストフィールドが新しい値で変更されたため、トグルボタンが機能しない理由がよくわかりません。

コードは次のとおりです:TableRowレイアウト:

<?xml version="1.0" encoding="utf-8"?>
    <TableRow xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         >
         <RelativeLayout 
             android:id="@+id/relativelayout_row_parametres"
             android:layout_width="fill_parent"
             android:layout_height="50dp"
             android:layout_weight="1.0">
            <TextView 
                android:id="@+id/textview_row_parametres"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="@android:color/black"
                android:textStyle="bold"
                android:textSize="20sp"
                android:layout_marginLeft="2dp"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                />
            <ToggleButton
                android:id="@+id/togglebutton_row_parametres"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="2dp"
                android:textOn="@string/togglebutton_on"
                android:textOff="@string/togglebutton_off" />
        </RelativeLayout>
    </TableRow>

アクティビティレイアウト:

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical">

        <ImageView style="@style/header" />

        <LinearLayout 
            android:layout_width="fill_parent" android:layout_height="0dp"
            android:layout_weight="0.8"
            android:background="@drawable/gradient_bg"
            android:padding="10dip"
            android:orientation="vertical">

          <ScrollView
            android:layout_width="fill_parent" android:layout_height="0dp"
            android:layout_weight="0.85"
            android:fillViewport="true"
            android:padding="10dip"
            android:layout_gravity="center">

              <TableLayout 
                  android:id="@+id/tablelayout_parametres"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:background="@drawable/cornered_bg"
                  android:paddingTop="5dip"
                  android:paddingBottom="5dip">

              </TableLayout>

          </ScrollView>
          <Button 
              android:id="@+id/button_parametres_accept"
              android:gravity="center"
              android:text="@string/accept_changes"
              android:layout_width="wrap_content"
              android:layout_height="0dp"
              android:layout_weight="0.15"
              android:layout_gravity="center_horizontal" />
          </LinearLayout>
    </LinearLayout>

そしてアクティビティコード:

public class Parameters extends Activity {

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final Map<String, Boolean> changes = new HashMap<String, Boolean>();
        final Context ctx = getApplicationContext();
        LanguageManager.updateConfig(this);

        setContentView(R.layout.parametres);

        CountryDB[] countries = Database.instance(getApplicationContext()).getCountries();

        TableLayout tabLayout = (TableLayout) findViewById(R.id.tablelayout_parametres);
        for(int i =0; i<countries.length; i++){
            TableRow newRow = (TableRow) getLayoutInflater().inflate(R.layout.row_parametres, null);
            TextView textView = (TextView) newRow.findViewById(R.id.textview_row_parametres);

            ToggleButton toggleButton = (ToggleButton) newRow.findViewById(R.id.togglebutton_row_parametres);
            toggleButton.setChecked(countries[i].isToSynchronize());
            toggleButton.setTag(countries[i]);
            Log.e("setChecked",""+toggleButton.getId()+"/"+countries[i].isToSynchronize());

            textView.setText(countries[i].getLabel());

            toggleButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    CountryDB countryTemp = (CountryDB) v.getTag();
                    changes.put(countryTemp.getLabel(), ((ToggleButton)v).isChecked());
                }
            });

            tabLayout.addView(newRow);

            TableRow rowDivider = (TableRow) getLayoutInflater().inflate(R.layout.row_divider, null);
            tabLayout.addView(rowDivider);
        }

        Button buttonValidation = (Button) findViewById(R.id.button_parameters_accept);
        buttonValidation.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Iterator<String> iterator = changes.keySet().iterator();
                while(iterator.hasNext()){
                    String stringTemp = iterator.next();
                    Database.instance(ctx).updateCountry(stringTemp, changes.get(stringTemp));
                }

                Intent intent = new Intent(ctx, Splash.class);

                String result = "restart";
                String from = "parameters";
                Intent returnIntent = new Intent();
                returnIntent.putExtra("result", result);
                returnIntent.putExtra("from", from);
                setResult(RESULT_OK, returnIntent);

                startActivity(intent);
            }
        });

    }

}

Log.eで、値を出力しますが、値は良好です。トグルボタンの表示が間違っており、すべて「false」です。

ご協力いただきありがとうございます。

4

3 に答える 3

2

onCreate() と onResume() の間で、Android はトグル ボタンの古い状態を復元しようとします。一意の ID がないため、Android は成功せず、すべてが再び false になります。setChecked() 呼び出しを onResume() に移動してみてください (おそらく onStart() も機能します)。

これは同じ質問に対するかなり良い答えです:

向きが変更されたときのトグルボタンの状態の変更

于 2013-02-20T08:59:54.523 に答える
-1

より簡単な解決策があります。ここで説明するように、アクティビティ宣言にconfigChangesプロパティを追加するだけで済みます。このようにして、向きが変わったときにアクティビティが再開するのを防ぐことができます。したがって、マニフェストには次のようなものが必要です

<activity android:name=".Parameters"
      android:configChanges="orientation|keyboardHidden">

または、buildTarget>=13の場合

<activity android:name=".Parameters"
      android:configChanges="orientation|keyboardHidden|screenSize">

編集:以下のコメントで報告されているように、これは最適なソリューションではありません。主な欠点は、上記のリンク先のドキュメントのメモに報告されています。

注:構成の変更を自分で処理すると、システムが自動的にそれらを適用しないため、代替リソースの使用がはるかに困難になる可能性があります。この手法は、構成の変更による再起動を回避する必要がある場合の最後の手段と見なす必要があり、ほとんどのアプリケーションには推奨されません

于 2013-02-20T09:05:41.690 に答える
-1

Orientation の前にデータを保存する必要があります。

AndroidにはメソッドがonSaveImstamceState(Bundle outState)あり、onRestoreInstanceState(BundleInstaceState)

Activity でこれらのメソッドをオーバーライドします。

于 2013-02-20T08:46:02.977 に答える