gpt4 book ai didi

适用于 Kotlin 的 Android Parcelable 无法正常工作

转载 作者:太空狗 更新时间:2023-10-29 15:37:38 27 4
gpt4 key购买 nike

我正在尝试将数据从一个 fragment 传递到另一个 fragment ,但我遇到了将数据从一个 fragment 发送到另一个 fragment 的问题。

 class MainFragment : Fragment() {


companion object {
public val KEY_PARSE_DATA = "parseData"
}

private var parseData: ParseData? = null

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater!!.inflate(R.layout.fragment_main, container, false).apply {
val editName = findViewById(R.id.edit_Name) as EditText
val editSurname = findViewById(R.id.edit_Surname) as EditText
val buttonNext = findViewById(R.id.btn_Next) as Button
buttonNext.setOnClickListener(View.OnClickListener {
val fragment = AnotherFragment()

if (parseData != null) {
var parseData = ParseData(editName.text.toString(), editSurname.text.toString())
val fragment = AnotherFragment()
val bundle = Bundle()
bundle.putParcelable(KEY_PARSE_DATA, parseData)
fragment.setArguments(bundle)
fragmentManager.beginTransaction().add(R.id.fragment_container, fragment).commitAllowingStateLoss()

}

})
}
}


}// Required empty public constructor

实现它的Parcelable类

 @SuppressLint("ParcelCreator")
@Parcelize
data class ParseData(val firstName: String, val lastName: String) : Parcelable {


constructor(parcel: Parcel) : this(
parcel.readString(),
parcel.readString()) {
}

override fun toString(): String {
return "ParseData(firstName='$firstName', lastName='$lastName')"
}


companion object : Parceler<ParseData> {

override fun ParseData.write(parcel: Parcel, flags: Int) {
parcel.writeString(firstName)
parcel.writeString(lastName)
}

override fun create(parcel: Parcel): ParseData {
return ParseData(parcel)
}
}
}

还有另一个从android中的parcelable类中获取数据的 fragment

    class AnotherFragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
return inflater!!.inflate(R.layout.fragment_another, container, false).apply {

val textName = findViewById<TextView>(R.id.textFirst)
val textSurname = findViewById<TextView>(R.id.textSecond)
val bundle = arguments
if (bundle != null) {
val parseData = bundle.getParcelable<ParseData>(KEY_PARSE_DATA)
textName.setText(parseData.firstName)
textSurname.setText(parseData.lastName)
}
}
}

}

我尝试了一些例子,但我不清楚如何在基于 kotlin 和 build.gradle 文件的 andoid 应用程序中实现 parcelable

 apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 26
defaultConfig {
applicationId "com.assignment.ankitt.kotlinsecond"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

androidExtensions {
experimental = true
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-v4:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

最佳答案

将以下代码添加到您的应用级 gradle。

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

androidExtensions {
experimental = true
}

Check IMG

关于适用于 Kotlin 的 Android Parcelable 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49030928/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com