如何在Kotlin中将我的值String转换为自定义类型?

时间:2018-10-13 08:18:29

标签: android kotlin sharedpreferences

我有一个Android应用,我需要使用kotlin将值保存在android的sharedpreferences中。下面的示例代码。

我的值是: AppdatabaseIMP @ 43b8de6 -------它是Appdatabase类型 所以我转换成这样的字符串 val APPDBSTR = appDatabase.toString()

保存:

val pref = cc.getApplicationContext().getSharedPreferences("MyPrefdd", 0)
val editor = pref.edit()
editor.putString("APPDBSTR", APPDBSTR)
editor.apply()

获取:

val pref = context!!.getSharedPreferences("MyPrefdd", 0)
val mFragAPPDBSTR = pref.getString("APPDBSTR", null)

在这里我可以得到值wihout问题,但我想将我的字符串重新转换为previus类型 如何在Kotlin中做到这一点

更新:

companion object {
        /**
         * new instance pattern for fragment
         */
        @JvmStatic
        fun newInstance(myObject: List<TransactionEntity>?, cc: Context, appDatabase: AppDatabase, networkDefinitionProvider: NetworkDefinitionProvider, incoming: TransactionAdapterDirection): SendingFragment {

            val gson = Gson()
            val gson1 = GsonBuilder().create()
            val model = myObject as List<TransactionEntity>
            val IT = gson.toJson(model)
            System.out.println("json representation :" + IT)

            val bo = ByteArrayOutputStream()
        val so = ObjectOutputStream(bo)
        so.writeObject(appdatabase)
        so.flush()
        val serializedObject = String(Base64.encode(bo.toByteArray()))

            val bundle = Bundle()
            bundle.putString("bundleValue", IT)
             bundle.putSerializable("serializedObject",serializedObject)
            val sendFragament: SendingFragment = SendingFragment()
            sendFragament.setArguments(bundle)
            return sendFragament
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        val gson = Gson()
        val gson1 = GsonBuilder().create()
        val pref = context!!.getSharedPreferences("MyPrefdd", 0)
        val mFragIT = pref.getString("NEWIT", "")

         val mFragserializedObject = arguments!!.getSerializable("serializedObject") --- i here i can the value 

        }

我收到此错误:

Caused by: java.io.NotSerializableException: com.crypto.wallet.data.AppDatabase_Impl
                                                                   at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
                                                                   at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
                                                                   at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
                                                                   at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
                                                                   at com.crypto.wallet.activities.SendingFragment$Companion.newInstance(SendingFragment.kt:52)
                                                                   at com.crypto.wallet.activities.MainActivity.setupViewPager(MainActivity.kt:420)
                                                                   at com.crypto.wallet.activities.MainActivity.access$setupViewPager(MainActivity.kt:70)
                                                                   at com.crypto.wallet.activities.MainActivity$onCreate$outgoingTransactionsObserver$1.onChanged(MainActivity.kt:277)
                                                                   at com.crypto.wallet.activities.MainActivity$onCreate$outgoingTransactionsObserver$1.onChanged(MainActivity.kt:70)
                                                                   at android.arch.lifecycle.LiveData.considerNotify(LiveData.java:109)
                                                                   at android.arch.lifecycle.LiveData.dispatchingValue(LiveData.java:126)
                                                                   at android.arch.lifecycle.LiveData.setValue(LiveData.java:282)
                                                                   at android.arch.lifecycle.LiveData$1.run(LiveData.java:87)
                                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                   at android.os.Looper.loop(Looper.java:135)

如何反序列化?

1 个答案:

答案 0 :(得分:2)

调用toString时到达的只是内存中对象的哈希。它不包含实际的对象值,因此无法通过这种方式还原。

  

https://developer.android.com/reference/java/lang/Object.html#toString()
  Object类的toString方法返回一个字符串,该字符串包括该对象为实例的类的名称,符号符“ @”和该对象的哈希码的无符号十六进制表示形式。

您需要序列化整个对象才能使恢复工作。根据您的班级,这可能像扩展Serializable并获得String一样简单,如以下答案所述:https://stackoverflow.com/a/8887244/4193263

ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream so = new ObjectOutputStream(bo);
so.writeObject(myObject);
so.flush();
String serializedObject = new String(Base64.encodeBase64(bo.toByteArray()));