无法 select android 中的旋转器项目
Unable to select spinner item in android
我添加了一个微调器并将列表加载到其中。使用 app
我想从列表中 select。但是我做不到。
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) {
}
});
当我单击微调器时,列表会显示,但当我单击某个项目时,它会返回到原始状态。即 selected 项目在点击后不显示。见下图
更新 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;
}
}
试试这个,在你的 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) {
}
});
与 一样,resetForm
方法使用
使用第一项重置微调器的值
subDivisionSpinner.setSelection(0);
所以对此进行评论,微调器应该会按预期工作
private void resetForm(boolean all, boolean signal) {
...
//subDivisionSpinner.setSelection(0); comment this
//meterTypeSpinner.setSelection(0);
关注 。我已完成以下操作,现在可以正常使用了
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("");
//......
}
}
将您的代码更改为此
通过移动
selectedSubDivision = subDivisionList.get(position);
selectedSubDivisionPosition = position;
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) {
}
});
我添加了一个微调器并将列表加载到其中。使用 app
我想从列表中 select。但是我做不到。
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) {
}
});
当我单击微调器时,列表会显示,但当我单击某个项目时,它会返回到原始状态。即 selected 项目在点击后不显示。见下图
更新 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;
}
}
试试这个,在你的 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) {
}
});
与 resetForm
方法使用
subDivisionSpinner.setSelection(0);
所以对此进行评论,微调器应该会按预期工作
private void resetForm(boolean all, boolean signal) {
...
//subDivisionSpinner.setSelection(0); comment this
//meterTypeSpinner.setSelection(0);
关注
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("");
//......
}
}
将您的代码更改为此 通过移动
selectedSubDivision = subDivisionList.get(position); selectedSubDivisionPosition = position; 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) {
}
});