java Android 动态微调器更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5453580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Dynamic Spinner Update
提问by user591162
I'm working with Android and Spinners and I need some help. I have a class that creates two spinners and a button. The first spinner is for my category, my second is for my sub-category. What I am trying to do is dynamically update the second spinner (spinner2). I've been trying to use adapter2.clear() but that crashes android, with an error "unable to start activity componentinfo unsupported operation"
我正在使用 Android 和 Spinner,我需要一些帮助。我有一个创建两个微调器和一个按钮的类。第一个微调器用于我的类别,第二个微调器用于我的子类别。我想要做的是动态更新第二个微调器 (spinner2)。我一直在尝试使用adapter2.clear() 但它使android 崩溃,并出现错误“无法启动活动组件信息不受支持的操作”
Here is my code:
这是我的代码:
public class MyClass extends MyBaseClass
{
int category;
int sub_category;
ArrayAdapter<String> adapter2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quizes);
//CATEGORY INFO
final String[] items1 = new String[] {"One", "Two", "Three"};
final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items1);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
//SUBCATEGORY INFO
final String[] items2 = new String[] {"SOne", "STwo", "SThree"};
final Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
adapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items2);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
// Capture our button from layout
Button button = (Button)findViewById(R.id.button1);
// Register the onClick listener with the implementation above
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
startActivity(new Intent(MyClass.this, GoToOtherClass.class));
}
});
//SELECTOR CONTROL FOR SPINNER1 {CATEGORY}
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
MyClass.this.category = spinner1.getSelectedItemPosition();
//OTHER STUFF
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
//SELECTOR CONTROL FOR SPINNER2 {SUB-CATEGORY}
spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
MyClass.this.sub_category = spinner2.getSelectedItemPosition();
//OTHER STUFF
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
return true;
}
}
I understand about the .clear()/.add() methods, but anytime I try clear()/add() my program crashes, what do I need to do to fix things so I can change the spinner2 contents for my sub-category list? Any advice would help, as I have spent hours doing things such as:
我了解 .clear()/.add() 方法,但是每当我尝试 clear()/add() 时,我的程序都会崩溃,我需要做些什么来修复问题,以便我可以更改我的子程序的 spinner2 内容类别清单?任何建议都会有所帮助,因为我花了几个小时做以下事情:
Object t=adapter2.getitem(0); spinner2.remove((String) t);
对象 t=adapter2.getitem(0); spinner2.remove((String) t);
or adapter2.clear() and a few other tricks and I have no further ideas left. I am still learning android. I've tried looking at some other posts here on stackoverflow and google but was not sure how to get their ideas working.
或 adapter2.clear() 和其他一些技巧,我没有进一步的想法了。我还在学习安卓。我已经尝试在 stackoverflow 和 google 上查看其他一些帖子,但不确定如何让他们的想法发挥作用。
回答by BigFwoosh
After you change the contents of the second Spinner, you need to call adapter2.notifyDataSetChanged()
. Without that call, the UI won't update with the new contents of the Spinner, and you could also have problems referencing things that don't exist anymore.
更改第二个 Spinner 的内容后,您需要调用adapter2.notifyDataSetChanged()
. 如果没有该调用,UI 将不会使用 Spinner 的新内容进行更新,并且您在引用不再存在的内容时也可能会遇到问题。
回答by Balaji
Try this Code..
试试这个代码..
public class MainActivity extends Activity {
Spinner sp1,sp2;
ArrayAdapter<String> adp1,adp2;
List<String> l1,l2;
int pos;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l1=new ArrayList<String>();
l1.add("A");
l1.add("B");
sp1= (Spinner) findViewById(R.id.spinner1);
sp2= (Spinner) findViewById(R.id.spinner2);
adp1=new ArrayAdapter<String> (this,android.R.layout.simple_dropdown_item_1line,l1);
adp1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp1.setAdapter(adp1);
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
pos=arg2;
add();
}
private void add() {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), ""+pos, Toast.LENGTH_SHORT).show();
switch(pos)
{
case 0:
l2= new ArrayList<String>();
l2.add("A 1");
l2.add("A 2");
adp2=new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_dropdown_item_1line,l2);
adp2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp2.setAdapter(adp2);
select();
break;
case 1:
l2= new ArrayList<String>();
l2.add("B 1");
l2.add("B 2");
adp2=new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_dropdown_item_1line,l2);
adp2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp2.setAdapter(adp2);
select();
break;
}
}
private void select() {
// TODO Auto-generated method stub
sp2.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Test "+arg2, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}