我正在尝试在标签布局下的片段中并排显示具有两个字符串值的自定义列表视图,但是数据未显示。
当我运行该应用程序时,空白屏幕什么都没有显示。
我遵循了本指南:javatpoint
并尝试将其应用于片段。
CustomAdapter.kt
import android.app.Activity
import android.view.View
import android.view.ViewGroup
import android.view.ViewParent
import android.widget.ArrayAdapter
import android.widget.TextView
class CustomListAdapter(private val context: Activity, private val keyName: Array<String>, private val valueName: Array<String>) : ArrayAdapter<String>(context, R.layout.customer_list_view){
override fun getView(position: Int, view: View?, parent: ViewGroup): View{
val inflater = context.layoutInflater
val rowView = inflater.inflate(R.layout.customer_list_view, null, true)
val keyNameText = rowView.findViewById<TextView>(R.id.keyName)
val valueNameText = rowView.findViewById<TextView>(R.id.valueName)
keyNameText.text = keyName[position]
valueNameText.text = valueName[position]
return rowView
}
}
FragmentSoftware.kt
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ListView
class FragmentSoftware : Fragment(){
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val fragmentVar: View = inflater.inflate(R.layout.software_fragment, container, false)
val names = arrayOf("Tesla", "BMW", "Audi", "Tata", "Ford", "Honda", "Jaguar", "Mustang", "Mercedes", "Lamborghini", "Tesla", "BMW", "Audi", "Tata", "Ford", "Honda", "Jaguar", "Mustang", "Mercedes", "Lamborghini")
val values = arrayOf("Tesla", "BMW", "Audi", "Tata", "Ford", "Honda", "Jaguar", "Mustang", "Mercedes", "Lamborghini", "Tesla", "BMW", "Audi", "Tata", "Ford", "Honda", "Jaguar", "Mustang", "Mercedes", "Lamborghini")
val fragmentListView = fragmentVar.findViewById<ListView>(R.id.listView2)
fragmentListView.adapter = CustomListAdapter(requireActivity(), names, values)
return fragmentVar
}
}
software_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView2"/>
</LinearLayout>
custom_list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/keyName"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textStyle="bold"
android:textColor="#000000"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/valueName"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textStyle="bold"
android:textColor="#000000"/>
</LinearLayout>
答案 0 :(得分:2)
您仍然需要覆盖适配器中的某些方法和最重要的getCount()
//1
override fun getCount(): Int {
return keyName.size
}
//2
override fun getItem(position: Int): Any {
return keyName[position]
}
//3
override fun getItemId(position: Int): Long {
return position.toLong()
}
答案 1 :(得分:1)
您可以将RecyclerView与GridLayoutManager一起使用。您可以增加所需的跨度计数。 Clone this :)幸福代码:)