4

i have fragment and i need to pass my values Constructor to OncretaeView() methods so i receive my values in constructor and tr to convert my objects to serialized one but it does not convert. Here is my code. Please guide me how to Serialize and deserialize my objects . Thanks in advance

import java.io.Serializable

class SendingFragment: Fragment(),Serializable {
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 mFragserializedObject = arguments!!.getSerializable("serializedObject")
        System.out.println( "json serializedObject" + mFragserializedObject)
    }
}

Appdatabase.kt

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

    public abstract AddressBookDAO getAddressBook();

    public abstract TokenDAO getTokens();

    public abstract TransactionDAO getTransactions();

    public abstract BalanceDAO getBalances();
}

UPDATE

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
    }
}

**myfragent.kt**



 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)
    //             Here
                val sampleVar = BeanDemo(appDatabase)
                sampleVar.setName(appDatabase)
 val bundle = Bundle()
            bundle.putString("bundleValue", IT)
            bundle.putSerializable("serializedObject",sampleVar)
            val sendFragament: SendingFragment = SendingFragment()
            sendFragament.setArguments(bundle)
            return sendFragament

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



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

        }

But val obj = is null, in setter method to hold the value but does not return it.

8
  • How's your data class? can you post it? Commented Oct 15, 2018 at 6:44
  • sure @Skizo-ozᴉʞS check my cquestion Commented Oct 15, 2018 at 6:46
  • What do youl want to be Serializable???? Commented Oct 15, 2018 at 6:46
  • Karthikeyan , check my answer with example below Commented Oct 15, 2018 at 6:49
  • @Skizo-ozᴉʞS then how can i pass the values to Oncreateview() method ??? Commented Oct 15, 2018 at 6:51

1 Answer 1

3

When you create your Model/POJO class, then extend the class with Serializable.

Example-

class JsonData : Serializable {

    @SerializedName("hasPreviousData")
    var hasPreviousData: Boolean = false

    @SerializedName("dataList")
    var dataList: ArrayList<DataList>? = null

    inner class DataList : Serializable {
        @SerializedName("id")
        var id: String? = null
        @SerializedName("createdAt")
        var createdAt: String? = null
        @SerializedName("name")
        var name: String? = null
    }
}
Sign up to request clarification or add additional context in comments.

20 Comments

once you store your data in a POJO/Model like this then you can access that in your activity/fragment
have a doubt , the result come up with look like this "AppdatabaseIMP@43b8de6 " how can i convert this ??? is your idea can be done this ???
i can refer this site can you plz check this stackoverflow.com/questions/8887197/…
give me few mins
sure , please . .
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.