我的第二个微调器无法选择项目并保存值。
如果我添加s2.setOnItemSelectedListener(this);
我的微调器不会选择或滚动。
如果我删除了监听器,则启用了项目选择,但它没有抓住选择,有人能告诉我我缺少的是什么吗?
感谢
public class MenuTiendas extends Activity implements
OnItemSelectedListener{
Spinner s1,s2,s3;
String sp1, sp2, sp3 = "";
String ID, id_pasajero = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.menutiendas);
s1 = (Spinner)findViewById(R.id.spinner1);
s2 = (Spinner)findViewById(R.id.spinner2);
//s3 = (Spinner)findViewById(R.id.spinner3);
s1.setOnItemSelectedListener(this);
s2.setOnItemSelectedListener(this);
//s3.setOnItemSelectedListener(this);
SharedPreferences prefs = getSharedPreferences("Sea_reg_app", Context.MODE_PRIVATE);
ID = prefs.getString("ID", "");
id_pasajero = prefs.getString("ID_own","");
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
sp1= String.valueOf(s1.getSelectedItem());
sp2= String.valueOf(s2.getSelectedItem());
//sp3= String.valueOf(s3.getSelectedItem());
Button siguiente = (Button) this.findViewById(R.id.button7);
//Toast.makeText(this, sp1, Toast.LENGTH_SHORT).show();
if(sp1.contentEquals("SUPERAMA")) {
List<String> list = new ArrayList<String>();
list.add("ACE RELL DE ALMNDRS (FCO)");
list.add("ACE RELL DE AJO (FCO)");
list.add("ACE RELL ARANDANOS (FCO)");
list.add("ACE RELL HIGO (FCO)");
list.add("AJO FINAS HIERBAS (FCO)");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
s2.setAdapter(dataAdapter);
}
if(sp1.contentEquals("CITY FRESKO (COMER)")) {
List<String> list = new ArrayList<String>();
list.add("ACE RELL ALMNDRS (FCO)");
list.add("ACE RELL AJO (FCO)");
list.add("ACE RELL PIMIENTO ROJO (FCO)");
list.add("ACE RELL COCO DULCE (FCO)");
list.add("ACE RELL ARANDANOS (FCO)");
list.add("ACE RELL DATIL DULCE (FCO)");
list.add("ACE RELL HIGO (FCO)");
list.add("ACE RELL DE PASAS DULCE (FCO)");
list.add("ACE RELL DE PINA (FCO)");
list.add("AJO FINAS HIERBAS (FCO)");
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
s2.setAdapter(dataAdapter2);
}
if(sp1.contentEquals("CITY FRESKO (FRESKO)")) {
List<String> list = new ArrayList<String>();
list.add("ACE RELL ALMNDRS (FCO)");
list.add("ACE RELL AJO (FCO)");
list.add("ACE RELL PIMIENTO ROJO (FCO)");
list.add("ACE RELL COCO DULCE (FCO)");
list.add("ACE RELL ARANDANOS (FCO)");
list.add("ACE RELL DATIL DULCE (FCO)");
list.add("ACE RELL HIGO (FCO)");
list.add("ACE RELL PASAS DULCE (FCO)");
list.add("ACE RELL PINA (FCO)");
list.add("AJO FINAS HIERBAS (FCO)");
ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter3.notifyDataSetChanged();
s2.setAdapter(dataAdapter3);
}
if(sp1.contentEquals("CITY FRESKO (C.MARKET)")) {
List<String> list = new ArrayList<String>();
list.add("ACE RELL ALMNDRS (FCO)");
list.add("ACE RELL AJO (FCO)");
list.add("ACE RELL PIMIENTO ROJO (FCO)");
list.add("ACE RELL COCO DULCE (FCO)");
list.add("ACE RELL ARANDANOS (FCO)");
list.add("ACE RELL DE DATIL DULCE (FCO)");
list.add("ACE RELL HIGO (FCO)");
list.add("ACE RELL PASAS DULCE (FCO)");
list.add("ACE RELL PINA (FCO)");
list.add("AJO FINAS HIERBAS (FCO)");
ArrayAdapter<String> dataAdapter4 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter4.notifyDataSetChanged();
s2.setAdapter(dataAdapter4);
}
if(sp1.contentEquals("CITY FRESKO (SUMESA)")) {
List<String> list = new ArrayList<String>();
list.add("ACE RELL ALMNDRS (FCO)");
list.add("ACE RELL AJO (FCO)");
list.add("ACE RELL PIMIENTO ROJO (FCO)");
list.add("ACE RELL COCO DULCE (FCO)");
list.add("ACE RELL ARANDANOS (FCO)");
list.add("ACE RELL DATIL DULCE (FCO)");
list.add("ACE RELL. HIGO (FCO)");
list.add("ACE RELL PASAS DULCE (FCO)");
list.add("ACE RELL PINA (FCO)");
list.add("AJO FINAS HIERBAS (FCO)");
ArrayAdapter<String> dataAdapter5 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter5.notifyDataSetChanged();
s2.setAdapter(dataAdapter5);
}
if(sp1.contentEquals("CHEDRAUI")) {
List<String> list = new ArrayList<String>();
list.add("ACE RELL ALMENDRAS (FCO)");
list.add("ACE RELL COCO DULCE (FCO)");
list.add("ACE RELL HIGO (FCO)");
list.add("ACE RELL PASAS DULCE (FCO)");
list.add("AJO FINAS HIERBAS (FCO)");
ArrayAdapter<String> dataAdapter6 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter6.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter6.notifyDataSetChanged();
s2.setAdapter(dataAdapter6);
}
siguiente.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("LOG", "resultado sp1 " + sp1);
Log.d("LOG", "resultado sp2 " + sp2);
//leemos status de usuario y determinamos a donde ir.
SharedPreferences prefs = getSharedPreferences("Sea_reg_app", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("sp1", sp1.toString());
editor.putString("sp2", sp2.toString());
//editor.putString("sp3", sp3.toString());
//cbMarcame1_1 = prefs.getBoolean("Guardar_Vincular",false);
editor.commit();
startActivity(new Intent(MenuTiendas.this, formulario.class));
}
});
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
以下是此活动的XML代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dip"
android:text="@string/lblAcc"
android:layout_marginTop="30dp"/>
<Spinner
android:id="@+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
android:layout_marginTop="20dp"
android:entries="@array/tienda" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dip"
android:text="@string/lblSubAcc1"
android:layout_marginTop="20dp" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip"
/>
<Button
android:text="Siguiente"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button7"
android:layout_marginTop="20dp"/>
</LinearLayout>
答案 0 :(得分:0)
我不明白这一部分:
如果我添加s2.setOnItemSelectedListener(this);我的旋转器不会选择或 滚动。如果我删除了监听器,则启用项目选择但是 它没有抓住选择
我尝试重现您的错误,但是使用您的代码,我可以选择或滚动Spinner2
,但内容将保持不变。这个原因是因为您未指定Spinner
中onItemSelected
代码中的Spinner2
代码。因此,如果您选择Spinner1
,则会检查Spinner2
并替换Spinner2
的数据。这就是为什么@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
sp1 = String.valueOf(s1.getSelectedItem());
sp2 = String.valueOf(s2.getSelectedItem());
//sp3= String.valueOf(s3.getSelectedItem());
Button siguiente = (Button) this.findViewById(R.id.button7);
//Toast.makeText(this, sp1, Toast.LENGTH_SHORT).show();
if (arg0.getId() == R.id.spinner1) {
if (sp1.contentEquals("SUPERAMA")) {
List<String> list = new ArrayList<>();
list.add("ACE RELL DE ALMNDRS (FCO)");
list.add("ACE RELL DE AJO (FCO)");
list.add("ACE RELL ARANDANOS (FCO)");
list.add("ACE RELL HIGO (FCO)");
list.add("AJO FINAS HIERBAS (FCO)");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
s2.setAdapter(dataAdapter);
}
if (sp1.contentEquals("CITY FRESKO (COMER)")) {
List<String> list = new ArrayList<String>();
list.add("ACE RELL ALMNDRS (FCO)");
list.add("ACE RELL AJO (FCO)");
list.add("ACE RELL PIMIENTO ROJO (FCO)");
list.add("ACE RELL COCO DULCE (FCO)");
list.add("ACE RELL ARANDANOS (FCO)");
list.add("ACE RELL DATIL DULCE (FCO)");
list.add("ACE RELL HIGO (FCO)");
list.add("ACE RELL DE PASAS DULCE (FCO)");
list.add("ACE RELL DE PINA (FCO)");
list.add("AJO FINAS HIERBAS (FCO)");
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
s2.setAdapter(dataAdapter2);
}
if (sp1.contentEquals("CITY FRESKO (FRESKO)")) {
List<String> list = new ArrayList<String>();
list.add("ACE RELL ALMNDRS (FCO)");
list.add("ACE RELL AJO (FCO)");
list.add("ACE RELL PIMIENTO ROJO (FCO)");
list.add("ACE RELL COCO DULCE (FCO)");
list.add("ACE RELL ARANDANOS (FCO)");
list.add("ACE RELL DATIL DULCE (FCO)");
list.add("ACE RELL HIGO (FCO)");
list.add("ACE RELL PASAS DULCE (FCO)");
list.add("ACE RELL PINA (FCO)");
list.add("AJO FINAS HIERBAS (FCO)");
ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter3.notifyDataSetChanged();
s2.setAdapter(dataAdapter3);
}
if (sp1.contentEquals("CITY FRESKO (C.MARKET)")) {
List<String> list = new ArrayList<String>();
list.add("ACE RELL ALMNDRS (FCO)");
list.add("ACE RELL AJO (FCO)");
list.add("ACE RELL PIMIENTO ROJO (FCO)");
list.add("ACE RELL COCO DULCE (FCO)");
list.add("ACE RELL ARANDANOS (FCO)");
list.add("ACE RELL DE DATIL DULCE (FCO)");
list.add("ACE RELL HIGO (FCO)");
list.add("ACE RELL PASAS DULCE (FCO)");
list.add("ACE RELL PINA (FCO)");
list.add("AJO FINAS HIERBAS (FCO)");
ArrayAdapter<String> dataAdapter4 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter4.notifyDataSetChanged();
s2.setAdapter(dataAdapter4);
}
if (sp1.contentEquals("CITY FRESKO (SUMESA)")) {
List<String> list = new ArrayList<String>();
list.add("ACE RELL ALMNDRS (FCO)");
list.add("ACE RELL AJO (FCO)");
list.add("ACE RELL PIMIENTO ROJO (FCO)");
list.add("ACE RELL COCO DULCE (FCO)");
list.add("ACE RELL ARANDANOS (FCO)");
list.add("ACE RELL DATIL DULCE (FCO)");
list.add("ACE RELL. HIGO (FCO)");
list.add("ACE RELL PASAS DULCE (FCO)");
list.add("ACE RELL PINA (FCO)");
list.add("AJO FINAS HIERBAS (FCO)");
ArrayAdapter<String> dataAdapter5 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter5.notifyDataSetChanged();
s2.setAdapter(dataAdapter5);
}
if (sp1.contentEquals("CHEDRAUI")) {
List<String> list = new ArrayList<String>();
list.add("ACE RELL ALMENDRAS (FCO)");
list.add("ACE RELL COCO DULCE (FCO)");
list.add("ACE RELL HIGO (FCO)");
list.add("ACE RELL PASAS DULCE (FCO)");
list.add("AJO FINAS HIERBAS (FCO)");
ArrayAdapter<String> dataAdapter6 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter6.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter6.notifyDataSetChanged();
s2.setAdapter(dataAdapter6);
}
}
siguiente.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("LOG", "resultado sp1 " + sp1);
Log.d("LOG", "resultado sp2 " + sp2);
//leemos status de usuario y determinamos a donde ir.
SharedPreferences prefs = getSharedPreferences("Sea_reg_app", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("sp1", sp1.toString());
editor.putString("sp2", sp2.toString());
//editor.putString("sp3", sp3.toString());
//cbMarcame1_1 = prefs.getBoolean("Guardar_Vincular",false);
editor.commit();
//startActivity(new Intent(MenuTiendas.this, formulario.class));
}
});
}
在您选择时不会更新的原因。以下代码将解决问题。
s2.setOnItemSelectedListener(this);
但是,如果我删除csv.writer
,代码也可以正常运行。
答案 1 :(得分:0)
你必须检查id,它在onItemSelected侦听器内触发才能使它工作。
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
switch (arg0.getId()){
case R.id.spinner1:
sp1= String.valueOf(s1.getSelectedItem());
if(sp1.contentEquals("SUPERAMA")) {
List<String> list = new ArrayList<String>();
list.add("ACE RELL DE ALMNDRS (FCO)");
list.add("ACE RELL DE AJO (FCO)");
list.add("ACE RELL ARANDANOS (FCO)");
list.add("ACE RELL HIGO (FCO)");
list.add("AJO FINAS HIERBAS (FCO)");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
s2.setAdapter(dataAdapter);
}
if(sp1.contentEquals("CITY FRESKO (COMER)")) {
List<String> list = new ArrayList<String>();
list.add("ACE RELL ALMNDRS (FCO)");
list.add("ACE RELL AJO (FCO)");
list.add("ACE RELL PIMIENTO ROJO (FCO)");
list.add("ACE RELL COCO DULCE (FCO)");
list.add("ACE RELL ARANDANOS (FCO)");
list.add("ACE RELL DATIL DULCE (FCO)");
list.add("ACE RELL HIGO (FCO)");
list.add("ACE RELL DE PASAS DULCE (FCO)");
list.add("ACE RELL DE PINA (FCO)");
list.add("AJO FINAS HIERBAS (FCO)");
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
s2.setAdapter(dataAdapter2);
}
if(sp1.contentEquals("CITY FRESKO (FRESKO)")) {
List<String> list = new ArrayList<String>();
list.add("ACE RELL ALMNDRS (FCO)");
list.add("ACE RELL AJO (FCO)");
list.add("ACE RELL PIMIENTO ROJO (FCO)");
list.add("ACE RELL COCO DULCE (FCO)");
list.add("ACE RELL ARANDANOS (FCO)");
list.add("ACE RELL DATIL DULCE (FCO)");
list.add("ACE RELL HIGO (FCO)");
list.add("ACE RELL PASAS DULCE (FCO)");
list.add("ACE RELL PINA (FCO)");
list.add("AJO FINAS HIERBAS (FCO)");
ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter3.notifyDataSetChanged();
s2.setAdapter(dataAdapter3);
}
if(sp1.contentEquals("CITY FRESKO (C.MARKET)")) {
List<String> list = new ArrayList<String>();
list.add("ACE RELL ALMNDRS (FCO)");
list.add("ACE RELL AJO (FCO)");
list.add("ACE RELL PIMIENTO ROJO (FCO)");
list.add("ACE RELL COCO DULCE (FCO)");
list.add("ACE RELL ARANDANOS (FCO)");
list.add("ACE RELL DE DATIL DULCE (FCO)");
list.add("ACE RELL HIGO (FCO)");
list.add("ACE RELL PASAS DULCE (FCO)");
list.add("ACE RELL PINA (FCO)");
list.add("AJO FINAS HIERBAS (FCO)");
ArrayAdapter<String> dataAdapter4 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter4.notifyDataSetChanged();
s2.setAdapter(dataAdapter4);
}
if(sp1.contentEquals("CITY FRESKO (SUMESA)")) {
List<String> list = new ArrayList<String>();
list.add("ACE RELL ALMNDRS (FCO)");
list.add("ACE RELL AJO (FCO)");
list.add("ACE RELL PIMIENTO ROJO (FCO)");
list.add("ACE RELL COCO DULCE (FCO)");
list.add("ACE RELL ARANDANOS (FCO)");
list.add("ACE RELL DATIL DULCE (FCO)");
list.add("ACE RELL. HIGO (FCO)");
list.add("ACE RELL PASAS DULCE (FCO)");
list.add("ACE RELL PINA (FCO)");
list.add("AJO FINAS HIERBAS (FCO)");
ArrayAdapter<String> dataAdapter5 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter5.notifyDataSetChanged();
s2.setAdapter(dataAdapter5);
}
if(sp1.contentEquals("CHEDRAUI")) {
List<String> list = new ArrayList<String>();
list.add("ACE RELL ALMENDRAS (FCO)");
list.add("ACE RELL COCO DULCE (FCO)");
list.add("ACE RELL HIGO (FCO)");
list.add("ACE RELL PASAS DULCE (FCO)");
list.add("AJO FINAS HIERBAS (FCO)");
ArrayAdapter<String> dataAdapter6 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter6.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter6.notifyDataSetChanged();
s2.setAdapter(dataAdapter6);
}
break;
case R.id.spinner2:
sp2= String.valueOf(s2.getSelectedItem());
break;
}
Button siguiente = (Button) this.findViewById(R.id.button7);
siguiente.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("LOG", "resultado sp1 " + sp1);
Log.d("LOG", "resultado sp2 " + sp2);
//leemos status de usuario y determinamos a donde ir.
SharedPreferences prefs = getSharedPreferences("Sea_reg_app", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("sp1", sp1.toString());
editor.putString("sp2", sp2.toString());
//editor.putString("sp3", sp3.toString());
//cbMarcame1_1 = prefs.getBoolean("Guardar_Vincular",false);
editor.commit();
// startActivity(new Intent(this, formulario.class));
}
});
}
答案 2 :(得分:0)
将您的Spinner
onItemSelected
方法分隔为:
public void onItemSelected(AdapterView<?> adapterView, View view, int pos, long id) {
if(adapterView.getId() == R.id.spinnerOneId){
// your action
sp1= String.valueOf(s1.getSelectedItem());
}
else if(adapterView.getId() == R.id.spinnerTwoId){
//your action
sp2= String.valueOf(s2.getSelectedItem());
}
}