ArrayAdapter
我尽可能多地扩展了哪些保存器的功能,只需将长描述设置为所选项目。
首先,我们需要一个新模型来为适配器供电。请注意,toString()
返回shortDescription
以便ArrayAdapter
在下拉列表中显示简短描述:
data class DescriptiveArrayItem(val shortDescription: String, val longDescription: String) {
override fun toString(): String {
return shortDescription
}
}
其次,我们创建了我们的自定义适配器,它getView()
在选择项目时覆盖并显示长描述(不幸的是,其中的许多字段和方法ArrayAdapter
是私有的,所以我不得不复制它们):
class DescriptiveArrayAdapter : ArrayAdapter<DescriptiveArrayItem> {
private var mResource: Int
private val mFieldId: Int
private val mContext: Context
private val mInflater: LayoutInflater
constructor(context: Context, resource: Int) : super(context, resource) {
mContext = context
mFieldId = 0
mInflater = LayoutInflater.from(context)
mResource = resource
}
constructor(context: Context, resource: Int, textViewResourceId: Int) : super(context, resource, textViewResourceId) {
mContext = context
mFieldId = textViewResourceId
mInflater = LayoutInflater.from(context)
mResource = resource
}
constructor(context: Context, resource: Int, objects: Array<out DescriptiveArrayItem>) : super(context, resource, objects) {
mContext = context
mFieldId = 0
mInflater = LayoutInflater.from(context)
mResource = resource
}
constructor(context: Context, resource: Int, textViewResourceId: Int, objects: Array<out DescriptiveArrayItem>) : super(context, resource, textViewResourceId, objects) {
mContext = context
mFieldId = textViewResourceId
mInflater = LayoutInflater.from(context)
mResource = resource
}
constructor(context: Context, resource: Int, objects: List<DescriptiveArrayItem>) : super(context, resource, objects) {
mContext = context
mFieldId = 0
mInflater = LayoutInflater.from(context)
mResource = resource
}
constructor(context: Context, resource: Int, textViewResourceId: Int, objects: List<DescriptiveArrayItem>) : super(context, resource, textViewResourceId, objects) {
mContext = context
mFieldId = textViewResourceId
mInflater = LayoutInflater.from(context)
mResource = resource
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
return createViewFromResource(mInflater, position, convertView, parent, mResource)
}
private fun createViewFromResource(inflater: LayoutInflater, position: Int,
convertView: View?, parent: ViewGroup, resource: Int): View {
val text: TextView?
val view: View = convertView ?: inflater.inflate(resource, parent, false)
try {
if (mFieldId == 0) {
// If no custom field is assigned, assume the whole resource is a TextView
text = view as TextView
} else {
// Otherwise, find the TextView field within the layout
text = view.findViewById(mFieldId)
if (text == null) {
throw RuntimeException("Failed to find view with ID "
+ mContext.resources.getResourceName(mFieldId)
+ " in item layout")
}
}
} catch (e: ClassCastException) {
Log.e("ArrayAdapter", "You must supply a resource ID for a TextView")
throw IllegalStateException(
"ArrayAdapter requires the resource ID to be a TextView", e)
}
val item: DescriptiveArrayItem? = getItem(position)
text.text = item?.longDescription
return view
}
}