哪位大神指导为什么y获取到的值是空,n的值是对的??
//获取系统时间
val simpleDateFormat1 = SimpleDateFormat("yyyy")
val date1 = Date(System.currentTimeMillis())
var datay=simpleDateFormat1.format(date1).toInt()
//time?.setText(simpleDateFormat.format(date))
val simpleDateFormat2 = SimpleDateFormat("MM")
val date2 = Date(System.currentTimeMillis())
val datam=simpleDateFormat2.format(date2).toInt()
// "获取控件"
val sp_year: Spinner = findViewById(R.id.sp_year)
val sp_month: Spinner = findViewById(R.id.sp_month)
//为Spinner加载数据
"1.准备数据"
val year = arrayOf(datay,datay-1,datay-2)
val month = arrayOf(
arrayOf(datam,1,2,3,4,5,6,7,8,9,10,11,12),
arrayOf(1,2,3,4,5,6,7,8,9,10,11,12),
arrayOf(1,2,3,4,5,6,7,8,9,10,11,12)
)
"2.把数据载入到数据适配器 参数一上下文 参数二每一行格式 参数三数据"
val year_adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, year)
"3.把数据适配器给Spinner对象"
sp_year.adapter = year_adapter
//为Spinner设置监听响应事件
sp_year.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {}
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {
sp_year_positon = position
//position:Int 指Spinner每一项的位置id
"将city的数据通过适配器给spinner"
val month_adapter = ArrayAdapter(
this@MainActivity,
android.R.layout.simple_spinner_item,
month[position]
)
sp_month.adapter = month_adapter
//Toast.makeText(this@ThirdActivity, country[position], Toast.LENGTH_LONG).show()
}
}
//为Spinner设置监听响应事件
sp_month.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onNothingSelected(parent: AdapterView<*>?) {}
override fun onItemSelected(
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) {}
}
var n = sp_year.getSelectedItem().toString()
var y = sp_month.getSelectedItem().toString()
getalldata(n, y)
setAdapter()
Toast.makeText(this,n,Toast.LENGTH_SHORT).show()
Toast.makeText(this,y,Toast.LENGTH_SHORT).show()