即时通讯使用arraylist填充微调器,并且该arraylist实际保存来自已存储用于t =我的应用程序的android应用程序数据的文件中的数据, 我粘贴代码,问题是微调器填充但值出现两次
String[] wee = list2.toArray(new String[list2.size()]);
final String[] str={"Report 1","Report 2","Report 3","Report 4","Report 5"};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item);
spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
for(int i = 0;i < wee.length; i++){
spinnerArrayAdapter.add(wee[i]+"\n");
spinnerArrayAdapter.notifyDataSetChanged();
}
// Spinner spinYear = (Spinner)findViewById(R.id.spin);
spin.setAdapter(spinnerArrayAdapter);
答案 0 :(得分:9)
尝试使用构造函数中的数组初始化微调器适配器。
String[] wee = list2.toArray(new String[list2.size()]);
final String[] str={"Report 1","Report 2","Report 3","Report 4","Report 5"};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, wee);
spinnerArrayAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
// Spinner spinYear = (Spinner)findViewById(R.id.spin);
spin.setAdapter(spinnerArrayAdapter);
答案 1 :(得分:1)
private void setSpinner() {
spinner = (Spinner) findViewById(R.id.spinner_size);
Intent intent = getIntent();
ArrayList<String> sizes = null;
if (intent != null) {
// Receiving ArrayList in Another Activity where "sizeList" is the Key
sizes = intent.getStringArrayListExtra("SizeList");
}
if (sizes != null) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, sizes);
spinner.setAdapter(adapter);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
}
}
从OnCreate()方法调用setSpinner()方法。您可以直接将ArrayList作为参数传递,不需要转换为String []。