• 全部
  • 音视频
  • Camera
  • UI
  • 协议
  • SDK
  • Android Studio
  • 问答

哪位大神指导为什么y获取到的值是空,n的值是对的??

weixin_45202484 四川文理学院 2020-12-21 10:16:44
//获取系统时间
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()
...全文
2 点赞 收藏 回复
写回复
回复
切换为时间正序
请发表友善的回复…
发表回复

还没有回复,快来抢沙发~

发帖
Android
创建于2009-10-09

7.8w+

社区成员

移动平台 Android
申请成为版主
帖子事件
创建了帖子
2020-12-21 10:16
社区公告
暂无公告