Return 在 RecyclerView 中点击的正确项目位置到另一个 activity
Return the correct item position clicked in RecyclerView to another activity
我在这个问题上苦苦挣扎了好几个星期,我总是从 RecyclerView
得到 (1) returned
- 我想 return 单击 SceneFormeActivity 的正确项目位置以便能够选择正确的对象,但我每次都得到相同的“1”,所以相同的 Case1'对象信息
我的主要 class.kt 代码:
class ListeMeuble3DActivity : AppCompatActivity(),{
var listMeubles = mutableListOf<Meuble>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_liste_meuble3_d)
listMeubles = generateList()
buildRecyclerView()
}
private fun generateList(): MutableList<Meuble>{
val list = ArrayList<Meuble>()
// to Add an Object Meuble(nom,prix,stock)
val meuble = Meuble(R.drawable.fauteilgris, "Gris","25000da",1)
val meuble1 = Meuble(R.drawable.fauteuille1, "","1000d&",1)
val meuble2 = Meuble(R.drawable.burau, "Bureau","25000da",1)
list.add(meuble)
list.add(meuble1)
list.add(meuble2)
return list
}
fun buildRecyclerView() {
mon_recycler.setHasFixedSize(true)
//mon_recycler.setAdapter(mAdapter)
mon_recycler.layoutManager = LinearLayoutManager(this)
mon_recycler.adapter = MeubleAdapter(listMeubles.toTypedArray())
{
val intent3 = Intent(this, SceneformeActivity::class.java)
intent3.putExtra("image_url",1)
startActivity(intent3)
}
}
}
我的适配器Class.kt:
class MeubleAdapter(private val exampleList: Array<Meuble>,val listener: (Meuble)-> Unit) :
RecyclerView.Adapter<MeubleAdapter.ExampleViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExampleViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.meuble_item,
parent, false)
return ExampleViewHolder(itemView)
}
override fun onBindViewHolder(holder: ExampleViewHolder, position: Int) {
val currentItem = exampleList[position]
holder.imageView.setImageResource(currentItem.imageResource)
holder.textViewNomMeuble.text = currentItem.nom
holder.textViewPrix.text = currentItem.prix
holder.textViewStock.text = currentItem.stock.toString()
holder.bind(exampleList[position],listener)
}
override fun getItemCount() = exampleList.size
class ExampleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(meuble:Meuble,listener: (Meuble) -> Unit)= with(itemView)
{
android.util.Log.i("XXXX","FCT Bind ")
setOnClickListener{(listener(meuble))}
}
val imageView: ImageView = itemView.image_view
val textViewNomMeuble: TextView = itemView.nomMeuble
val textViewStock: TextView = itemView.stock
val textViewPrix: TextView = itemView.prix
}
}
我在 SceneFormeActivity.java 中的函数我应该在其中获取项目位置
private void initializeGallery() {
int imageUrl = getIntent().getIntExtra("image_url",0); // here, everytime it return 1
LinearLayout gallery = findViewById(R.id.gallery_layout);
int obj =imageUrl;
switch (obj) {
case 0: {
ImageView andy = new ImageView(this);
andy.setImageResource(R.drawable.fauteilgris);
andy.setContentDescription("fauteuil Créme");
andy.setOnClickListener(view ->{addObject(Uri.parse("foteuil.sfb"));});
gallery.addView(andy);
}
break;
case 1: {
ImageView andy = new ImageView(this);
andy.setImageResource(R.drawable.banc);
andy.setContentDescription("banc extérieur");
andy.setOnClickListener(view ->{addObject(Uri.parse("Bench.sfb"));});
gallery.addView(andy);
}
break;
case 2 : {
ImageView andy = new ImageView(this);
andy.setImageResource(R.drawable.burau);
andy.setContentDescription("bureau");
andy.setOnClickListener(view ->{addObject(Uri.parse("bureau.sfb"));});
gallery.addView(andy);
}
break;
case 3 : {
ImageView andy = new ImageView(this);
andy.setImageResource(R.drawable.fauteuille3);
andy.setContentDescription("Fauteuil une place");
andy.setOnClickListener(view ->{addObject(Uri.parse("ff.sfb"));});
gallery.addView(andy);
}
break;
default:Toast.makeText(getApplicationContext(),"Nothing",Toast.LENGTH_SHORT).show();
break;
}
}
如果您真正想要的只是项目的位置,那么您根本不需要在点击时发送 Meuble。在您的适配器中,将 onBindViewHolder
更改为:
override fun onBindViewHolder(holder: ExampleViewHolder, position: Int) {
val currentItem = exampleList[position]
holder.imageView.setImageResource(currentItem.imageResource)
holder.textViewNomMeuble.text = currentItem.nom
holder.textViewPrix.text = currentItem.prix
holder.textViewStock.text = currentItem.stock.toString()
holder.bind(position,listener)
}
设置您的适配器现在应该如下所示:
mon_recycler.adapter = MeubleAdapter(listMeubles.toTypedArray()){ position ->
val intent3 = Intent(this, SceneformeActivity::class.java)
intent3.putExtra("image_url", position)
startActivity(intent3)
}
编辑:扩展适配器用法:
最初,您将两个东西传递给 Recyclerview 适配器 -
- Meuble 列表(无需将其转换为 TypedArray
- 将在单击列表中的项目时起作用的 lambda
考虑下面的重构代码和我的内嵌注释。注意 - 我不清楚你为什么要发送位置(假设只是为了测试?)所以我假设你希望发送的对象中有一些 imageURL。我在下面展示了您如何访问它。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_liste_meuble3_d)
listMeubles = generateList()
buildRecyclerView()
}
private fun generateList(): MutableList<Meuble>{
val list = mutableListOf<Meuble>()
// to Add an Object Meuble(nom,prix,stock)
val meuble = Meuble(R.drawable.fauteilgris, "Gris","25000da",1)
val meuble1 = Meuble(R.drawable.fauteuille1, "","1000d&",1)
val meuble2 = Meuble(R.drawable.burau, "Bureau","25000da",1)
list.add(meuble)
list.add(meuble1)
list.add(meuble2)
return list
}
fun buildRecyclerView() {
mon_recycler.setHasFixedSize(true)
mon_recycler.layoutManager = LinearLayoutManager(this)
// NOTE: Here the lambda implicitly had an item of type Meuble passed. We name it here but could have just used "it".
mon_recycler.adapter = MeubleAdapter(listMeubles.toTypedArray())
{ item ->
val intent3 = Intent(this, SceneformeActivity::class.java)
intent3.putExtra("image_url",item.imageUrl)
startActivity(intent3)
}
}
}
我的适配器 Class .kt:
// Don't need to pass as a TypedArray - List is fine
class MeubleAdapter(private val exampleList: List<Meuble>,val listener: (Meuble)-> Unit) :
RecyclerView.Adapter<MeubleAdapter.ExampleViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExampleViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.meuble_item,
parent, false)
return ExampleViewHolder(itemView)
}
override fun onBindViewHolder(holder: ExampleViewHolder, position: Int) {
val currentItem = exampleList[position]
holder.imageView.setImageResource(currentItem.imageResource)
holder.textViewNomMeuble.text = currentItem.nom
holder.textViewPrix.text = currentItem.prix
holder.textViewStock.text = currentItem.stock.toString()
holder.bind(exampleList[position],listener)
}
override fun getItemCount() = exampleList.size
class ExampleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(meuble:Meuble,listener: (Meuble) -> Unit)= with(itemView)
{
android.util.Log.i("XXXX","FCT Bind ")
setOnClickListener{(listener(meuble))}
}
val imageView: ImageView = itemView.image_view
val textViewNomMeuble: TextView = itemView.nomMeuble
val textViewStock: TextView = itemView.stock
val textViewPrix: TextView = itemView.prix
}
}
所以我使用了这个解决方案,我将我点击的对象的名称(名称)传递给 SceneFormActivity 以获取引用并在下一个 activity:
中显示我的 3D 对象
主要代码
val intent3 = Intent(this, SceneformeActivity::class.java)
intent3.putExtra("image_url",it.nom) // it.nom to get the name of the object clicked
startActivity(intent3)
在 SceneformeActivity 中:
private void initializeGallery() {
String imageUrl = getIntent().getStringExtra("image_url"); // changed this from getInt to getString to get the name
LinearLayout gallery = findViewById(R.id.gallery_layout);
String obj =imageUrl;
switch (obj) {
case "fauteilgris": { // case "String": the name of the object clicked in the previous activity(RecyclerView)
ImageView andy = new ImageView(this);
andy.setImageResource(R.drawable.fauteilgris);
andy.setContentDescription("fauteuil Créme");
andy.setOnClickListener(view ->{addObject(Uri.parse("foteuil.sfb"));});
gallery.addView(andy);
}
break;
case "banc": {
ImageView andy = new ImageView(this);
andy.setImageResource(R.drawable.banc);
andy.setContentDescription("banc extérieur");
andy.setOnClickListener(view ->{addObject(Uri.parse("Bench.sfb"));});
gallery.addView(andy);
}
break;
case "burau": {
ImageView andy = new ImageView(this);
andy.setImageResource(R.drawable.burau);
andy.setContentDescription("bureau");
andy.setOnClickListener(view ->{addObject(Uri.parse("bureau.sfb"));});
gallery.addView(andy);
}
break;
default:Toast.makeText(getApplicationContext(),"No Object",Toast.LENGTH_SHORT).show();
break;
}
}
我在这个问题上苦苦挣扎了好几个星期,我总是从 RecyclerView
得到 (1) returned- 我想 return 单击 SceneFormeActivity 的正确项目位置以便能够选择正确的对象,但我每次都得到相同的“1”,所以相同的 Case1'对象信息
我的主要 class.kt 代码:
class ListeMeuble3DActivity : AppCompatActivity(),{
var listMeubles = mutableListOf<Meuble>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_liste_meuble3_d)
listMeubles = generateList()
buildRecyclerView()
}
private fun generateList(): MutableList<Meuble>{
val list = ArrayList<Meuble>()
// to Add an Object Meuble(nom,prix,stock)
val meuble = Meuble(R.drawable.fauteilgris, "Gris","25000da",1)
val meuble1 = Meuble(R.drawable.fauteuille1, "","1000d&",1)
val meuble2 = Meuble(R.drawable.burau, "Bureau","25000da",1)
list.add(meuble)
list.add(meuble1)
list.add(meuble2)
return list
}
fun buildRecyclerView() {
mon_recycler.setHasFixedSize(true)
//mon_recycler.setAdapter(mAdapter)
mon_recycler.layoutManager = LinearLayoutManager(this)
mon_recycler.adapter = MeubleAdapter(listMeubles.toTypedArray())
{
val intent3 = Intent(this, SceneformeActivity::class.java)
intent3.putExtra("image_url",1)
startActivity(intent3)
}
}
}
我的适配器Class.kt:
class MeubleAdapter(private val exampleList: Array<Meuble>,val listener: (Meuble)-> Unit) :
RecyclerView.Adapter<MeubleAdapter.ExampleViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExampleViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.meuble_item,
parent, false)
return ExampleViewHolder(itemView)
}
override fun onBindViewHolder(holder: ExampleViewHolder, position: Int) {
val currentItem = exampleList[position]
holder.imageView.setImageResource(currentItem.imageResource)
holder.textViewNomMeuble.text = currentItem.nom
holder.textViewPrix.text = currentItem.prix
holder.textViewStock.text = currentItem.stock.toString()
holder.bind(exampleList[position],listener)
}
override fun getItemCount() = exampleList.size
class ExampleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(meuble:Meuble,listener: (Meuble) -> Unit)= with(itemView)
{
android.util.Log.i("XXXX","FCT Bind ")
setOnClickListener{(listener(meuble))}
}
val imageView: ImageView = itemView.image_view
val textViewNomMeuble: TextView = itemView.nomMeuble
val textViewStock: TextView = itemView.stock
val textViewPrix: TextView = itemView.prix
}
}
我在 SceneFormeActivity.java 中的函数我应该在其中获取项目位置
private void initializeGallery() {
int imageUrl = getIntent().getIntExtra("image_url",0); // here, everytime it return 1
LinearLayout gallery = findViewById(R.id.gallery_layout);
int obj =imageUrl;
switch (obj) {
case 0: {
ImageView andy = new ImageView(this);
andy.setImageResource(R.drawable.fauteilgris);
andy.setContentDescription("fauteuil Créme");
andy.setOnClickListener(view ->{addObject(Uri.parse("foteuil.sfb"));});
gallery.addView(andy);
}
break;
case 1: {
ImageView andy = new ImageView(this);
andy.setImageResource(R.drawable.banc);
andy.setContentDescription("banc extérieur");
andy.setOnClickListener(view ->{addObject(Uri.parse("Bench.sfb"));});
gallery.addView(andy);
}
break;
case 2 : {
ImageView andy = new ImageView(this);
andy.setImageResource(R.drawable.burau);
andy.setContentDescription("bureau");
andy.setOnClickListener(view ->{addObject(Uri.parse("bureau.sfb"));});
gallery.addView(andy);
}
break;
case 3 : {
ImageView andy = new ImageView(this);
andy.setImageResource(R.drawable.fauteuille3);
andy.setContentDescription("Fauteuil une place");
andy.setOnClickListener(view ->{addObject(Uri.parse("ff.sfb"));});
gallery.addView(andy);
}
break;
default:Toast.makeText(getApplicationContext(),"Nothing",Toast.LENGTH_SHORT).show();
break;
}
}
如果您真正想要的只是项目的位置,那么您根本不需要在点击时发送 Meuble。在您的适配器中,将 onBindViewHolder
更改为:
override fun onBindViewHolder(holder: ExampleViewHolder, position: Int) {
val currentItem = exampleList[position]
holder.imageView.setImageResource(currentItem.imageResource)
holder.textViewNomMeuble.text = currentItem.nom
holder.textViewPrix.text = currentItem.prix
holder.textViewStock.text = currentItem.stock.toString()
holder.bind(position,listener)
}
设置您的适配器现在应该如下所示:
mon_recycler.adapter = MeubleAdapter(listMeubles.toTypedArray()){ position ->
val intent3 = Intent(this, SceneformeActivity::class.java)
intent3.putExtra("image_url", position)
startActivity(intent3)
}
编辑:扩展适配器用法:
最初,您将两个东西传递给 Recyclerview 适配器 -
- Meuble 列表(无需将其转换为 TypedArray
- 将在单击列表中的项目时起作用的 lambda
考虑下面的重构代码和我的内嵌注释。注意 - 我不清楚你为什么要发送位置(假设只是为了测试?)所以我假设你希望发送的对象中有一些 imageURL。我在下面展示了您如何访问它。
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_liste_meuble3_d)
listMeubles = generateList()
buildRecyclerView()
}
private fun generateList(): MutableList<Meuble>{
val list = mutableListOf<Meuble>()
// to Add an Object Meuble(nom,prix,stock)
val meuble = Meuble(R.drawable.fauteilgris, "Gris","25000da",1)
val meuble1 = Meuble(R.drawable.fauteuille1, "","1000d&",1)
val meuble2 = Meuble(R.drawable.burau, "Bureau","25000da",1)
list.add(meuble)
list.add(meuble1)
list.add(meuble2)
return list
}
fun buildRecyclerView() {
mon_recycler.setHasFixedSize(true)
mon_recycler.layoutManager = LinearLayoutManager(this)
// NOTE: Here the lambda implicitly had an item of type Meuble passed. We name it here but could have just used "it".
mon_recycler.adapter = MeubleAdapter(listMeubles.toTypedArray())
{ item ->
val intent3 = Intent(this, SceneformeActivity::class.java)
intent3.putExtra("image_url",item.imageUrl)
startActivity(intent3)
}
}
} 我的适配器 Class .kt:
// Don't need to pass as a TypedArray - List is fine
class MeubleAdapter(private val exampleList: List<Meuble>,val listener: (Meuble)-> Unit) :
RecyclerView.Adapter<MeubleAdapter.ExampleViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ExampleViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.meuble_item,
parent, false)
return ExampleViewHolder(itemView)
}
override fun onBindViewHolder(holder: ExampleViewHolder, position: Int) {
val currentItem = exampleList[position]
holder.imageView.setImageResource(currentItem.imageResource)
holder.textViewNomMeuble.text = currentItem.nom
holder.textViewPrix.text = currentItem.prix
holder.textViewStock.text = currentItem.stock.toString()
holder.bind(exampleList[position],listener)
}
override fun getItemCount() = exampleList.size
class ExampleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(meuble:Meuble,listener: (Meuble) -> Unit)= with(itemView)
{
android.util.Log.i("XXXX","FCT Bind ")
setOnClickListener{(listener(meuble))}
}
val imageView: ImageView = itemView.image_view
val textViewNomMeuble: TextView = itemView.nomMeuble
val textViewStock: TextView = itemView.stock
val textViewPrix: TextView = itemView.prix
}
}
所以我使用了这个解决方案,我将我点击的对象的名称(名称)传递给 SceneFormActivity 以获取引用并在下一个 activity:
中显示我的 3D 对象主要代码
val intent3 = Intent(this, SceneformeActivity::class.java)
intent3.putExtra("image_url",it.nom) // it.nom to get the name of the object clicked
startActivity(intent3)
在 SceneformeActivity 中:
private void initializeGallery() {
String imageUrl = getIntent().getStringExtra("image_url"); // changed this from getInt to getString to get the name
LinearLayout gallery = findViewById(R.id.gallery_layout);
String obj =imageUrl;
switch (obj) {
case "fauteilgris": { // case "String": the name of the object clicked in the previous activity(RecyclerView)
ImageView andy = new ImageView(this);
andy.setImageResource(R.drawable.fauteilgris);
andy.setContentDescription("fauteuil Créme");
andy.setOnClickListener(view ->{addObject(Uri.parse("foteuil.sfb"));});
gallery.addView(andy);
}
break;
case "banc": {
ImageView andy = new ImageView(this);
andy.setImageResource(R.drawable.banc);
andy.setContentDescription("banc extérieur");
andy.setOnClickListener(view ->{addObject(Uri.parse("Bench.sfb"));});
gallery.addView(andy);
}
break;
case "burau": {
ImageView andy = new ImageView(this);
andy.setImageResource(R.drawable.burau);
andy.setContentDescription("bureau");
andy.setOnClickListener(view ->{addObject(Uri.parse("bureau.sfb"));});
gallery.addView(andy);
}
break;
default:Toast.makeText(getApplicationContext(),"No Object",Toast.LENGTH_SHORT).show();
break;
}
}