将数据从一个函数发送到另一个函数

Send data from one function to another

深入了解函数内部insertUserinsertMoney

我想把insertUser中id的值传给insertMoney id 是一个 变量

我试过 var idT,insertUser 可以工作,但在其他函数中 insertMoney returns "".

这是我的视图模型:

var idT= ""
class AddUserViewModel(
    val mUserInfoDAO: UserDAO,
    val mTransactionDAO: TransactionsDAO,
    val mLoan: LoanDAO,
    val mBankDAO: BankDAO,
    application: Application
) :
AndroidViewModel(application) {
var viewModelJob = Job()
val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob)


    fun insertUser(
    fullName: String,
    accountId: String,
    mobileNumber: String,
    phoneNumber: String,
    dateOfCreation: String,
    address: String,
) {
    uiScope.launch {
        try {
            Log.d("TAG", "insertTest")
            val user = UserInfo(
                0L,
                fullName,
                accountId,
                mobileNumber,
                phoneNumber,
                dateOfCreation,
                address
            )
            withContext(Dispatchers.IO) {
           
                #*#*#*#*#*#* I WANT TO SENT VALUE OF *id* TO insertMoney #*#*#*#*#*#* 

                val id = mUserInfoDAO.insert(user)
             
   
               idT=id.toString()
                Log.d("TAG", "insertUser: $idT")
            }
        } catch (e: Exception) {
            Log.d("TAG", "insertContact: ${e.message}")
        }
    }
}

fun insertMoney(
    amount: String,
    increasePage: String?
) {
    Log.d("TAG", "insertUserTTT: $idT")

    var bankId: Long = bankInfo.value?.get(selectedItemPosition)?.bankId!!
    uiScope.launch {
        mTransactionDAO.insert(
            Transaction(
                0L,
            
                *#*#*#*#*#HERE#*#*#*#*#*#
                // id //,
                null,
                bankId,
                null,
                amount,
                null,
                null,
                null,
                increasePage
            )
        )

    }

}

我在片段中调用了这两个函数:

   mAddUserViewModel.addUser.observe(viewLifecycleOwner, Observer {
        if (it == true) {
          
          
               mAddUserViewModel.insertUser(
                mAddUserListBinding.fullName.text.toString(),
                mAddUserListBinding.accountId.text.toString(),
                mAddUserListBinding.mobileNumber.text.toString(),
                mAddUserListBinding.phoneNumber.text.toString(),
                mAddUserListBinding.createdDate.text.toString(),
                mAddUserListBinding.edtAddress.text.toString(),
            )
         
         
            val firstMoney = "firstMoney"
            mAddUserViewModel.insertMoney(
                mAddUserListBinding.firstMoney.text.toString(),
                firstMoney
            )
            findNavController().popBackStack()
        }
    })

我解决了

我混合了两个函数体 我删除了 insertMoney 函数并将其主体添加到 insertUser 中,我得到了 editText 的值并将其插入到 insert User

 fun insertUser(
    fullName: String,
    accountId: String,
    mobileNumber: String,
    phoneNumber: String,
    dateOfCreation: String,
    address: String,
) {
    uiScope.launch {
        try {
            Log.d("TAG", "insertTest")
            val user = UserInfo(
                0L,
                fullName,
                accountId,
                mobileNumber,
                phoneNumber,
                dateOfCreation,
                address
            )
            withContext(Dispatchers.IO) {
                val id = mUserInfoDAO.insert(user)
                Log.d("TAG", "insertUser: $amount")
                val increasePage = "firstMoney"
                val bankId: Long = bankInfo.value?.get(selectedItemPosition)?.bankId!!
                mTransactionDAO.insert(
                    Transaction(
                        0L,
                        id,
                        null,
                        bankId,
                        null,
                        amount,
                        null,
                        null,
                        null,
                        increasePage
                    )
                )
            }

        } catch (e: Exception) {
            Log.d("TAG", "insertContact: ${e.message}")
        }

    }
}
private var username = ObservableField("").toString()
fun afterUserNameChange(s: CharSequence) {
    Log.i("truck", s.toString());
    amount = s.toString()
    this.username = s.toString()
}
fun insertUser(
    fullName: String,
    accountId: String,
    mobileNumber: String,
    phoneNumber: String,
    dateOfCreation: String,
    address: String,
    function:(Int,String,String)->Unit ,
    firstMoney:String  )  {
    uiScope.launch {
        try {
            Log.d("TAG", "insertTest")
            val user = UserInfo(
                0L,
                fullName,
                accountId,
                mobileNumber,
                phoneNumber,
                dateOfCreation,
                address
            )
             withContext(Dispatchers.IO) {
                val id = mUserInfoDAO.insert(user)

             function (id, firstMoney,"first money")

            }
        } catch (e: Exception) {
            Log.d("TAG", "insertContact: ${e.message}")
        }
    }   
  }

insertUser(......,...,..., ::insertMoney,"30")