如何将BigInteger []存储和检索到Preference中

时间:2015-04-10 08:33:49

标签: android sharedpreferences

我想将BigInteger[]存储到Preference,有人可以告诉我可靠的方法吗?我尝试了很多方法,但这似乎没有用。

private BigInteger[] array =  new BigInteger[20];

提前致谢

完整代码:

private BigInteger[] array =  new BigInteger[10];

for (int i = 0; i <= count; i++) {
    String[] bigString;
    bigString = new String[array.length];
    SharedPreferences sharedPreferences = context.getSharedPreferences("preferencetime", 0);
    Editor editor = sharedPreferences.edit();

    // Store
    for (int l = 0; l < array.length; l++) {
        bigString[l] = array[l].toString();
        editor.putString("BIG_STRING" + l, bigString[l]);
        editor.commit();
        Log.d("Prefs", "Time Saved : " + bigString[l]);
    }

    // Retrieve
    for (int m = 0; m < array.length; m++) {
        bigString[m] = array[m].toString();
        String temp = sharedPreferences.getString("BIG_STRING" + m, "Not found");
        //Toast.makeText(this, temp, Toast.LENGTH_SHORT).show();
        Log.d("Prefs", "Time retrieved : " + temp);
    }
}

1 个答案:

答案 0 :(得分:0)

试试这段代码:

public class StoreBigInteger extends Activity {

private SharedPreferences sharedPreferences;
private Editor editor;
private BigInteger[] bigInteger = new BigInteger[5];
private String[] bigString;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_store);
    bigString = new String[bigInteger.length];
    bigInteger[0] = new BigInteger("4254366747468425");
    bigInteger[1] = new BigInteger("4254366747468426");
    bigInteger[2] = new BigInteger("4254366747468427");
    bigInteger[3] = new BigInteger("4254366747468428");
    bigInteger[4] = new BigInteger("4254366747468429");
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    editor = sharedPreferences.edit();
}

public void onStore(View v) {
    for (int i = 0; i < bigInteger.length; i++) {
        bigString[i] = bigInteger[i].toString();
        editor.putString("BIG_STRING" + i, bigString[i]);
        editor.commit();
    }
}

public void onRetrieve(View v) {
    for (int i = 0; i < bigInteger.length; i++) {
        bigString[i] = bigInteger[i].toString();
        String temp = sharedPreferences.getString("BIG_STRING" + i, "Not Found");
        Toast.makeText(this, temp, Toast.LENGTH_SHORT).show();
    }
}}

将它放入xml:

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

<Button
    android:id="@+id/store"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onStore"
    android:text="store" />

<Button
    android:id="@+id/retreive"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onRetrieve"
    android:layout_below="@id/store"
    android:text="Retrieve" />