0

私はこのようなスピナーを持っています:

// Spinner 1


final Spinner plan = (Spinner) dialog.findViewById(R.id.spinner1);
strings = getResources().getStringArray(R.array.paymentplan);
sAdapter = new SpinnerAdapter(this,
        android.R.layout.simple_spinner_item, strings);
sAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
plan.setAdapter(sAdapter);

// plan.setAdapter(spinner1Adapter);
plan.setSelection(prefsDisplay.getInt("spinnerSelection1", 0));
plan.setOnItemSelectedListener(new MyOnItemSelectedListenerPlan());

ユーザーがクリックすると、状態を保存したい:

  public void onClick(View v) {
                Editor editor2 = prefsPlan.edit();
                int selectedPosition1 = plan.getSelectedItemPosition();
                editor2.putInt("spinnerSelection1", selectedPosition1);
                editor2.commit();


}

位置を SharedPref に保存しますが、スピナーはデフォルトに戻ります。ここで何か見える人はいますか?

4

4 に答える 4

1

あなたが保管しているspinnerSelection

editor1.putInt("spinnerSelection", selectedPosition);

アクセスspinnerSelection1

prefsDisplay.getInt("spinnerSelection1", 0)

それらを一貫させます。

アップデート

にアクセスしているときplan.getSelectedItemPosition()。スピナーが表示されますか?NOだと思います。

選択した位置にパブリック変数を配置してみてください。で更新selected positionしますMyOnItemSelectedListenerPlan。そして、その位置を共有設定に保存します。私はそれがあなたの問題を解決すると思います。

于 2012-12-06T03:50:12.420 に答える
1

保存する:

int selectedPosition = yourSpinner.getSelectedItemPosition()
editor.putInt("spinnerSelection", selectedPosition);
editor.commit();

ロードする:

 yourSpinner.setSelection(prefs.getInt("spinnerSelection",0));

配列を使用している場合は、このように変更する必要があります

String selectedString = yourArray[yourSpinner.getSelectedItemPosition()];
editor.putString("spinnerSelection", selectedString);
editor.commit();

array[i] を prefs.if に格納されている値に対してチェックします。代わりに ArrayList を使用する場合、この部分はループなしで呼び出すことができます

ArrayList.indexOf(prefs.getString("spinnerSelection", "");

コミットすると、上記の配列項目がすべて表示されなくなります。配列に誰も表示しません。

于 2012-12-06T04:11:07.177 に答える
1

以下のコードを試して、最初に以下のコードをonItemSelectedListener()使用して現在選択されている項目の位置を 1 つの整数変数に保存し、その後、この変数の値を共有設定に保存します。

値を 1 つの変数に格納します。

int index;

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
    // Here Position is Item Index
    index = position;
}

価値を共有設定に保存します。

SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putInt("SelectedIndex", index);
prefsEditor.commit();

詳細については、以下のリンクを参照してください。

Android スピナー

Android 共有設定

于 2012-12-06T05:32:39.673 に答える
0

環境設定に保存した後、さらに使用するために、選択したアイテムをスピナーに設定する必要があります

なので

int pos = prefsDisplay.getInt("spinnerSelection", "0");
display.setSelection(pos);

しかし、あなたは使用していますspinnerSelection1。so デフォルトでは、設定に一致するものがない場合。デフォルト値が返されます。ここでは0が返され、スピナーが最初の位置に設定されます

于 2012-12-06T03:52:07.153 に答える