gpt4 book ai didi

android - 如何使用Intent在Kotlin中放置数据类列表

转载 作者:行者123 更新时间:2023-12-02 13:32:22 29 4
gpt4 key购买 nike

我想使用Intent发送用于新 Activity 的数据类列表(列表)。
在下面的代码(SplashScreenRepository.kt)中,首先我使用改造连接api(此操作已完成),当响应成功时,我将数据放入DataPeople列表中。
在第二部分中,我需要将此数据列表发送给带有Intent的新 Activity ,以放入回收器 View 。
因此,我希望将所有信息放入intent.putExtra,但是如何?

数据人

data class DataPeople(
val name : String,
val height : String,
val mass : String,
val hair_color : String,
val skin_color : String,
val eye_color : String,
val birth_year : String,
val gender : String
)

SplashScreenRepository.kt
fun getDataInApi(onFinnish : (List<DataPeople>) -> Unit) {
val retrofitClient = NetworkUtils.getRetrofitInstance()
val endpoint = retrofitClient.create(Endpoint::class.java)
val callDataOfPeople = endpoint.dataOfPeople()

val list = ArrayList<DataPeople>()

callDataOfPeople.enqueue(object : Callback<SerializeDataPeople?> {
override fun onResponse(call: Call<SerializeDataPeople?>, response: Response<SerializeDataPeople?>) {
response.body().let{
val note: SerializeDataPeople? = it

loop@ for (i in 0..9) {
name = note?.results?.get(i)?.name.toString()
height = note?.results?.get(i)?.height.toString()
mass = note?.results?.get(i)?.mass.toString()
hair_color = note?.results?.get(i)?.hair_color.toString()
skin_color = note?.results?.get(i)?.skin_color.toString()
eye_color = note?.results?.get(i)?.eye_color.toString()
birth_year = note?.results?.get(i)?.birth_year.toString()
gender = note?.results?.get(i)?.gender.toString()

list.add(DataPeople(name, height, mass, hair_color, skin_color, eye_color, birth_year, gender))
}
dataPeopleList = list
onFinnish(dataPeopleList)
}
}

override fun onFailure(call: Call<SerializeDataPeople?>, t: Throwable) {
Log.e("onFailure error", t?.message)
onFinnish(emptyList())
}
})
}

SplashScreen.kt
class SplashScreenActivity : AppCompatActivity() {
private lateinit var splashScreenViewModel: SplashScreenViewModel

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

(...)

splashScreenViewModel.getIsUpdatingLiveDataLoaded()?.observe(this, Observer{ it ->
when(it){
SplashScreenViewModel.States.DONE ->{
Toast.makeText(this, "Data is ready.", Toast.LENGTH_LONG).show()

splashScreenViewModel.getPeopleLiveData()?.observe(this, Observer {
val intent = Intent(this, MainActivity::class.java)
intent.putExtra("list", it)
startActivity(intent)
})
}
(...)
}
})
}

最佳答案

您可以使用Room创建一个Sqlite表:

数据人

@Entity
data class DataPeople(
@PrimaryKey val id :Int,
val name : String,
val height : String,
val mass : String,
val hair_color : String,
val skin_color : String,
val eye_color : String,
val birth_year : String,
val gender : String
)

DataPeopleDao.kt
@Dao
interface DataPeopleDao {
@Query("SELECT * FROM DataPeople")
fun getAll(): List<DataPeople>

@Insert
fun insertAll(dataPeople:List<DataPeople>)
}

AppDatabase.kt
@Database(entities = arrayOf(DataPeople::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun dataPeopleDao(): DataPeopleDao
}

SplashScreenActivty.kt
override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)
val db = Room.databaseBuilder(
applicationContext,
AppDatabase::class.java, "database-name"
).build()
splashScreenViewModel.getIsUpdatingLiveDataLoaded()?.observe(this, Observer{ it ->
when(it){
SplashScreenViewModel.States.DONE ->{

splashScreenViewModel.getPeopleLiveData()?.observe(this, Observer {
//execute this line on a background thread
db.dataPeopleDao().insertAll(it)
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
})
}

}
})


}


MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {

val db = Room.databaseBuilder(
applicationContext,
AppDatabase::class.java, "database-name"
).build()
//execute this line on a background thread
val peopleData = db.dataPeopleDao().getAll()


}

更好的方法

您可以使用Fragment代替 Activity ,并使用ViewModel在它们之间使用 share data

但:
我建议避免使用:
第二种方法Singleton可能会伤害您。
第三方法文件总是比数据库慢。

关于android - 如何使用Intent在Kotlin中放置数据类列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60697172/

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