【问题标题】:How to Retrieve Child jSON Object in Android When a listView is clicked单击listView时如何在Android中检索子jSON对象
【发布时间】:2017-09-05 10:09:21
【问题描述】:

我正在使用 Kotlin 开发简单的文章 Android 应用程序。我为所有列表创建了一个列表视图和一个详细信息视图。我将数据上传到了 Firebase 数据库中。我需要的是当我点击每个列表时,我想得到childView 基于其位置。这里是 ListView...

> 第一类: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)
             }
    }
     }

} 验证数据: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
            }
        }

这是上面列表的详细视图..

类 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)


    }

    }

}

这是 Firebase 数据库中的 JSON 文件。

我现在想要的是当单击列表视图的第一行时,我希望打开“名称”子项,当单击列表视图的第二行时,打开“一个”Json 子项等。 当我实现上面的代码时,所有的列表视图行都会打开 JSON 的最后一个子节点,即“三”。

【问题讨论】:

    标签: android json listview kotlin


    【解决方案1】:

    我从我的一个朋友那里得到了解决方案。我应该创建一个列表视图,其中包含 One Activity 中的所有列表和子列表。也许有人需要代码...这里是...

    名为 one.kt 的 MainListView

    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
    
    
        }
    

    }

    这是主要的细节活动......

     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)
    
    
        }
    
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多