简体   繁体   中英

use EditText to change TextView value on another activity (backoffice)

I am wanting to create an Admin backoffice for my app, where I can update text in other activities that are set as TextView(should they be EditText)? I have an EditText box in my Admin activity, and have set a button that "should" initialize the copying of current contents in EditText and save it to a variable "flowSales", I than try to grab the variable "flowSales" in my customer activity and .setText to my TextView. Any help would be appreciated even if it's a simple, you are doing it wrong.

I am able to find variable "flowSales" in my second activity, at this time my "var flowSales = Editable" returns no companion for Editable.

adminPage.kt (back office) editMeFs is the EditText box, and flowSales is a variable I created so I can pull it to second activity

var flowSales = Editable

class adminPage : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_admin_page)

    upDate.setOnClickListener{
        flowSales = editMeFs.text
    }
  }
}

activity2.kt (user page) flowersales is the TextView to be updated

class Activity2 : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_activity2)
    setSupportActionBar(toolbar)

    fab.setOnClickListener { view ->
        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
            .setAction("Action", null).show()
    }

    flowersales.setText(flowSales)
  }
}

So right now with var flowSales = Editable I am unable to run the app, returning error

"\adminPage.kt: (9, 17): Classifier 'Editable' does not have a companion object, and thus must be initialized here", 
if i set "= null" or "= 0"

I can run my app and click the button on admin page, however when i go to the Activity 2 it has not updated the text that i had put into EditText.

Firstly, always class name start with upper case. This is the truth;

class AdminPage: AppCompatActivity()

If we come to your problem, you can solve this way;

lateinit var flowSales: String

class AdminPage : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_admin_page)

    upDate.setOnClickListener{
        flowSales = editMeFs.text.toString()
    }
  }
}
var flowSales: String = ""

class adminPage : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_admin_page)

        //do this function when button is clicked
        upDate.setOnClickListener{
            //edit the the variable to fit what's in editText
            flowSales = editMeFs.text.toString()
        }
    }

}

user activity

class Activity2 : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_activity2)
        setSupportActionBar(toolbar)

        fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show()
        }
        //call variable from admin page and set as flowersales text
        flowersales.text = flowSales
    }
}

this works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM