好吧,我正在嘗試進行設置活動,但這樣做並不順利。現在它只有一個顯示爲Spinner的設置。嘗試從SharedPreferences中設置元素狀態,但僅應用默認值
我已成功將所選項目保存到SharedPreferences,但我無法弄清楚如何使活動將保存的數據應用於開始的視圖。 我想要應用任何int與關鍵「spinnerPosition」配對Spinner.setSelection();
,但默認值sharedPreferences.getInt(String key, int defValue);
覆蓋它。
這裏是文件sharedPreferences保存到:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <string>[email protected]</string> <string name="finalWeighType">Pounds</string> <int name="spinnerPosition" value="1" /> </map>
和一些logcat的:
04-04 18:21:13.849: D/spinner Listener(9027): ok 04-04 18:21:15.035: D/onPause(9027): Saving Settings 04-04 18:21:15.035: I/System.out(9027): iSavedPos: 9 04-04 18:21:19.030: W/InputEventReceiver(9027): Attempted to finish an input event but the input event receiver has already been disposed. 04-04 18:21:19.036: I/System.out(9027): iSavedPos 9 04-04 18:21:19.037: D/Do(9027): running 04-04 18:21:19.037: D/pos def(9027): ok 04-04 18:21:19.404: D/spinner Listener(9027): ok
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class SettingsActivity extends ActionBarActivity{
private Spinner selectWeighType;
SharedPreferences weighTypeLoader;
String finalWeighType;
public static final String DEFAULT = "UNSET";
public int itemSelectedPosition;
SharedPreferences getItemSavedPosition;
private int itemSavedPosition;
private boolean populated = false;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// add items to selectWeighType Spinner
populateSelectWeighType();
// add listener to selectWeightType Spinner
selectWeighTypeListener();
weighTypeLoader = getSharedPreferences("weightType", Context.MODE_PRIVATE);
//Load saved data.. hopefully
getItemSavedPosition = getSharedPreferences("weightType", Context.MODE_PRIVATE);
itemSavedPosition = getItemSavedPosition.getInt("spinnerPosition", 9);
System.out.println("iSavedPos " + itemSavedPosition);
}
@Override
protected void onResume() {
super.onResume();
do {Log.d("Do", "running");
switch (itemSavedPosition) {
case 0:
selectWeighType.setSelection(0);
Log.d("pos 0", "ok");
break;
case 1:
selectWeighType.setSelection(1);
Log.d("pos 1", "ok");
break;
case 2:
selectWeighType.setSelection(2);
Log.d("pos 2", "ok");
break;
default:
selectWeighType.setSelection(1);
Log.d("pos def", "ok");
}
populated = true;
}while(populated == false);
}
public void populateSelectWeighType(){
selectWeighType = (Spinner) findViewById(R.id.weight_type_spinner);
ArrayAdapter<CharSequence> selectWeighTypeAdapter =
ArrayAdapter.createFromResource(this,
R.array.weight_type_select,
android.R.layout.simple_spinner_item);
selectWeighTypeAdapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
selectWeighType.setAdapter(selectWeighTypeAdapter);
}
public void selectWeighTypeListener(){
selectWeighType = (Spinner) findViewById(R.id.weight_type_spinner);
selectWeighType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String weighTypeSelected = parent.getItemAtPosition(position).toString();
itemSelectedPosition = position;
int rowID = (int) id;
Log.d("spinner Listener", "ok");
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_settings, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
Toast weightTypeToast;
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences commitWeighSetting = getSharedPreferences("WeightType", Context.MODE_PRIVATE);
SharedPreferences.Editor editSetting = commitWeighSetting.edit();
editSetting.putString("WeighType",
selectWeighType.getSelectedItem().toString());
editSetting.putInt("spinnerPosition", itemSelectedPosition);
editSetting.commit();
Log.d("onPause", "Saving Settings");
System.out.println("iSavedPos: " + itemSavedPosition);
}
}
佈局XML
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.SettingsActivity"
>
<TextView
android:id="@+id/weight_type_setting_title"
android:text="@string/weight_type_setting_title"
android:textSize="18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/weight_type_spinner"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center_horizontal"
android:layout_row="0"
android:layout_column="0"
android:layout_columnWeight="0"
android:layout_marginLeft="16dp" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/weight_type_spinner"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/weight_type_setting_title"
android:layout_toEndOf="@+id/weight_type_setting_title"
android:layout_row="0"
android:layout_column="1"
android:spinnerMode="dropdown"
android:layout_columnWeight="0"
android:layout_marginRight="16dp"
android:layout_gravity="center_horizontal" />
</GridLayout>
哦,這當然是一個問題。謝謝你打開我的眼睛。我希望我能報告它解決了這個問題,但至少我可能更接近於解決它。 – 2015-04-04 23:35:46