我正在尝试在警告对话框中选择要在不同班级中显示的特定项目,并将相应的值添加为价格。我想让它显示所选内容。我知道for循环是错误的;应该去哪里,所以我将数组放在hashset的位置值。
final String[] OptionsStringARR = {"Babysit", "Snorkelling", "Test1", "Test2"};
final int[] OptionsPricingARR = {10, 35, 14, 9};
final ArrayList OptionssSelected = new ArrayList();
final HashSet<Integer> optset = new HashSet<>();
boolean[] checked= {false,false,false,false};
public class OptionClass implements View.OnClickListener {
@Override
public void onClick(View v) {
new AlertDialog.Builder(ibMap.this)
.setTitle(guidedetail.getText() + " available options.")
.setMultiChoiceItems(OptionsStringARR, checked,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int selectedItemId,
boolean isSelected) {
if (isSelected) {
checked[selectedItemId]=true;
optset.add(selectedItemId);
} else if (optset.contains(selectedItemId)) {
checked[selectedItemId]=false;
optset.remove(Integer.valueOf(selectedItemId));
}
}
})
//.setMessage("//I need to put shit here. Array lists maybe. and check boxes")
.setCancelable(true)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}
)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//optset.addAll(OptionssSelected);
dialog.dismiss();
}
}
).create().show();
}
}
我正在使用2个数组和一个哈希集来定义它们是什么,用于确定所选内容的哈希集,然后是包含该信息的数组。
public class ConfirmClass implements View.OnClickListener {
@Override
public void onClick(View v) {
int TotalOpt=0;
for (int j = 0; j < optset.size(); j++){
int[] y = new int[optset.size()];
int x = y[j];
TotalOpt=TotalOpt+ OptionsPricingARR[x];
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < optset.size(); i++) {
if (i > 0) {
sb.append(", ");
}
sb.append(OptionsStringARR[i]);
}
new AlertDialog.Builder(ibMap.this)
.setTitle(guidedetail.getText())
.setMessage("Options Selected: " + sb +
"\nTotal: "+ TotalOpt)
.setCancelable(true)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}
)
.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(ibMap.this, confirm.class));
}
}
).create().show();
}
}
谢谢!此外,如果您可以通过alertdialog帮助显示选项的价格,那就太棒了!
答案 0 :(得分:0)
为什么不为Option
创建单独的类,如下所示:
class Option {
private Integer itemId;
private String stringARR;
private Integer pricingARR;
public Option(Integer itemId, String stringARR, Integer pricingARR) {
// constructor code
}
// setter and getters
}
使用预先填充的Map<Integer, Option>
代替使用2个数组作为选项字符串和定价,例如:
final static Map<Integer, Option> optionMap = new HashMap<>();
final Set<Integer> optset = new HashSet<>();
// pre initialize options in map for later use
static{
optionMap.put(1, new Option(1, "Babysit", 10)); // assuming 1 is option id for "Babysit"
optionMap.put(2, new Option(2, "Snorkelling", 35)); // assuming 2 is option id for "Snorkelling"
// populate other options
}
然后在类onClick
的方法ConfirmClass
中,使用下面的代码进行定价和字符串连接:
@Override
public void onClick(View v) {
int totalOpt=0;
StringBuilder sb = new StringBuilder();
for (Iterator<Integer> iterator = optset.iterator(); iterator.hasNext();){
// add option price
totalOpt += optionMap.get(iterator.next()).getPricingARR();
// append option description
if (sb.length() > 0) {
sb.append(", ");
}
sb.append(optionMap.get(iterator.next()).getStringARR());
}
// rest of the code
}
注意:查看您的代码,我假设您的应用程序中的选项已修复,这就是我使用static
初始化程序块的原因..