android中spinner选中值的获取
//为门店绑定数据
ArrayAdapter shop_adapter = new ArrayAdapter(FirstLoginActivity.this,android.R.layout.simple_spinner_item,shopList);
shop_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 样式
spinnershop.setAdapter(shop_adapter);
spinnershop.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//Spinner spinner = (Spinner) arg0;
//shopname = (String) spinner.getItemAtPosition(arg2);
shopname=arg0.getItemAtPosition(arg2).toString();//传递门店
((Declare)getApplication()).setShopname(shopname);
//arg0.setVisibility(View.VISIBLE);
System.out.println("门店:"+ shopname);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
//为库区绑定数据
final ArrayAdapter<CharSequence> positon_adapter = ArrayAdapter.createFromResource(this, R.array.position,android.R.layout.simple_spinner_item);
positon_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerposition.setAdapter(positon_adapter);
spinnerposition.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//获取所选择的地址
position=spinnerposition.getSelectedItem().toString();//传递库区
((Declare)getApplication()).setPosition(position);
arg0.setVisibility(View.VISIBLE);
System.out.println("库区:"+ spinnerposition.getSelectedItem().toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
selectpositon.setText("您还没有选择库区");
}
});
//为选择堆头还是选择货架绑定数据
final ArrayAdapter<CharSequence> shelfheap_adapter = ArrayAdapter.createFromResource(this, R.array.shelfheap,android.R.layout.simple_spinner_item);
spinnershelfheap.setAdapter(shelfheap_adapter);
spinnershelfheap.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
shelfheap = FirstLoginActivity.this.getResources().getStringArray(R.array.shelfheap)[arg2].toString();
((Declare)getApplication()).setShelfheap(shelfheap);//选择盘点对象是堆头还是货架
arg0.setVisibility(View.VISIBLE);
System.out.println("堆头还是货架:"+ spinnershelfheap.getSelectedItem().toString());
if(shelfheap.equals("货架"))
{
spinnershop.setOnItemSelectedListener(itemSelectedListener);
}
if(shelfheap.equals("堆头"))
{
selectHeapno();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
selectshelf.setText("您还没有选择是要对堆头还是货架进行盘点");
}
});
//设置取消按钮监听器
cancelbutton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
//结束当前activity
finish();
}
});
//设置登录按钮监听器
loginbutton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
String username=useredittext.getText().toString();//工号
String pwdpwd=pwdedittext.getText().toString();
database1 = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/" + DBManager.DB_NAME, null);
//查询获得游标
//String sql=" select * from User where username='"+uname+"' and userno='"+password+"'";
Cursor c=database1.rawQuery("select * from t_user where username=? and userno=?",new String[]{username.toString(),pwdpwd.toString()});
if(c.moveToFirst())
{
//启动activity
//String shopname=((Declare)getApplicationContext()).getShopname();//门店传过来
// String position=((Declare)getApplicationContext()).getPosition();//库区传过来
// String shelfheap=((Declare)getApplicationContext()).getShelfno();//货架号或者堆头号传过来
if(username.equals("")||pwdpwd.equals("")||shopname.equals("")||position.equals("")||shelfheap.equals(""))
{
showDialog("请选择之后再开始盘点");
return;
}
Intent intent=new Intent(FirstLoginActivity.this,FirstInActivity.class);
startActivity(intent);
}
else
{
showDialog("输入用户工号不对,请重试!");
}
}
});
}
protected void selectHeapno() {
// TODO Auto-generated method stub
final ArrayAdapter<CharSequence> heapno_adapter = ArrayAdapter.createFromResource(this, R.array.heapno,android.R.layout.simple_spinner_item);
spinnershelf.setAdapter(heapno_adapter);
spinnershelf.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
heapno = FirstLoginActivity.this.getResources().getStringArray(R.array.heapno)[arg2].toString(); //若是盘点堆头,则传递堆头号
((Declare)getApplication()).setShelfno(heapno);//传递堆头号
arg0.setVisibility(View.VISIBLE);
System.out.println("堆头号货架号:"+ spinnershelf.getSelectedItem().toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
String selShop=null;
// 选择门店 联动 对应货架号
private OnItemSelectedListener itemSelectedListener = new OnItemSelectedListener() {
//从门店货架号表中查找对应门店下的货架号
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Spinner spShop = (Spinner) parent;
selShop = (String) spShop.getItemAtPosition(position);
Map<String,Integer> data = (Map)shopData.get(position);//根据所选择得到对应的id 然后得到货架
int pid = data.get(selShop);//得到门店的id
List shelfList = ShopDB.getCityByPid(pid);
ArrayAdapter shelf_adapter = new ArrayAdapter(FirstLoginActivity.this,android.R.layout.simple_spinner_item,shelfList);
shelf_adapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnershelf.setAdapter(shelf_adapter);
spinnershelf.setOnItemSelectedListener(citySelectedListener);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
String selectedShop = null;
private OnItemSelectedListener citySelectedListener = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Spinner spShelf = (Spinner) parent;
heapno=spinnershelf.getSelectedItem().toString();//传递门店
((Declare)getApplication()).setShelfno(heapno);
selectedShop = (String) spShelf.getItemAtPosition(position);
System.out.println("堆头号:"+ spinnershelf.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
在另外一个activity中,
String shopname=((Declare)getApplicationContext()).getShopname();//门店传过来
String position=((Declare)getApplicationContext()).getPosition();//库区传过来
String shelfheap=((Declare)getApplicationContext()).getShelfno();//货架号或者堆头号传过来
tvnotify.setText("门店为:"+shopname+"库区为:"+position+"货架号为:"+shelfheap);
结果出现shopname 不管我在spinner中选什么,总是默认为下拉列表中的第一项的值,各位高手看过来,实在是搞不明白spinner这个东西了,求指教。