【发布时间】:2020-02-11 08:56:00
【问题描述】:
这是我的Post.kt 课程
data class Post ( val id: Int, val name: String?, val slug: String?,
val image: String?, val body: String?, val icon: String?,
val quote: String?, val video: String?, val created_at: String?,
val updated_at: String?
)
我需要在我的适配器意图中获取val slug,以便我可以将其发送到其他活动
Adapter
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var textView: TextView
var aImage: ImageView
init {
textView = itemView.findViewById(R.id.text_name)
aImage = itemView.findViewById(R.id.a_image)
aImage.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
val intent = Intent(aImage.context, ArticlesDetail::class.java)
intent.putExtra("my_slug",text_slug.text.toString()) //here
aImage.context.startActivity(intent)
Log.e("Click", "you click on = $adapterPosition")
}
})
}
}
注意:
text_slug.text.toString()不存在,它只是一个占位符,我需要提供我的val slug而不是text_slug。这就是我要找的。p>
有什么想法吗?
更新
我的适配器完整代码 + 包括下面的答案
class MyPostsRecyclerViewAdapter(val posts : ArrayList<Post>) : RecyclerView.Adapter<MyPostsRecyclerViewAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view: View = LayoutInflater.from(parent.context).inflate(R.layout.fragment_posts, parent, false)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return posts.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.textView.text = posts.get(position).name
holder.bind(this.posts[position]) //added
Glide.with(holder.aImage.context)
.load(posts.get(position).image)
.placeholder(R.drawable.placeholder2)
.error(R.drawable.placeholder2)
.fallback(R.drawable.placeholder2)
.into(holder.aImage)
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var textView: TextView
var aImage: ImageView
init {
textView = itemView.findViewById(R.id.text_name)
aImage = itemView.findViewById(R.id.a_image)
}
// added
fun bind(post:Post){
textView.text = post.slug?:""
aImage.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
val intent = Intent(aImage.context, ArticlesDetail::class.java)
intent.putExtra("my_slug",post.slug?:"") // <<---
aImage.context.startActivity(intent)
Log.d("my slug is:", post.slug)
Log.e("Click", "you click on = $adapterPosition")
}
})
}
}
}
此代码的结果是应用程序崩溃(关闭并出现此错误
my.app.name has stopped)
更新 2
这是我的目标活动ArticlesDetail 代码。
说明 我上面的意图代码发送的 slug 来到这个活动,并根据该 slug 从 api 链接获取数据。
class ArticlesDetail : AppCompatActivity() {
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.article_details)
callAPIDemo()
}
// api code
private fun callAPIDemo() {
val mySlugValue: String = intent.getStringExtra("my_slug")
// Instantiate the RequestQueue.
val queue = Volley.newRequestQueue(this)
val url = "https://example.com/api/articles/$mySlugValue"
// Request a string response from the provided URL.
val stringRequest = StringRequest(
Request.Method.GET, url,
Response.Listener<String> { response ->
val list: ArrayList<Post> = ArrayList()
getPosts(response,list)
// here you will have the complete list of data in your "list" variable
posts_list.layoutManager = LinearLayoutManager(this)
Log.d("my list", list.toString())
posts_list.adapter = ArticlesAdapter(list)
},
Response.ErrorListener { error ->
//displaying the error in toast if occurrs
Toast.makeText(applicationContext, error.message, Toast.LENGTH_SHORT)
.show()
})
// Add the request to the RequestQueue.
queue.add(stringRequest)
}
fun getPosts(response: String,list:ArrayList<Post>) {
var jsonObject = JSONObject(response)
val jsonArray = jsonObject.getJSONArray("posts")
for (i in 0 until jsonArray.length()) {
val jsonObject1 = jsonArray.getJSONObject(i)
var listingObject = Post(
jsonObject1.getInt("id"),
jsonObject1.getString("name"),
jsonObject1.getString("slug"),
jsonObject1.getString("image"),
jsonObject1.getString("body"),
jsonObject1.getString("icon"),
jsonObject1.getString("quote"),
jsonObject1.getString("video"),
jsonObject1.getString("created_at"),
jsonObject1.getString("updated_at")
)
list.add(listingObject)
}
}
}
目前,当我单击应该将我带入此活动以及 slug 值的图像时,我收到此错误
在 my.app.name.ui.ArticlesDetail.ArticlesDetail.getPosts(ArticlesDetail.kt:73)
第 73 行是这一行:
val jsonArray = jsonObject.getJSONArray("posts")
【问题讨论】:
-
在 Post 类中实现 Parcelable 并将数据作为 intent.putExtra("user", Parcel OBJECT);并获取 getIntent().getParcelableExtra("")
标签: android kotlin android-intent