gpt4 book ai didi

android - 使用微调器和编辑文本实现重置按钮时无法获得相同的值

转载 作者:行者123 更新时间:2023-11-29 21:52:23 25 4
gpt4 key购买 nike

我有 10 个微调器和 10 个编辑文本(用户可以让多个微调器和编辑文本留空),我还有 2 个按钮用于对所选微调器对象的值求和并编辑文本。成功求和该值,但是当我使用我的重置按钮回到应用程序首次启动时的初始状态时,我遇到了问题。结果是错误的,即使我已将其设置回默认值,我还是从第一次操作中获得了值。这是我的重置按钮代码

hapus.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
qty1.setText(null);
qty2.setText(null);
qty3.setText(null);
qty4.setText(null);
qty5.setText(null);
qty6.setText(null);
qty7.setText(null);
qty8.setText(null);
qty9.setText(null);
qty10.setText(null);
spin1.setSelection(0);
spin2.setSelection(0);
spin3.setSelection(0);
spin4.setSelection(0);
spin5.setSelection(0);
spin6.setSelection(0);
spin7.setSelection(0);
spin8.setSelection(0);
spin9.setSelection(0);
spin10.setSelection(0);
sum.setText(null);
hasil = 0;
}
});

这是我的微调器和求和操作代码的一部分

spin8.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View selectedItem, int pos, long id)
{
Object item = parent.getItemAtPosition(pos);
value8 = item.toString();
if (item.equals("null"))
{
hrg8 = 0;
}
else if (item.equals("Soto Istimewa"))
{
hrg8 = 24000;
}
else if (item.equals("Soto Biasa"))
{
hrg8 = 17000;
}
else if (item.equals("Nasi Putih"))
{
hrg8 = 4000;
}
else if (item.equals("Emping"))
{
hrg8 = 2000;
}
else if (item.equals("Kerupuk"))
{
hrg8 = 1000;
}
else if (item.equals("Es Teh"))
{
hrg8 = 2000;
}
else if (item.equals("Es Teh Manis"))
{
hrg8 = 3000;
}
else if (item.equals("Es Teh Botol"))
{
hrg8 = 5000;
}
else if (item.equals("Teh Botol Dingin"))
{
hrg8 = 3000;
}
else if (item.equals("Air Jeruk Panas"))
{
hrg8 = 7000;
}
else if (item.equals("Air Jeruk Dingin"))
{
hrg8 = 7000;
}
else if (item.equals("Aqua"))
{
hrg8 = 3000;
}
}

@Override
public void onNothingSelected(AdapterView<?> parent)
{

}
});

spin9.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View selectedItem, int pos, long id)
{
Object item = parent.getItemAtPosition(pos);
value9 = item.toString();
if (item.equals("null"))
{
hrg9 = 0;
}
else if (item.equals("Soto Istimewa"))
{
hrg9 = 24000;
}
else if (item.equals("Soto Biasa"))
{
hrg9 = 17000;
}
else if (item.equals("Nasi Putih"))
{
hrg9 = 4000;
}
else if (item.equals("Emping"))
{
hrg9 = 2000;
}
else if (item.equals("Kerupuk"))
{
hrg9 = 1000;
}
else if (item.equals("Es Teh"))
{
hrg9 = 2000;
}
else if (item.equals("Es Teh Manis"))
{
hrg9 = 3000;
}
else if (item.equals("Es Teh Botol"))
{
hrg9 = 5000;
}
else if (item.equals("Teh Botol Dingin"))
{
hrg9 = 3000;
}
else if (item.equals("Air Jeruk Panas"))
{
hrg9 = 7000;
}
else if (item.equals("Air Jeruk Dingin"))
{
hrg9 = 7000;
}
else if (item.equals("Aqua"))
{
hrg9 = 3000;
}
}

@Override
public void onNothingSelected(AdapterView<?> parent)
{

}
});

spin10.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View selectedItem, int pos, long id)
{
Object item = parent.getItemAtPosition(pos);
value10 = item.toString();
if (item.equals("null"))
{
hrg10 = 0;
}
else if (item.equals("Soto Istimewa"))
{
hrg10 = 24000;
}
else if (item.equals("Soto Biasa"))
{
hrg10 = 17000;
}
else if (item.equals("Nasi Putih"))
{
hrg10 = 4000;
}
else if (item.equals("Emping"))
{
hrg10 = 2000;
}
else if (item.equals("Kerupuk"))
{
hrg10 = 1000;
}
else if (item.equals("Es Teh"))
{
hrg10 = 2000;
}
else if (item.equals("Es Teh Manis"))
{
hrg10 = 3000;
}
else if (item.equals("Es Teh Botol"))
{
hrg10 = 5000;
}
else if (item.equals("Teh Botol Dingin"))
{
hrg10 = 3000;
}
else if (item.equals("Air Jeruk Panas"))
{
hrg10 = 7000;
}
else if (item.equals("Air Jeruk Dingin"))
{
hrg10 = 7000;
}
else if (item.equals("Aqua"))
{
hrg10 = 3000;
}
}

@Override
public void onNothingSelected(AdapterView<?> parent)
{

}
});

hitung.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
try
{
if (qty1.getText().toString() != null && !(qty1.getText().toString().equals("")))
{
jml1 = Integer.parseInt(qty1.getText().toString());
item1 = hrg1 * jml1;
}
else
{
jml1 = 0;
}
if (qty2.getText().toString() != null && !(qty2.getText().toString().equals("")))
{
jml2 = Integer.parseInt(qty2.getText().toString());
item2 = hrg2 * jml2;
}
else
{
jml2 = 0;
}
if (qty3.getText().toString() != null && !(qty3.getText().toString().equals("")))
{
jml3 = Integer.parseInt(qty3.getText().toString());
item3 = hrg3 * jml3;
}
else
{
jml3 = 0;
}
if (qty4.getText().toString() != null && !(qty4.getText().toString().equals("")))
{
jml4 = Integer.parseInt(qty4.getText().toString());
item4 = hrg4 * jml4;
}
else
{
jml4 = 0;
}
if (qty5.getText().toString() != null && !(qty5.getText().toString().equals("")))
{
jml5 = Integer.parseInt(qty5.getText().toString());
item5 = hrg5 * jml5;
}
else
{
jml5 = 0;
}
if (qty6.getText().toString() != null && !(qty6.getText().toString().equals("")))
{
jml6 = Integer.parseInt(qty6.getText().toString());
item6 = hrg6 * jml6;
}
else
{
jml6 = 0;
}
if (qty7.getText().toString() != null && !(qty7.getText().toString().equals("")))
{
jml7 = Integer.parseInt(qty7.getText().toString());
item7 = hrg7 * jml7;
}
else
{
jml7 = 0;
}
if (qty8.getText().toString() != null && !(qty8.getText().toString().equals("")))
{
jml8 = Integer.parseInt(qty8.getText().toString());
item8 = hrg8 * jml8;
}
else
{
jml8 = 0;
}
if (qty9.getText().toString() != null && !(qty9.getText().toString().equals("")))
{
jml9 = Integer.parseInt(qty9.getText().toString());
item9 = hrg9 * jml9;
}
else
{
jml9 = 0;
}
if (qty10.getText().toString() != null && !(qty10.getText().toString().equals("")))
{
jml10 = Integer.parseInt(qty10.getText().toString());
item10 = hrg10 * jml10;
}
else
{
jml10 = 0;
}
hasil = item1 + item2 + item3 + item4 + item5 + item6 + item7 + item8 + item9 + item10;
Toast.makeText(getApplicationContext(), "Rp. " + hasil, Toast.LENGTH_LONG).show();
sum.setText("Rp. " + Integer.toString(hasil));
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "Quantity can't be empty. Please type item quantity", Toast.LENGTH_LONG).show();
}
}
});

任何帮助将不胜感激,谢谢

最佳答案

您正在尝试将 null 用作字符串:

if (item.equals("null"))

应该是:

if (item == null)

此外,Spinner 或 EditText 的默认值是一个空字符串(即 ""),而不是 null。我相信您应该在重置值时使用 .setText("");,所以实际上第一个 if 语句应该是:

if (item.isEmpty()) // or item.equals("")

最后,您是否有 10 个包含 80-90% 相同代码的 OnItemSelectedListener?如果是这样,您应该编写一个通用监听器并使用 parent.getId() 来确定要设置的值。

关于android - 使用微调器和编辑文本实现重置按钮时无法获得相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14088261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com