【发布时间】: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)
}
}
}
我现在想要的是当单击列表视图的第一行时,我希望打开“名称”子项,当单击列表视图的第二行时,打开“一个”Json 子项等。 当我实现上面的代码时,所有的列表视图行都会打开 JSON 的最后一个子节点,即“三”。
【问题讨论】:
标签: android json listview kotlin