我有一个微调器,显示县名,图像和索引号。 现在,我想在列表视图中显示微调器的“选定项目”。 我正在获取所选微调器项目的位置,但该位置项目没有显示在列表视图中。我在res / raw文件夹中有couuntries.txt文件,在drawable中有图像
如果我要连接新的适配器,必须传递哪些参数。此外,如何在列表视图中显示所有3个项目
MainActivity.kt
class MainActivity : AppCompatActivity() {
var countryname: MutableList<String> = mutableListOf()
var countryflag: MutableList<Int> = mutableListOf()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
readfile()
// spinnerCustomlist()
}
private lateinit var myCustomAdapter: CustomAdapter
fun readfile() {
var imagecode: Int? = null
var nam: String? = null
var name: String? = null
var code: String? = null
var instream = resources.openRawResource(R.raw.countries)
var scan = Scanner(instream)
while (scan.hasNext()) {
nam = scan.nextLine()
code = nam.substring(0, 2)
code = code.toLowerCase()
name = nam.substring(3)
imagecode = resources.getIdentifier("$code", "drawable", "com.example.dell.custom_spinner_flags")
if (imagecode > 0) {
countryname.add(name)
countryflag.add(imagecode)
}
}
myCustomAdapter = CustomAdapter(applicationContext, countryname, countryflag)
spinner2.adapter = myCustomAdapter
lists.x
spinner2?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onNothingSelected(parent: AdapterView<*>?) {
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
// val List: Int = parent!!.getItemAtPosition(position) ////////////////////Code//////////////////////
} } }}
用于微调器的CustomAdapter.kt
class CustomAdapter(context: Context, var countryname : MutableList<String>, var countryflag: MutableList<Int>) : BaseAdapter() {
val layoutInflater: LayoutInflater = LayoutInflater.from(context)
override fun getItem(position: Int): Any? {
return null //To change body of created functions use File | Settings | File Templates.
}
override fun getItemId(position: Int): Long {
return 0 //To change body of created functions use File | Settings | File Templates.
}
override fun getCount(): Int {
return countryname.size //To change body of created functions use File | Settings | File Templates.
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val rowView = layoutInflater.inflate(R.layout.custom,parent, false)
val name: TextView = rowView.findViewById(R.id.textView)
val number: TextView = rowView.findViewById(R.id.textView2)
val imageview: ImageView = rowView.findViewById(R.id.Image)
name.text = countryname[position]
imageview.setImageResource(countryflag[position])
var num = position
num += 1
number.text = num.toString()
return rowView
}
}
ListViewAdapter.kt
class ListViewdisplay
(context: Context, ? ///////// ) :
ArrayAdapter<String>
(context, R.layout.activity_list_viewdisplay,x) {
@SuppressLint("ViewHolder")
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val layoutInflater: LayoutInflater = LayoutInflater.from(context)
val rowView = layoutInflater.inflate(R.layout.activity_list_viewdisplay, parent, false)
val list: ListsView = rowView.findViewById(R.id.list)
////////////////////////////////
return rowView
}
}