如果我们在微调器中有一些项目(如下所示)我们如何在运行时添加额外的项目
这是代码:
String[] Items = {
"Alarm",
"Office",
"Meeting",
"Party",
"Lunch",
"Breakfast",
"Supper",
"Home",
"Private",
"Outdoor",
"Family",
"Friends",
"Others"
};
Spinner s1;
和
s1 = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item,Items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3)
{
int index = s1.getSelectedItemPosition();
Toast.makeText(getBaseContext(),
"You have selected item : " + Items[index],
Toast.LENGTH_SHORT).show();
if (index==12)
{
EditText edit = (EditText) findViewById(R.id.edittext);
Button add=(Button) findViewById(R.id.add);
edit.setVisibility(View.VISIBLE);
add.setVisibility(View.VISIBLE);
}
else
{
EditText edit = (EditText) findViewById(R.id.edittext);
Button add=(Button) findViewById(R.id.add);
edit.setVisibility(View.GONE);
add.setVisibility(View.GONE);
}
}
public void onNothingSelected(AdapterView<?> arg0) {}
});
}
如果选择“其他”意味着我们要在微调器列表中添加额外的项目..
谢谢,
答案 0 :(得分:3)
我希望你能在用户选择“其他人”时检测到这种情况
在这种情况下,在Items数组中添加其他值并调用函数adapter.notifyDataSetChanged()
它应该适应更改,您应该在微调器中看到新值。
尝试一下并恢复原状。
答案 1 :(得分:1)
您可以创建BaseAdapter,在运行时填充它并将适配器分配给微调器。
答案 2 :(得分:0)
以下为我效劳:
/* Read values from resource into an array */
String[] strColorValues = getResources().getStringArray(R.array.colors);
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < strColorValues.length; i++) {
list.add(strColorValues[i]);
}
ArrayAdapter adapterColors = new ArrayAdapter(getActivity(), android.R.layout.simple_spinner_item, list);
adapterColors.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerColors.setAdapter(adapterPermissionLevels);
spinnerColors.setOnItemSelectedListener(this);
/* Remove first element from the adapter and notify dataset changed. */
String item = spinnerColors.getItemAtPosition(0).toString();
adapterColors.remove(item);
adapterColors.notifyDataSetChanged();