0

I have an activity for creating new item for listView. On click Save button of this activity , I want to add date to arrayList. This arrayList is in another activity and this activity has a listView and its adapter gets items from arrayList

This is my adapter :

 inner class mo3d1Adapter : BaseAdapter {
    override fun getItemId(p0: Int): Long {
        return p0.toLong()
    }

    override fun getCount(): Int {
        return listOfmo3d.size
    }

    var listOfmo3d = ArrayList<mo3dInfo>()
    var context: Context? = null

    constructor(context: Context, listOfmo3d: ArrayList<mo3dInfo>) : super() {
        this.listOfmo3d = listOfmo3d
        this.context = context
    }

    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {


        val mo3d = listOfmo3d[p0]

        var inflatormo3d = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var myViewmo3d = inflatormo3d.inflate(R.layout.item_mo3d, null)




            myViewmo3d.setOnClickListener() {

                Toast.makeText(context, " click" , Toast.LENGTH_SHORT).show()
                var intent = Intent(context, mo3dDetails::class.java)
                startActivity(intent)
            }
        myViewmo3d.meeting_time_mo3d.text = mo3d.time.toString()

        myViewmo3d.meeting_name_mo3d.text = mo3d.name.toString()
     //   myViewmo3d .meeting_time.text = mo3d .time.toString()

        myViewmo3d.meeting_date_mo3d.text = mo3d.date.toString()
            //   myViewmo3d.attendance_number_.text = mo3d.n2.toString()!!


            return myViewmo3d


    }

    override fun getItem(p0: Int): Any {
        return  listOfmo3d[p0]

    }


}

This is my arrayList :

  var listOfmo3d = ArrayList<mo3dInfo>()

This is on click save button function :

fun SaveAction(view: View) {
    var i = MainMo3d()

    i.listOfmo3d.add(mo3dInfo("f", "test", "test"))
}

How can I pass data from activity to another activity and add it to arrayList ?

Vishnu M Menon
  • 1,459
  • 2
  • 19
  • 34
Nour Medhat
  • 567
  • 4
  • 9
  • 15

2 Answers2

0

You can send from activity like this

 List< mo3dInfo > l = new ArrayList<>();
            Type listOfTestObject = new TypeToken<List<mo3dInfo>>() {
            }.getType();
String s = new Gson().toJson(l,listOfObjects);
Intent intent = new Intent(context,activity);
intent.putExtra("data",s);
startActivity(intent);

And you can receive data like this

Type type=new TypeToken<List<mo3dInfo>>(){}.getType();
            List<mo3dInfo> list=new Gson().fromJson(getintent.getStringExtra("data"),type));
Ahsan Saeed
  • 701
  • 10
  • 22
0

Using Parcelable from Kotlin 1.1.4

Use @Parcelize annotation in your mo3dInfo class extending from Parcelable

@Parcelize
class mo3dInfo():Parceable

NOTE : The studio gives you error.but you can ignore this error; the lint checks haven’t yet been updated to understand @Parcelize. The corresponding YouTrack issue is here:

https://youtrack.jetbrains.com/issue/KT-19300

and then pass the object of array of mo3dInfo in Intent like

intent.putExtra(key,arrayOfmo3dInfo)

then to parse this object in your listView activity

 var array: ArrayList<mo3dInfo> = intent.getParcelableExtra(key)

More info refer Kotlin parcelable

Pravin Londhe
  • 865
  • 7
  • 14
  • @Parcelize class mo3dInfo():Parceable{} when i add this line i got an error – Nour Medhat Aug 28 '17 at 12:15
  • What error you get? If you not enabled android extensions 1.1.4 then put androidExtensions{ experimental = true } in your app level build.gradle and kotlin version should be 1.1.4 – Pravin Londhe Aug 28 '17 at 12:41
  • how can i enabled it ? – Nour Medhat Aug 28 '17 at 12:42
  • put androidExtensions{ experimental = true } in your app level build.gradle refer https://blog.jetbrains.com/kotlin/2017/08/kotlin-1-1-4-is-out/ – Pravin Londhe Aug 28 '17 at 12:42
  • @NourMedhat i just updated my answer .check that work for you.You can also refer the same type of question answered by me https://stackoverflow.com/questions/46093364/android-kotlin-create-class-implement-parcelable-give-an-error-in-override-of/46097159#46097159 – Pravin Londhe Sep 08 '17 at 01:38