我有一个微调器,我想在其中显示字符串列表(例如语言);我尝试了很多方法,但是都没有用。现在,我陷入了这个唯一的错误。关于ArrayAdapted,“以下任何函数都不能使用提供的参数来调用”。我尝试了这篇帖子Link to a similar question中提到的技巧。
val arrayAdapter = ArrayAdapter(this,android.R.layout.simple_spinner_item, statuts.toList())
这:
val arrayAdapter = ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, statuts.toList())
这:
val arrayAdapter = ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, statuts)
但是它不起作用。请帮助我,我是Kotlin的新手。
class FirstFragment : Fragment(),AdapterView.OnItemSelectedListener {
// attributes
var statuts = arrayOf("English", "French", "Spanish")
var spinner:Spinner? = null
var textView_msg:TextView? = null
// methods
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
textView_msg = msg
spinner = this.statuts_sp
spinner!!.setOnItemSelectedListener(this)
val arrayAdapter = ArrayAdapter(this,android.R.layout.simple_spinner_item, statuts.toList())
// Set layout to use when the list of choices appear
arrayAdapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
// Set Adapter to Spinner
statuts_sp!!.adapter = aa
// Inflate the layout for this fragment
return inflater!!.inflate(R.layout.fragment_first, container, false)
}
/*************************************************************************************/
override fun onItemSelected(arg0: AdapterView<*>, arg1: View, position: Int, id: Long) {
textView_msg!!.text = "Selected : "+statuts[position]
}
/*************************************************************************************/
override fun onNothingSelected(arg0: AdapterView<*>) {}
}
答案 0 :(得分:2)
在片段中使用数组适配器时,第一个参数必须是来自活动的当前类上下文。
替换此行
val aa = ArrayAdapter(this,android.R.layout.simple_spinner_item, statuts.toList())
收件人:
var list = ArrayList<String>()
val aa = ArrayAdapter(activity,android.R.layout.simple_spinner_item,list)
答案 1 :(得分:2)
尝试:
您的主要活动中:
import android.content.Intent
import android.os.Bundle
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity;
import android.view.View
import android.widget.ArrayAdapter
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_logged.*
import kotlinx.android.synthetic.main.content_logged.*
import kotlinx.android.synthetic.main.content_main.*
class logged : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_logged)
setSupportActionBar(toolbar)
// Create an ArrayAdapter
val adapter = ArrayAdapter.createFromResource(this,
R.array.city_list, android.R.layout.simple_spinner_item)
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item)
// Apply the adapter to the spinner
spinner.adapter = adapter
}
fun getValues(view: View) {
Toast.makeText(this, "Spinner 1 " + spinner.selectedItem.toString()
, Toast.LENGTH_LONG).show()
}
}
我将项目列表放在 strings.xml
中<string-array name="city_list">
<item>Bangkok</item>
<item>London</item>
<item>Paris</item>
<item>Singapore</item>
<item>New York</item>
<item>Istanbul</item>
<item>Dubai</item>
<item>Kuala Lumpur</item>
<item>Hong Kong</item>
<item>Barcelona</item>
</string-array>
答案 2 :(得分:1)
检查此
val countryAdapter = ArrayAdapter<Country>(
this, R.layout.dialog_row_place, countryArrayList
)
countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
mListView.setAdapter(countryAdapter)
布局dialog_row_place是列表项的自定义xml文件
答案 3 :(得分:1)
问题::ArrayAdapter (Context context,
int resource,
List objects)收到一个Context
作为第一个参数,但是您将this
作为Fragment
传递了(不是{{1的子类}})作为第一个参数。
解决方案:从以下位置更改代码
Context
到
val aa = ArrayAdapter(this,android.R.layout.simple_spinner_item, statuts.toList())