无法在android中选择微调器项目
Posted
技术标签:
【中文标题】无法在android中选择微调器项目【英文标题】:Unable to select spinner item in android 【发布时间】:2020-10-15 20:08:32 【问题描述】:我添加了一个微调器并将 a 列表加载到其中。使用app
我想从列表中进行选择。但是我做不到。
subDivisionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
if (isUserAction)
if (position != selectedSubDivisionPosition)
resetForm(false, false);
selectedSubDivision = subDivisionList.get(position);
selectedSubDivisionPosition = position;
isUserAction = true;
@Override
public void onNothingSelected(AdapterView<?> parent)
);
当我点击微调器时,列表会显示,但当我点击一个项目时,它会恢复到原始状态。即选定的项目在点击后不显示。见下图
更新 1
下面是我的resetForm
方法
private void resetForm(boolean all, boolean signal)
this.refNofield1.setText("");
this.consumerNameEditText.setText("");
this.consumerAddressEditText.setText("");
this.longitudeEditText.setText("");
this.latitudeEditText.setText("");
this.placeEditText.setText("");
this.tarifEditText.setText("");
this.sLoadEditText.setText("");
this.cableLengthEditText.setText("");
this.runningLoadEntryA.setText("");
this.runningLoadEntryB.setText("");
this.runningLoadEntryC.setText("");
this.ctRatio.setText("");
this.transformerRating.setText("");
this.newSurveyImagesBitmap = new ArrayList<Bitmap>();
this.newSurveyImageSliderAdapter.setSliderImages(this.newSurveyImagesBitmap);
this.imagesNames = new ArrayList<String>();
latestSelectedImagePath = "";
refNo = "";
customerId = "";
imageCount = 0;
meterLocationRadioGroup.clearCheck();
meterTypeSpinner.setSelection(0);
meterTypeDesiredSpinner.setSelection(0);
meterTypeFieldSpinner.setSelection(0);
installTypeSpinner.setSelection(0);
meterStatusSpinner.setSelection(0);
CtRatiospinner.setSelection(0);
TransfRateSpinner.setSelection(0);
transformerTypeSpinner.setSelection(0);
subDivisionSpinner.setSelection(0);
//meterTypeSpinner.setSelection(0);
selectedMeterType = "";
zongDataRateEdittext.setText("");
mobilinkDataRateEdittext.setText("");
telenorDataRateEdittext.setText("");
ufoneDataRateEdittext.setText("");
enableDisableDataRateEntry(false);
enableDisableOperators(false,false);
if (all)
setSubDivs();
isatbSealedChecked = false;
atbSealedCheckBox.setEnabled(false);
ctQuantitySpinner.setSelection(0);
if (signal)
zongRadioGroup.clearCheck();
waridRadioGroup.clearCheck();
mobilinkRadioGroup.clearCheck();
telenorRadioGroup.clearCheck();
if (isMobilinkChecked)
mobilinkCheckBox.setChecked(false);
if (isWaridChecked)
waridCheckBox.setChecked(false);
if (isTelenorChecked)
telenorCheckBox.setChecked(false);
if (isZongChecked)
zongCheckBox.setChecked(false);
zongType = "";
mobilinkType = "";
telenorType = "";
waridType = "";
zongDataRate = 0;
waridDataRate = 0;
telenorDataRate = 0;
mobilinkDataRate = 0;
【问题讨论】:
告诉我们resetForm()是做什么的,如果你也给我们那个函数代码会更好 似乎问题出在resetForm(false, false)
方法中,该方法正在重置值,因此请添加完整代码。
resetForm()
是我重置表单字段的方法。
@AgentP 代码已添加,您可以查看
@Pavneet_Singh 示例代码已添加,请检查
【参考方案1】:
作为mentioned above,resetForm
方法是使用第一项重置微调器的值
subDivisionSpinner.setSelection(0);
所以评论这个,微调器应该按预期工作
private void resetForm(boolean all, boolean signal)
...
//subDivisionSpinner.setSelection(0); comment this
//meterTypeSpinner.setSelection(0);
【讨论】:
resetForm()
在进程完成后也会被调用,所以在这种情况下我的subDivisionSpinner
不会设置为0
@Moeez 我认为问题出在 resetForm 本身...你检查过评论了吗?
我已经添加了答案【参考方案2】:
关注@Pavneet_Sing Solution。我已经完成了以下操作,现在可以使用了
private void resetForm(boolean all, boolean signal)
if(all || signal)// if both true then reset form otherwise not
this.refNofield1.setText("");
this.consumerNameEditText.setText("");
this.consumerAddressEditText.setText("");
this.longitudeEditText.setText("");
this.latitudeEditText.setText("");
this.placeEditText.setText("");
//......
【讨论】:
很高兴看到您已修复流程。不过,您可以使用 else case 和单个布尔标志if(all) // reset all else// reset single
进一步优化它。提取所有和单个方法的通用设置,并从 if-else 等中调用该方法。快乐编码!【参考方案3】:
试试这个,在你的 if 语句之前添加isUserAction = true;
subDivisionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
isUserAction = true;
if (isUserAction)
if (position != selectedSubDivisionPosition)
resetForm(false, false);
selectedSubDivision = subDivisionList.get(position);
selectedSubDivisionPosition = position;
@Override
public void onNothingSelected(AdapterView<?> parent)
);
【讨论】:
private Boolean isUserAction = true;
已经设置为 true【参考方案4】:
将您的代码更改为此 通过移动
selectedSubDivision = subDivisionList.get(position); selectedSubDivisionPosition = 位置; isUserAction = true;
在 else 语句中。
subDivisionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
if (isUserAction)
if (position != selectedSubDivisionPosition)
resetForm(false, false);
//MOVE YOUR CODE INSIDE THE ELSE STATEMENT
else
selectedSubDivision = subDivisionList.get(position);
selectedSubDivisionPosition = position;
isUserAction = true;
// selectedSubDivision = subDivisionList.get(position);
// selectedSubDivisionPosition = position;
// isUserAction = true;
@Override
public void onNothingSelected(AdapterView<?> parent)
);
【讨论】:
以上是关于无法在android中选择微调器项目的主要内容,如果未能解决你的问题,请参考以下文章