【问题标题】:How to store and retrieve BigInteger[] into Preference [duplicate]如何将 BigInteger [] 存储和检索到 Preference [重复]
【发布时间】:2018-04-04 04:46:54
【问题描述】:

我想将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);
    }
}

【问题讨论】:

  • 请发布您尝试过的代码。
  • 看看here,这样的问题已经有了答案。

标签: android sharedpreferences


【解决方案1】:

试试这个代码:

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" />

【讨论】:

  • Vikalp : 我试过了,我可以保存但不能恢复
  • 我也试过了,它工作正常。发布您的代码,以便我可以看到真正发生的事情。
  • 我已经编辑了我的代码。谢谢
猜你喜欢
  • 1970-01-01
  • 2011-05-21
  • 1970-01-01
  • 1970-01-01
  • 2021-01-13
  • 2016-04-02
  • 2019-11-18
  • 2014-05-11
  • 1970-01-01
相关资源
最近更新 更多