0

Complete Newbie to Kotlin, trying to get three values to pass across from text boxes to another page, I am getting no errors put the info is not either being passed across or displayed I have tried for hours to get this to work with no avail.page 1 is where I input data and page 2 is where it should be displayed.

Could someone point me in a good or right direction, please?

I have now edited my code as given in the help below on the page that is now receiving the info I cannot get it to display.

PAGE1

package com.example.james.visitorapp

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.provider.AlarmClock.EXTRA_MESSAGE
import android.view.View
import android.widget.EditText

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    /** Called when the user taps the Send button */
    fun sendMessage(view: View) {
        val editText = findViewById<EditText>(R.id.editText)
        val editText2 = findViewById<EditText>(R.id.editText2)
        val editText3 = findViewById<EditText>(R.id.editText3)
        val message1 = editText.text.toString()
        val message2 = editText2.text.toString()
        val message3 = editText3.text.toString()


        val intent = Intent(this, DisplayMessageActivity::class.java).apply {
            putExtra(EXTRA_MESSAGE, arrayOf(message1, message2, message3))
        }

        startActivity(intent)
    }
}

receiving page

package com.example.james.visitorapp

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.provider.AlarmClock.EXTRA_MESSAGE
import android.widget.TextView

class DisplayMessageActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_display_message)

        // Get the Intent that started this activity and extract the string
        val message1 = intent.getStringExtra(EXTRA_MESSAGE)
        val message2 = intent.getStringExtra(EXTRA_MESSAGE)
        val message3 = intent.getStringExtra(EXTRA_MESSAGE)
        // Capture the layout's TextView and set the string as its text
        val textView = findViewById<TextView>(R.id.textView).apply {
            text = message1

        }
        val textView2 = findViewById<TextView>(R.id.textView2).apply {
            text = message2

        }
        val textView3 = findViewById<TextView>(R.id.textView3).apply {
            text = message3

        }
    }
}
Ronan Quillevere
  • 3,699
  • 1
  • 29
  • 44
James24
  • 73
  • 4
  • 12
  • is the `sendMessage()` invoked when button clicked? – tahsinRupam Apr 24 '18 at 10:57
  • Yes i have Assigned the sendMessage to the button on my app page – James24 Apr 24 '18 at 11:03
  • Sure, does the click work? Do you go to `DisplayMessageActivity`after the button click? – tahsinRupam Apr 24 '18 at 11:05
  • Yes once i run the app from my phone it goes to the DisplayMessageActivity page but the page is blank – James24 Apr 24 '18 at 11:13
  • The problem is that the data is being 'put' as a String array in MainActivity but the 'get' is retrieving it as individual Strings. Simplest fix is to introduce EXTRA_MESSAGE1, EXTRA_MESSAGE2, EXTRA_MESSAGE3 and call put and get 3 times each. Or update DisplayMessageActivity to retrieve the data as an array. – Elletlar Apr 24 '18 at 11:26
  • @user3509297 Check my following answer please. – tahsinRupam Apr 24 '18 at 12:19

1 Answers1

1

You should do as following, in MainActivity.kt:

class MainActivity : AppCompatActivity() {

    var editText : EditText ?= null
    var editText2 : EditText ?= null;
    var editText3 : EditText ?= null;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        editText = findViewById<EditText>(R.id.editText)
        editText2 = findViewById<EditText>(R.id.editText2)
        editText3 = findViewById<EditText>(R.id.editText3)

    }

    /** Called when the user taps the Send button */
    fun sendMessage(view: View) {

        val message1 = editText.text.toString()
        val message2 = editText2.text.toString()
        val message3 = editText3.text.toString()


        val intent = Intent(this, DisplayMessageActivity::class.java).apply {
            putExtra("EXTRA_MSG1", message1)
            putExtra("EXTRA_MSG2", message2)
            putExtra("EXTRA_MSG3", message3)
        }

        startActivity(intent)
    }

In DisplayMessageActivity.kt

class DisplayMessageActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_display_message)

        // Get the Intent that started this activity and extract the string
        val message1 = intent.getStringExtra("EXTRA_MSG1")
        val message2 = intent.getStringExtra("EXTRA_MSG2")
        val message3 = intent.getStringExtra("EXTRA_MSG3")
        // Capture the layout's TextView and set the string as its text
        val textView = findViewById<TextView>(R.id.textView).apply {
            text = message1

        }
        val textView2 = findViewById<TextView>(R.id.textView2).apply {
            text = message2

        }
        val textView3 = findViewById<TextView>(R.id.textView3).apply {
            text = message3

        }
    }
}
tahsinRupam
  • 6,325
  • 1
  • 18
  • 34