Android开发设置Spinner默认选中项

释放双眼,带上耳机,听听看~!

ps:因公司业务逻辑需要,需要将首页展示页面的产品型号在修改页面进行修改操作,所以就涉及到在修改页面中默认选中当前首页选中的spinner子项数值

因个人spinner数据的不同,还有懒,我就直接将主要的设置选中的代码沾上去

   private void initSpiner() {
jingli = new ArrayList<>();
xinghao = new ArrayList<>();
productBeans = new ArrayList<>();
humanBeans = new ArrayList<>();
model.ledgJiekouEntityMutableLiveData().observe(this, new Observer<LedgJiekouEntity>() {//封装的网络请求
@Override
public void onChanged(@Nullable LedgJiekouEntity ledgJiekouEntity) {
for(int i=0;i<ledgJiekouEntity.getProduct().size();i++){//将所有产品数据设置到spinner中
xinghao.add(ledgJiekouEntity.getProduct().get(i).getCinvstd()+ledgJiekouEntity.getProduct().get(i).getCinvname());
productBeans.add(new LedgJiekouEntity.ProductBean(ledgJiekouEntity.getProduct().get(i).getI_id(),ledgJiekouEntity.getProduct().get(i).getCinvname()));
}
adapter = new ArrayAdapter<String>(RepaireLedgerEdit.this,R.layout.spinneritem,xinghao);
binding.ledgerEditProduct.setAdapter(adapter);//型号的设置完成
for(int j=0;j<ledgJiekouEntity.getHuman().size();j++){//设置当前默认选中  -- 重点
jingli.add(ledgJiekouEntity.getHuman().get(j).getCpersonname());
humanBeans.add(new LedgJiekouEntity.HumanBean(ledgJiekouEntity.getHuman().get(j).getCpersoncode(),ledgJiekouEntity.getHuman().get(j).getCpersonname()));
}
jingAdapter = new ArrayAdapter<String>(RepaireLedgerEdit.this,R.layout.spinneritem,jingli);
binding.ledgerEditJingl.setAdapter(jingAdapter);//经理设置完成
for(int z=0;z<ledgJiekouEntity.getHuman().size();z++){//经理的默认选中状态
if(ledgJiekouEntity.getHuman().get(z).getCpersonname().equals(databeanlist.getPerson())){
binding.ledgerEditJingl.setSelection(z,true);
currenthuman = ledgJiekouEntity.getHuman().get(z).getCpersoncode();
}
}
for(int a=0;a<ledgJiekouEntity.getProduct().size();a++){
if(databeanlist.getProduct().equals(ledgJiekouEntity.getProduct().get(a).getCinvstd())){
binding.ledgerEditProduct.setSelection(a,true);
if(currentproduct.isEmpty()){
currentproduct="";
}else {
currentproduct = ledgJiekouEntity.getProduct().get(a).getI_id();
}
}
}
binding.ledgerEditProduct.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {//型号的点击事件
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
productBeanentity = productBeans.get(position);
currentproduct = productBeanentity.getI_id();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
binding.ledgerEditJingl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
humanBeanentity = humanBeans.get(position);
currentproduct = humanBeanentity.getCpersoncode();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
});
}

我遇到的难题:
我在进行产品数据默认选中的时候,for循环里面的数据就只打印一个,直接导致无法进行默认设置,然后我将 if条件判断的顺序改变了一下 就好了 哎花了我一上午时间没想到会败到这里 ,希望看到这篇文章的你能够有用

人已赞赏
Android文章

Android开发中SQLite实时刷新(数据源观察者模式)

2020-3-11 12:50:58

Android文章

Android开发react navigation 页面切换方法总结

2020-3-11 13:23:38

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索