如何将项目传递给 ViewHolder?
How to pass items into ViewHolder?
我是 Kotlin 的新手,我正在制作一个货币兑换应用。在适配器中,我想将一些项目传递到新的 activity.
class AdapterC (val countryList: ArrayList<CountriesData>): RecyclerView.Adapter<AdapterC.ViewHolder>() {
override fun onCreateViewHolder(view: ViewGroup, position: Int): ViewHolder {
val v = LayoutInflater.from(view?.context).inflate(R.layout.country_list,view,false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return countryList.size
}
override fun onBindViewHolder(view: ViewHolder, position: Int) {
val country : CountriesData=countryList.toTypedArray()[position]
view?.textViewName.text=country.name
/*
Old code in which i can accese to items
view.itemView.setOnClickListener{
var name = country.name
var id = country.id
}
*/
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
val textViewName= itemView.findViewById(R.id.TextViewCountry) as TextView
//New code that i found on the internet
init {
itemView.setOnClickListener{
val intent = Intent(itemView.context, CurrencyActivity::class.java)
itemView.context.startActivity(intent)
}
}
}
}
据我所知,将 setOnClickListener 放在 onBindViewHolder 中是一种不好的做法,我无法在其中启动新的 activity,所以我在互联网上查找并找到了启动新的解决方案 Activity 在 ViewHolder 中 class。但是现在我不知道如何传递一个项目,点击进入新的activity。
下面是数据class
data class CountriesData(val name :String,val id :String)
您应该在 ViewHolder
class 中使用 getAdapterPosition()
来获取用户点击项目的位置
或
你也可以把你的onClick
代码放到onCreateViewHolder()
然后使用
vh = ViewHolder(v)
vh.textViewName.setOnClickListener {
//Your code to start activity
val intent = Intent(itemView.context, CurrencyActivity::class.java)
val bundle = Bundle()
bundle.putString("name", name)
bundle.putString("id", id)
intent.putExtras(bundle)
itemView.context.startActivity(intent)
}
你可以这样发送 CountriesData
这就是我在 Java
中的做法
@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int index)
{
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CountriesData countryData = countryList.get(index);
Intent intent= new Intent(context, NewActivity.class);
intent.putExtra("key", countryData );
context.startActivity(intent);
}
}
备注
如果您要传递 Class 对象,那么您的 class 对象应该实现 Serializable
或 Parceable
接口。
你的 CountryData
class 应该是这样的
public class CountryData implements Serializable{
// getter and setter
}
如何获取值?
您可以使用键值获取 Class 对象。别忘了打字。
Intent i = getIntent();
CountryData countrydata = (CountryData)i.getSerializableExtra("key");
Kotlin 中的 RecyclerView 适配器
二手红豆 https://github.com/Kotlin/anko
Anko is a Kotlin library which makes Android application development faster and easier. It makes your code clean and easy to read, and lets you forget about rough edges of the Android SDK for Java.
class StackAdapter(val context: Context, val countryList: ArrayList<CountriesData>) : RecyclerView.Adapter<StackAdapter.StackViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StackViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
return StackViewHolder(view)
}
override fun getItemCount(): Int = countryList.size
override fun onBindViewHolder(holder: StackViewHolder, position: Int) {
holder.setUpViewHolder(countryList[position])
}
inner class StackViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val Name: TextView = itemView.txtView
fun setUpViewHolder(countries: CountriesData){
Name.text = countries.name
Name.setOnClickListener {
context.startActivity<CurrencyActivity>(COUNTRIES to countries)
}
}
}
}
如何在Activity
中获取数据
data = intent.getStringExtra(COUNTRIES)
注意 COUNTRIES 只是 Constants.kt
中的一个键
这就是您在 Kotlin 中的做法。
const val HEADER_USER_TYPE = "userType"
我是 Kotlin 的新手,我正在制作一个货币兑换应用。在适配器中,我想将一些项目传递到新的 activity.
class AdapterC (val countryList: ArrayList<CountriesData>): RecyclerView.Adapter<AdapterC.ViewHolder>() {
override fun onCreateViewHolder(view: ViewGroup, position: Int): ViewHolder {
val v = LayoutInflater.from(view?.context).inflate(R.layout.country_list,view,false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return countryList.size
}
override fun onBindViewHolder(view: ViewHolder, position: Int) {
val country : CountriesData=countryList.toTypedArray()[position]
view?.textViewName.text=country.name
/*
Old code in which i can accese to items
view.itemView.setOnClickListener{
var name = country.name
var id = country.id
}
*/
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
val textViewName= itemView.findViewById(R.id.TextViewCountry) as TextView
//New code that i found on the internet
init {
itemView.setOnClickListener{
val intent = Intent(itemView.context, CurrencyActivity::class.java)
itemView.context.startActivity(intent)
}
}
}
}
据我所知,将 setOnClickListener 放在 onBindViewHolder 中是一种不好的做法,我无法在其中启动新的 activity,所以我在互联网上查找并找到了启动新的解决方案 Activity 在 ViewHolder 中 class。但是现在我不知道如何传递一个项目,点击进入新的activity。
下面是数据class
data class CountriesData(val name :String,val id :String)
您应该在 ViewHolder
class 中使用 getAdapterPosition()
来获取用户点击项目的位置
或
你也可以把你的onClick
代码放到onCreateViewHolder()
然后使用
vh = ViewHolder(v)
vh.textViewName.setOnClickListener {
//Your code to start activity
val intent = Intent(itemView.context, CurrencyActivity::class.java)
val bundle = Bundle()
bundle.putString("name", name)
bundle.putString("id", id)
intent.putExtras(bundle)
itemView.context.startActivity(intent)
}
你可以这样发送 CountriesData
这就是我在 Java
中的做法 @Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, int index)
{
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CountriesData countryData = countryList.get(index);
Intent intent= new Intent(context, NewActivity.class);
intent.putExtra("key", countryData );
context.startActivity(intent);
}
}
备注
如果您要传递 Class 对象,那么您的 class 对象应该实现 Serializable
或 Parceable
接口。
你的 CountryData
class 应该是这样的
public class CountryData implements Serializable{
// getter and setter
}
如何获取值?
您可以使用键值获取 Class 对象。别忘了打字。
Intent i = getIntent();
CountryData countrydata = (CountryData)i.getSerializableExtra("key");
Kotlin 中的 RecyclerView 适配器
二手红豆 https://github.com/Kotlin/anko
Anko is a Kotlin library which makes Android application development faster and easier. It makes your code clean and easy to read, and lets you forget about rough edges of the Android SDK for Java.
class StackAdapter(val context: Context, val countryList: ArrayList<CountriesData>) : RecyclerView.Adapter<StackAdapter.StackViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StackViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
return StackViewHolder(view)
}
override fun getItemCount(): Int = countryList.size
override fun onBindViewHolder(holder: StackViewHolder, position: Int) {
holder.setUpViewHolder(countryList[position])
}
inner class StackViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val Name: TextView = itemView.txtView
fun setUpViewHolder(countries: CountriesData){
Name.text = countries.name
Name.setOnClickListener {
context.startActivity<CurrencyActivity>(COUNTRIES to countries)
}
}
}
}
如何在Activity
中获取数据 data = intent.getStringExtra(COUNTRIES)
注意 COUNTRIES 只是 Constants.kt
中的一个键这就是您在 Kotlin 中的做法。 const val HEADER_USER_TYPE = "userType"