从一个活动传递一个对象的ArrayList到另一个活动

huangapple go评论61阅读模式
英文:

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 -&gt;
                Log.d(&quot;API Response&quot;, response.toString())
                val articlesArray = response.optJSONArray(&quot;articles&quot;)
                val newsList = parseNewsArticles(articlesArray)

                println(newsList)

                val newsAdapter = NewsAdapter(newsList as ArrayList&lt;News&gt;){
                    val myIntent = Intent(this@UserFeed, NewsDetails::class.java)
                    myIntent.putExtra(&quot;news&quot;, newsList)
                    startActivity(myIntent)
                }
                binding.recyclerView.adapter = newsAdapter
                binding.recyclerView.layoutManager = LinearLayoutManager(this@UserFeed)

            },
            { error -&gt;
                Log.e(&quot;API Error&quot;, 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 &#39;kotlin-parcelize&#39;
}

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(&quot;news&quot;, newsList)
startActivity(myIntent)

huangapple
  • 本文由 发表于 2023年5月25日 06:25:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定