Android Fragments 如何在Android中将数据从片段发送到Activity

wswtfjt7  于 2023-03-23  发布在  Android
关注(0)|答案(4)|浏览(129)

在我的应用程序中,我想使用FragmentActivity,我想从这个fragment中得到startActivity,我应该用startActivity发送数据
我写下面的代码,但当运行应用程序显示activity

分片编码:

homeDashboard_giftInfoLayout.setOnClickListener {
    val intent = Intent(requireContext(), DashboardChargeWalletActivity::class.java)
    intent.putExtra(USER_WALLET_PRICE, response.user.wallet.credit)
    intent.putExtra(USER_CHARGED_VALUE, 0)
    startActivity(intent)
}

活动编码:

userWallet = intent?.getStringExtra(USER_WALLET_PRICE).toString()
userWallet.let {
    //User wallet
    dashboardChargeWallet_userWallet.text =
        "${getString(R.string.walletInventory)}: $userWallet ${getString(R.string.toman)}"
}

当点击startActivity的按钮时,我将此response.user.wallet.credit显示到吐司中。
但是在activity中显示null
我用Kotlin来做语言。
我该怎么修呢?

kcugc4gi

kcugc4gi1#

该行:

intent.putExtra(USER_WALLET_PRICE, response.user.wallet.credit)

我怀疑response.user.wallet.credit不是一个字符串。所以下面这行:

userWallet = intent?.getStringExtra(USER_WALLET_PRICE).toString()

我找不到一个带有USER_WALLET_PRICE键的字符串。检查www.example.com是什么类型response.user.wallet.credit,int?long?serializable?并确保你得到的是相同的类型

dsekswqp

dsekswqp2#

你需要从一个片段中使用这样的意图。

homeDashboard_giftInfoLayout.setOnClickListener {
 val intent = Intent(activity, DashboardChargeWalletActivity::class.java)
 intent.putExtra(USER_WALLET_PRICE, response.user.wallet.credit)
 intent.putExtra(USER_CHARGED_VALUE, 0)
 startActivity(intent);
}

也可以使用requireActivity

val intent=Intent(requireActivity(),DashboardChargeWalletActivity::class.java)

希望这对你有用。

xa9qqrwz

xa9qqrwz3#

试试这个,可能会有帮助

Intent intent = new Intent(getActivity(), LoadActivity.class);
    intent.putExtra(USER_WALLET_PRICE, response.user.wallet.credit)
    intent.putExtra(USER_CHARGED_VALUE, 0)
    startActivity(intent);
pb3s4cty

pb3s4cty4#

Kotlin:数据发送片段到activity*一步一步跟随 *
第一步:分片端创建接口

class SurveyPageOneFragment : Fragment() {
private var _binding: FragmentSurveyPageOneBinding? = null
private val binding get() = _binding!!
private var mContext: Context? = null

interface OnDataPassFragOne {
    fun onDataPassFragOne(
        firstName : String?,
        middleName : String?,
        lastName : String?,
        mobile: String?,
        age: Int?,
        dob: String?,
        bDay: String?,
        bMonth: String?,
        bYear: String?,
        gender: String?, bloodGroup: String?)
}

private lateinit var dataPasser: OnDataPassFragOne
fun setListener(dataPasser1: OnDataPassFragOne){
    dataPasser = dataPasser1
}

override fun onAttach(context: Context) {
    super.onAttach(context)
    dataPasser = context as OnDataPassFragOne
}

fun passDataFragOne(){
    dataPasser.onDataPassFragOne(firstName,middleName,lastName,mobile,niceAge,birthDate,bDay,bMonth,bYear,gender,bloodGroup)
}}

第二步:进入设置视图页中的活动和第一次更改

private fun setUpViewPager() {

    surveyPageOneFragment = SurveyPageOneFragment.newInstance(false)
    surveyPageOneFragment.setListener(this)

    surveyPageTwoFragment = SurveyPageTwoFragment.newInstance(false)
    surveyPageTwoFragment.setListenerTwo(this)

    surveyPageThreeFragment = SurveyPageThreeFragment.newInstance(false)
    surveyPageThreeFragment.setListenerThree(this)

    surveyPageFourFragment = SurveyPageFourFragment.newInstance(false)
    surveyPageFourFragment.setListenerFour(this)

    surveyPageFiveFragment = SurveyPageFiveFragment.newInstance(false)
    surveyPageFiveFragment.setListenerFive(this)

    val fragmentList: MutableList<Fragment> = mutableListOf()
    fragmentList.add(surveyPageOneFragment)
    fragmentList.add(surveyPageTwoFragment)
    fragmentList.add(surveyPageThreeFragment)
    fragmentList.add(surveyPageFourFragment)
    fragmentList.add(surveyPageFiveFragment)

    surveyPagerAdapter = SurveyPagerAdapter(fragmentList, supportFragmentManager, lifecycle)
    binding.viewPager.adapter = surveyPagerAdapter
    binding.dotsIndicator.attachTo(binding.viewPager)

}

第三步:活动调用界面

class SurveyInfoActivity : AppCompatActivity() SurveyPageOneFragment.OnDataPassFragOne{

private lateinit var surveyPageOneFragment: SurveyPageOneFragment

override fun onDataPassFragOne(
    firstName: String?,
    middleName: String?,
    lastName: String?,
    mobile: String?,
    age: Int?,
    dob: String?,
    bDay: String?,
    bMonth: String?,
    bYear: String?,
    gender: String?,
    bloodGroup: String?
) {
    Log.i(
        "TAG",
        "fullName: $firstName $middleName $lastName, moble: $mobile, age: $age, dob: $dob, gender: $gender, bloodGroup: $bloodGroup"
    )
    surveyRequestModel.name = "$firstName $middleName $lastName"
    surveyRequestModel.mobileNumber = "$mobile"
    surveyRequestModel.age = "$age"
    surveyRequestModel.dateOfBirth = "$bYear-$bMonth-$bDay"
    surveyRequestModel.gender = "$gender"
    surveyRequestModel.bloodGroup = "$bloodGroup"
}}

第四步:点击页面按位置设置

binding.btnNext.setOnClickListener {
if (binding.viewPager.currentItem < binding.viewPager.adapter?.itemCount!!) {
    when (fragPosition) {
        0 -> {
            Log.i("TAG", "Page 1: Position $fragPosition")
            binding.viewPager.currentItem = binding.viewPager.currentItem + 1
            surveyPageOneFragment.passDataFragOne()
            saveSurveyFragOne()
        }
    }
}}

相关问题