我有一个用vb.net编写的程序,现在用java工作室用java编写。试图模仿与原始程序相同的流程。
我在某个部分,在输入内容后,我进入数据库并检索记录。在我的例子中它有2条记录。我试图逐步浏览每个记录并询问用户输入。每条记录最多4个输入。
我一直在尝试在for循环中完成此操作。 (我更像是一个Windows程序员.net所以我不熟悉Java)在Windows中你只需弹出一个消息框(它们处于模态模式,所以程序停止并等待输入继续。)
这就是我。
对于每次迭代,我都需要提问。我尝试过对话框和dialogFragment。
if (addNew.equals("Y")) {
final Dialog dialog = new Dialog(ReturnAreaActivity.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.dialog_location);
dialog.setTitle("New Location Setup");
TextView message = (TextView) dialog.findViewById(R.id.theText);
message.setText("Is this location a Pick Bin");
Button buttonYes = (Button) dialog.findViewById(R.id.buttonYes);
Button buttonNo = (Button) dialog.findViewById(R.id.buttonNo);
dialog.show();
dialog.setCancelable(false);
buttonYes.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PickBinFlag="Y" ;
myField = "QTY";
dialog.dismiss();
location_Type = "P";
mILM=ItemLevelsModel.getInstance();
mylevels = DatabaseMgr.GetItemLevels(IsItem);
for (int i = 0; i < mylevels.size(); i++) {
String lLvl = mylevels.get(i).getitemLevel().toString();
String lLvlD = mylevels.get(i).getItemLvlDesc().toString();
CheckPicking(IsItem, lLvl, lLvlD);
if (PickBinFlag.equals("Y")){
final Dialog dialog1 = new Dialog(ReturnAreaActivity.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
dialog1.setContentView(R.layout.dialog_minmax);
TextView message = (TextView) dialog1.findViewById(R.id.minMax);
TextView theText = (TextView) dialog1.findViewById(R.id.theText);
if (whichQTY.equals("MAX")){
dialog1.setTitle("Enter Max QTY");
message.setText("MAX");
theText.setText("Enter Max QTY for this location");
}else{
dialog1.setTitle("Enter Min QTY");
message.setText("MAX");
theText.setText("Enter Min QTY for this location");
}
final TextView maxQTY = (TextView) dialog1.findViewById(R.id.editAmt);
Button button1 = (Button) dialog1.findViewById(R.id.btn1);
Button button2 = (Button) dialog1.findViewById(R.id.btn2);
Button button3 = (Button) dialog1.findViewById(R.id.btn3);
Button button4 = (Button) dialog1.findViewById(R.id.btn4);
Button button5 = (Button) dialog1.findViewById(R.id.btn5);
Button button6 = (Button) dialog1.findViewById(R.id.btn6);
Button button7 = (Button) dialog1.findViewById(R.id.btn7);
Button button8 = (Button) dialog1.findViewById(R.id.btn8);
Button button9 = (Button) dialog1.findViewById(R.id.btn9);
Button button0 = (Button) dialog1.findViewById(R.id.btn0);
Button buttonEnter = (Button) dialog1.findViewById(R.id.btnEnter);
Button buttonClear = (Button) dialog1.findViewById(R.id.btnClear);
dialog1.show();
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "1";
maxQTY.setText(mPin);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "2";
maxQTY.setText(mPin);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "3";
maxQTY.setText(mPin);
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "4";
maxQTY.setText(mPin);
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "5";
maxQTY.setText(mPin);
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "6";
maxQTY.setText(mPin);
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "7";
maxQTY.setText(mPin);
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "8";
maxQTY.setText(mPin);
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "9";
maxQTY.setText(mPin);
}
});
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "0";
maxQTY.setText(mPin);
}
});
buttonEnter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMax = Integer.parseInt(mPin);
if (whichQTY.equals("Max")) {
mMax = Integer.parseInt(mPin);
whichQTY = "Min";
} else {
mMin = Integer.parseInt(mPin);
whichQTY = "Max";
}
mPin = "";
dialog1.dismiss();
return;
}
});
buttonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = "";
maxQTY.setText("");
}
});
}
}
}
});
buttonNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PickBinFlag="N";
dialog.dismiss();
myField = "QTY";
return;
}
});
/* dialog.setCancelable(false);
dialog.show();*/
}else{
return;
}
} else {
}
}
}else{
// we are in QTY field.
mQTY.setText(scanner.getScanResult());
}
}
};
}
public void CheckPicking(String myItem,String myLevel,String myLvlDesc){
Timber.d(myItem);
if (PickBinFlag.equals("Y")) {
location_Type = "P";
mILM=ItemLevelsModel.getInstance();
mylevels = DatabaseMgr.GetItemLevels(myItem);
whichQTY="Max";
Integer cnt = mylevels.size();
for (int i = 0; i < mylevels.size(); i++) {
String lLvl = mylevels.get(i).getitemLevel().toString();
String lLvlD = mylevels.get(i).getItemLvlDesc().toString();
final Dialog dialog = new Dialog(ReturnAreaActivity.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.dialog_location);
dialog.setTitle("Pick Levels");
dialog.setCancelable(false);
TextView message = (TextView) dialog.findViewById(R.id.theText);
message.setText("Will " + lLvl + " " + lLvlD + " \nbe Picked from this location?");
Button buttonYes = (Button) dialog.findViewById(R.id.buttonYes);
Button buttonNo = (Button) dialog.findViewById(R.id.buttonNo);
buttonYes.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PickBinFlag = "Y";
myField = "QTY";
dialog.dismiss();
final Dialog dialog1 = new Dialog(ReturnAreaActivity.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
dialog1.setContentView(R.layout.dialog_minmax);
TextView message = (TextView) dialog1.findViewById(R.id.minMax);
TextView theText = (TextView) dialog1.findViewById(R.id.theText);
if (whichQTY.equals("Max")) {
dialog1.setTitle("Enter Max QTY");
message.setText("MAX");
theText.setText("Enter Max QTY for this location");
} else {
dialog1.setTitle("Enter Min QTY");
message.setText("MAX");
theText.setText("Enter Min QTY for this location");
}
final TextView maxQTY = (TextView) dialog1.findViewById(R.id.editAmt);
Button button1 = (Button) dialog1.findViewById(R.id.btn1);
Button button2 = (Button) dialog1.findViewById(R.id.btn2);
Button button3 = (Button) dialog1.findViewById(R.id.btn3);
Button button4 = (Button) dialog1.findViewById(R.id.btn4);
Button button5 = (Button) dialog1.findViewById(R.id.btn5);
Button button6 = (Button) dialog1.findViewById(R.id.btn6);
Button button7 = (Button) dialog1.findViewById(R.id.btn7);
Button button8 = (Button) dialog1.findViewById(R.id.btn8);
Button button9 = (Button) dialog1.findViewById(R.id.btn9);
Button button0 = (Button) dialog1.findViewById(R.id.btn0);
Button buttonEnter = (Button) dialog1.findViewById(R.id.btnEnter);
Button buttonClear = (Button) dialog1.findViewById(R.id.btnClear);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "1";
maxQTY.setText(mPin);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "2";
maxQTY.setText(mPin);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "3";
maxQTY.setText(mPin);
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "4";
maxQTY.setText(mPin);
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "5";
maxQTY.setText(mPin);
}
});
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "6";
maxQTY.setText(mPin);
}
});
button7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "7";
maxQTY.setText(mPin);
}
});
button8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "8";
maxQTY.setText(mPin);
}
});
button9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "9";
maxQTY.setText(mPin);
}
});
button0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = mPin + "0";
maxQTY.setText(mPin);
}
});
buttonEnter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mMax = Integer.parseInt(mPin);
if (whichQTY.equals("Max")) {
mMax = Integer.parseInt(mPin);
whichQTY = "Min";
} else {
mMin = Integer.parseInt(mPin);
whichQTY = "Max";
}
mPin = "";
dialog1.dismiss();
return;
}
});
buttonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPin = "";
maxQTY.setText("");
}
});
dialog1.setCancelable(false);
dialog1.show();
}
});
buttonNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PickBinFlag = "N";
dialog.dismiss();
myField = "QTY";
}
});
dialog.setCancelable(false);
dialog.show();
}
}
我想我的问题是如何最好地解决这个问题。
答案 0 :(得分:0)
首先,您添加了太多不必要且不可读的代码,因此很难为您提供准确的解决方案。
但是,在Android编程中,您通常使用回调来捕获Dialog
的结果。如果你只使用Dialog
(我担心这不是推荐的解决方案,应该使用DialogFragment
)你应该在按钮点击监听器中调用你的方法。但是如果你使用DialogFragment
,你应该创建自己的监听器。以下是Android guide中的示例:
public class NoticeDialogFragment extends DialogFragment {
public interface NoticeDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
NoticeDialogListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}
...
}
然后你必须在你的活动中实现监听器回调:
public class MainActivity extends FragmentActivity
implements NoticeDialogFragment.NoticeDialogListener{
...
public void showNoticeDialog() {
DialogFragment dialog = new NoticeDialogFragment();
dialog.show(getSupportFragmentManager(), "NoticeDialogFragment");
}
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
// User touched the dialog's positive button
...
}
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
// User touched the dialog's negative button
...
}
}
现在在循环中显示对话框是一个可怕的想法,因为你必须等待结果。正如您现在所做的那样,循环可以由recursion替换。因此,解决方案是创建一个显示对话框的方法。然后在您的活动中实现的回调方法中,您可以使用该方法在旧的关闭后显示新对话框。