我正在使用Kotlin开发简单的文章Android应用程序。我为所有列表创建了ListView和详细信息视图。我在Firebase数据库中上传了数据。我需要的是当我点击每个listiew时,我想得到childView基于它的position.Here是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)
}
}
}
} val data: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
}
}
以下是上面列表的详细视图..
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)
}
}
}
我现在想要的是当点击listview的第一行时我想打开'Name'子项,当点击listview的第二行然后'One'Json Child打开等。 当我实现上面的代码时,所有列表视图行都会打开JSON的最后一个Child,即'Three'。
答案 0 :(得分:0)
我从我的一个朋友那里得到了解决方案。我应该创建一个列表视图,其中包含One Activity中的所有列表和子列表。可能有人需要代码......这里是......
MainListView名为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
}
}
这是主要的细节活动......
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)
}
}
}