英文:
Passing an ArrayList of Object from one activity to another
问题
I understand your request. Here's the translated portion:
用户动态
val jsonObjectRequest = JsonObjectRequest(Request.Method.GET, url, null,
{ response ->
Log.d("API响应", response.toString())
val articlesArray = response.optJSONArray("articles")
val newsList = parseNewsArticles(articlesArray)
println(newsList)
val newsAdapter = NewsAdapter(newsList as ArrayList<News>){
val myIntent = Intent(this@UserFeed, NewsDetails::class.java)
myIntent.putExtra("news", newsList)
startActivity(myIntent)
}
binding.recyclerView.adapter = newsAdapter
binding.recyclerView.layoutManager = LinearLayoutManager(this@UserFeed)
},
{ error ->
Log.e("API错误", error.toString())
})
requestQueue.add(jsonObjectRequest)
数据类
data class News(
val author: String,
val title: String,
val url: String,
val urlToImage: String,
val description: String,
val publishedAt: String,
val content: String
)
最后,我想能够在NewsDetails类中像在我的主Activity中一样按如下方式访问列表中的新闻标题:news.title
。
我知道有可能将JSON响应传递到Intent中,并在我的第二个Activity中重新解析它,但这并不理想。我也研究过使用getSerializable()
进行序列化,但这将意味着将我的数据类转换为可序列化,这将干扰程序的其他部分。
英文:
I'm trying to find a way to pass my already parsed JSON response from my API to another activity on a click event. When a user click on a preview, he gets redirected to the full article. Here I'm using an Adapter along with a DataClass
User Feed
val jsonObjectRequest = JsonObjectRequest(Request.Method.GET, url, null,
{ response ->
Log.d("API Response", response.toString())
val articlesArray = response.optJSONArray("articles")
val newsList = parseNewsArticles(articlesArray)
println(newsList)
val newsAdapter = NewsAdapter(newsList as ArrayList<News>){
val myIntent = Intent(this@UserFeed, NewsDetails::class.java)
myIntent.putExtra("news", newsList)
startActivity(myIntent)
}
binding.recyclerView.adapter = newsAdapter
binding.recyclerView.layoutManager = LinearLayoutManager(this@UserFeed)
},
{ error ->
Log.e("API Error", error.toString())
})
requestQueue.add(jsonObjectRequest)
Data Class
data class News (
val author : String,
val title : String,
val url : String,
val urlToImage : String,
val description : String,
val publishedAt : String,
val content : String)
Finally I would like to be able to access the list as follow news.title on my NewsDetails Class Just like I do on my main Activity.
I aware of the possibility, but not ideal, to pass my JSON response inside my Intent and parse it again on my second Activity . I've also looked into serialize with getSerializable() but this will imply to cast my DataClass to serializable which will interfere with other parts of the program.
答案1
得分: 0
建议使用 Parcelable
来通过 bundle/intent 从一个 Screen
发送列表到另一个 Screen
。您也可以使用 @Serializable
,但由于反射对性能的影响,最好坚持使用 Parcelable
,我没有看到使类成为 Parcelable
会产生任何影响
在您的 app 模块的 build.gradle
中的插件中添加以下内容
plugins {
id 'kotlin-parcelize'
}
然后将数据类设为可 Parcelable
@Parcelize
data class News (
val author: String,
val title: String,
val url: String,
val urlToImage: String,
val description: String,
val publishedAt: String,
val content: String
): Parcelable
在发送数据时
myIntent.putParcelableArrayListExtra("news", newsList)
startActivity(myIntent)
英文:
It is recommended to use Parcelable
for sending list from one Screen
to other via bundle/intent. You could also use @Serializable
but given the implications on performance due to reflection better to stick with Parcelable
and I do not see any implications on class being Parcelable
Add below to your plugins in build.gradle
inside app module
plugins {
id 'kotlin-parcelize'
}
Then make data class parcelable
@Parcelize
data class News (
val author : String,
val title : String,
val url : String,
val urlToImage : String,
val description : String,
val publishedAt : String,
val content : String): Parcelable
while sending data
myIntent.putParcelableArrayListExtra("news", newsList)
startActivity(myIntent)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论