如何从Kotlin中的Serializable类获取值?

时间:2018-10-15 11:42:58

标签: android kotlin getter-setter

我有一个fragment正在处理objects。所以我需要将值Instance发送到OnCreateview(),以便转换我的可序列化表格,所以我用getter / setter创建这些类

BeanDemo.kt

class BeanDemo : Serializable {

    var MyAppDatabase: AppDatabase ? = null

    constructor() {

    }

    //secoutry constructor

    constructor(appDatabase: AppDatabase){
        this. MyAppDatabase =  appDatabase
        Log.d("appDatabase : Bean", "appDatabase$appDatabase")
    }

    //getter/setter methods

    fun getName(): AppDatabase? {
        Log.d("appDatabase : getName", "appDatabase$MyAppDatabase")
        return MyAppDatabase
    }

    fun setName(NEWAPPDB: AppDatabase) {
        Log.d("appDatabase : NEWAPPDB", "appDatabase$NEWAPPDB")
        MyAppDatabase = NEWAPPDB
    }
}

SendingFragment.kt

class SendingFragment: Fragment(),Serializable
{

    private var linearLayoutManager: LinearLayoutManager? = null

    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 sampleVar = BeanDemo(appDatabase)
            sampleVar.setName(appDatabase)

            val bundle = Bundle()
            bundle.putSerializable("serializedObject",sampleVar)
            val sendFragament = SendingFragment()
            sendFragament.arguments = bundle
            return sendFragament
        }
    }


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

        val beanDemo: BeanDemo
        val bundle = arguments
        beanDemo = bundle!!.getSerializable("serializedObject") as BeanDemo
        val name = beanDemo.getName()

        // Inflate the layout for this fragment
        val rootView = inflater.inflate(R.layout.send_fragment, container, false)
        val recyclerView = rootView.findViewById<RecyclerView>(R.id.transaction_recycler_out) as RecyclerView
        linearLayoutManager = LinearLayoutManager(activity, LinearLayout.VERTICAL, false)
        recyclerView.layoutManager = linearLayoutManager
        //recyclerView.adapter = DuplicateTransactionRecyclerAdapter(NewIT,NewAPPDB,NewINCOMTYPE,NewNETWORKPROVSTR)
        recyclerView.setHasFixedSize(true)
        return rootView
    }
}

问题是我可以在片段的类的实例中设置值,但是我无法在OnCretaview()中获取数据。返回空值。请指导我我犯错了。提前谢谢

更新

@Database(entities = {AddressBookEntry.class, Token.class, Balance.class, TransactionEntity.class}, version = 1)
@TypeConverters({RoomTypeConverters.class})
public abstract class AppDatabase extends RoomDatabase implements Serializable {

    public abstract AddressBookDAO getAddressBook();

    public abstract TokenDAO getTokens();

    public abstract TransactionDAO getTransactions();

    public abstract BalanceDAO getBalances();
}

1 个答案:

答案 0 :(得分:0)

如果类AppDatabase本身不是可序列化的,则无法对BeanDemo进行序列化。

还要考虑到,当在kotlin中使用类似Bean的对象时,可以使用数据类(https://kotlinlang.org/docs/reference/data-classes.html)来避免编写太多样板。通过这样做:

data class BeanDemo(var MyAppDatabase: AppDatabase? = null) : Serializable()

除其他外,您还可以使用:setter,getter,equals(),hashCode()和toString()明智的实现。

如果您使用的是Android,请考虑使用Parcelable和“ Kotlin Android扩展”(此处的文档:https://kotlinlang.org/docs/tutorials/android-plugin.html)来使用Bundles。