Cómo recuperar Objeto jSON secundario en Android Cuando se hace clic en un ListView

Estoy trabajando en una aplicación de artículo simple de Android usando Kotlin. He creado un ListView y una vista de detalle para todas las lists. Cargué los datos en la database de Firebase. Lo que necesito es que cuando hago clic en cada list, quiero get el childView basado en su position. Aquí está el ListView …

> class uno: AppCompatActivity () {

override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.listview) val LV = findViewById(R.id.LV) as ListView val adapter = CustomListAdapter(this,R.layout.customlistlayout,data) LV.adapter = adapter LV.onItemClickListener = AdapterView.OnItemClickListener { _, _, position, _ -> when(position) { 0 -> { val intent = Intent(this@One, detailOne::class.java) startActivity(intent) } 1 -> { val intent = Intent(this@One ,detailOne:: class.java) startActivity(intent) } } } 

} val datos: ArrayList

  get() { var list : ArrayList<CustomListLayout> = ArrayList<CustomListLayout>() list.add(CustomListLayout("You are almighty God")) list.add(CustomListLayout("This is It!")) list.add(CustomListLayout("What a Blessing!")) list.add(CustomListLayout("We do love..")) list.add(CustomListLayout("That's what a great personality says")) list.add(CustomListLayout("Creative life")) list.add(CustomListLayout("I know what you did last night")) list.add(CustomListLayout("Great King")) return list } } 

y Aquí está la vista de detalles para la list de arriba ..

class detailOne: AppCompatActivity () {

 private var mTextView : TextView? = null companion object { internal var alreadyCalled = false } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.detailone) val myButton = findViewById(R.id.butOne) as Button mTextView = findViewById(R.id.myText) as TextView if(! alreadyCalled) { FirebaseDatabase.getInstance().setPersistenceEnabled(true) alreadyCalled = true } FirebaseApp.initializeApp(this) var database = FirebaseDatabase.getInstance() var myRef = database.getReference("Player") var myText = myRef.push().key myRef.addValueEventListener(object: ValueEventListener { val TAG: String? = null override fun onDataChange(dataSnapshot: DataSnapshot) { for(postSnapShot in dataSnapshot.children){ val playerName: String = postSnapShot.getValue(String::class.java) mTextView?.text = playerName } if(myText == null) { Log.w(TAG,"User data is null") return } } override fun onCancelled(error: DatabaseError) { Log.w(TAG, "Failed to read Value", error.toException()) } }) myButton.setOnClickListener { val myIntent = Intent() myIntent.action = Intent.ACTION_SEND myIntent.type = "text/plain" // In java type is called as setType... myIntent.putExtra(Intent.EXTRA_TEXT,mTextView?.text) startActivity(myIntent) } } 

}

y aquí está el file JSON en la database de Firebase. enter image description here

Lo que quiero ahora es Cuando se hace clic en la primera fila de la list, quiero abrir el Niño 'Nombre', cuando se hace clic en la segunda fila de la vista de list, luego se abre 'Uno' Json Child, etc. Cuando implemente el código anterior, todos las filas de la vista de list abren el último elemento secundario de JSON, que es 'Tres'.

Obtuve la solución de un amigo mío. Debería crear una vista de list que contenga todas las lists y lists secundarias dentro de One Activity. Quizás alguien necesite el código … aquí está …

The MainListView llamado one.kt

 class One : AppCompatActivity() { var myParams : AbsListView.LayoutParams?= null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.listview) val LV = findViewById(R.id.LV) as ListView val whenTextEmpty = findViewById(R.id.tez) as TextView val progressBar = findViewById(R.id.zProgress) as ProgressBar LV.emptyView = whenTextEmpty LV.emptyView = progressBar val childArrayHolder = ArrayList<String>() val mainLister = ArrayList<String>() var databaseReference = FirebaseDatabase.getInstance().getReference("zOne") databaseReference.addChildEventListener(object : ChildEventListener{ override fun onCancelled(p0: DatabaseError?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } override fun onChildMoved(p0: DataSnapshot?, p1: String?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } override fun onChildChanged(p0: DataSnapshot?, p1: String?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } override fun onChildAdded(p0: DataSnapshot?, p1: String?) { val playerName: String = p0!!.getValue(String::class.java) childArrayHolder.add(playerName) LV.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id -> val intent = Intent(this@One, mainDetail::class.java) intent.putExtra("key",childArrayHolder[position]) startActivity(intent) } } override fun onChildRemoved(p0: DataSnapshot?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } }) databaseReference.addValueEventListener( object : ValueEventListener{ override fun onDataChange(p0: DataSnapshot?) { for(player in p0!!.children){ Log.i("zOne",player.key) mainLister.add(player.key) } } override fun onCancelled(p0: DatabaseError?) { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } }) val arrayAdapter = object : ArrayAdapter<String>(this,android.R.layout.simple_selectable_list_item,mainLister){ override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { var view : View = super.getView(position, convertView, parent) myParams = view.layoutParams as AbsListView.LayoutParams? var textView = view.findViewById(android.R.id.text2) as TextView textView.textSize = 20F textView.setTextColor(Color.BLACK) view.layoutParams = myParams return view } } LV.adapter = arrayAdapter } 

}

Y aquí está la principal actividad de detalles …

  class mainDetail : AppCompatActivity() { private var mTextView : TextView? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.detailone) val myButton = findViewById(R.id.butOne) as Button mTextView = findViewById(R.id.myText) as TextView val intent = intent if(intent.hasExtra("key")){ mTextView?.text = intent.getStringExtra("key") } myButton.setOnClickListener { val myIntent = Intent() myIntent.action = Intent.ACTION_SEND myIntent.type = "text/plain" // In java type is called as setType... myIntent.putExtra(Intent.EXTRA_TEXT,mTextView?.text) startActivity(myIntent) } } 

}