我正在制作一个警告对话框以填写收据项目。警报对话框中有一个按钮,用于移至新活动以扫描QR。问题是在扫描QR码并意图返回到“收货项目活动”后,alertdialog不再显示,因此我需要再次按一下alertdialog按钮。从QR扫描仪返回创建收据后如何使警报对话框仍然显示
我已经尝试添加onShowListener,但alertdialog仍然不显示。
final AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button btnScan = v.findViewById(R.id.alertdialog_receipt_scanqr);
btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(CreateReceiptActivity.this, QRScannerActivity.class);
startActivityForResult(i, QR_REQUEST_CODE);
alertDialog.dismiss();
finish();
}
});
}
});
alertDialog.show();
这是ReceiptActivity的完整代码:
public class CreateReceiptActivity extends AppCompatActivity {
@BindView(R.id.receipt_date)
TextView date;
@BindView(R.id.receipt_invoice)
TextView invoiceNumber;
@BindView(R.id.btn_receipt_add_item)
ImageButton addItem;
@BindView(R.id.btn_receipt_print)
ImageButton printItem;
@BindView(R.id.receipt_view_recycler)
RecyclerView recyclerView;
@BindView(R.id.create_receipt_pb_loading)
ProgressBar pbloading;
private static final int QR_REQUEST_CODE = 1;
List<ListAutoComplete> autoCompleteList;
ListAutoComplete listAutoComplete;
List<ListReceiptItem> receiptItemList;
ListReceiptItem listReceiptItem;
ReceiptItemAdapter adapter;
public String itemType, itemQty, itemPrice, itemDate, itemInvoice, lastInvoice, qrResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_receipt);
ButterKnife.bind(this);
receiptItemList = new ArrayList<>();
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new ReceiptItemAdapter(this, receiptItemList);
recyclerView.setAdapter(adapter);
itemInvoice = invoiceNumber.getText().toString();
itemDate = setDate(date);
date.setText(this.getString(R.string.date, setDate(date)));
printItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
pbloading.setVisibility(View.VISIBLE);
cutStock();
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(CreateReceiptActivity.this);
builder.setMessage("Print Transaksi ?").setPositiveButton("Ya", dialogClickListener)
.setNegativeButton("Tidak", dialogClickListener).show();
}
});
addItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater li = CreateReceiptActivity.this.getLayoutInflater();
final View v = li.inflate(R.layout.alertdialog_create_receipt, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(CreateReceiptActivity.this);
builder.setView(v);
final EditText addItemType = v.findViewById(R.id.alertdialog_receipt_type);
final EditText addItemQty = v.findViewById(R.id.alertdialog_receipt_qty);
final EditText addItemPrice = v.findViewById(R.id.alertdialog_receipt_price);
Button btnSubmit = v.findViewById(R.id.alertdialog_receipt_submit);
addItemType.setText(qrResult);
final AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button btnScan = v.findViewById(R.id.alertdialog_receipt_scanqr);
btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(CreateReceiptActivity.this, QRScannerActivity.class);
startActivityForResult(i, QR_REQUEST_CODE);
alertDialog.dismiss();
finish();
}
});
}
});
alertDialog.show();
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
itemType = addItemType.getText().toString().trim();
itemQty = addItemQty.getText().toString().trim();
itemPrice = addItemPrice.getText().toString().trim();
listReceiptItem = new ListReceiptItem(itemType, itemQty, itemPrice, "0");
receiptItemList.add(listReceiptItem);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
alertDialog.dismiss();
Toast.makeText(CreateReceiptActivity.this, "barang tertambah", Toast.LENGTH_SHORT).show();
}
});
}
});
Bundle extras = getIntent().getExtras();
if(extras != null && extras .containsKey("QRItemtype")){
qrResult = extras.getString("QRItemtype");
if (qrResult == null) {
Toast.makeText(this, "Scan gagal", Toast.LENGTH_SHORT).show();
} else if (!(qrResult == null)) {
Toast.makeText(this, qrResult, Toast.LENGTH_SHORT).show();
}
}
}
private void cutStock() {
final FirebaseFirestore db = FirebaseFirestore.getInstance();
for (ListReceiptItem listreceiptItem : receiptItemList) {
final String soldItemDate = date.getText().toString().trim();
final String soldItemInvoice = invoiceNumber.getText().toString().trim();
final String soldItemtype = listreceiptItem.getType();
final String soldItemQty = listreceiptItem.getQty();
final String soldItemPrice = listreceiptItem.getPrice();
db.collection("watchlist").whereEqualTo("type", soldItemtype)
.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(Tag.ITEM, document.getId() + "=>" + document.getData());
String id = document.getString("id");
String oldqty = document.getString("qty");
Integer i = Integer.parseInt(oldqty) - Integer.parseInt(soldItemQty);
String newQty = String.valueOf(i);
Map<Object, String> map = new HashMap<>();
map.put("qty", newQty);
db.collection("watchlist").document(document.getId()).set(map, SetOptions.merge());
ArrayList<Map<String, Object>> list = new ArrayList<>();
Map<String, Object> receiptItem = new HashMap<>();
receiptItem.put("invoice", soldItemInvoice);
list.add(receiptItem);
receiptItem.put("date", soldItemDate);
list.add(receiptItem);
receiptItem.put("type", soldItemtype);
list.add(receiptItem);
receiptItem.put("qty", soldItemQty);
list.add(receiptItem);
receiptItem.put("price", soldItemPrice);
list.add(receiptItem);
final FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
firebaseFirestore.collection("sales").add(receiptItem).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(CreateReceiptActivity.this, "Berhasil mencetak transaksi", Toast.LENGTH_SHORT).show();
Integer i = Integer.parseInt(soldItemInvoice) + 1;
String newInvoice = String.valueOf(i);
invoiceNumber.setText(newInvoice);
pbloading.setVisibility(View.GONE);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(CreateReceiptActivity.this, "Gagal mencetak", Toast.LENGTH_SHORT).show();
pbloading.setVisibility(View.GONE);
}
});
}
} else {
Toast.makeText(CreateReceiptActivity.this, "Barang tidak terdaftar", Toast.LENGTH_SHORT).show();
Log.w(Tag.ITEM, "error getting documents", task.getException());
pbloading.setVisibility(View.GONE);
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(CreateReceiptActivity.this, "Barang tidak terdaftar", Toast.LENGTH_SHORT).show();
pbloading.setVisibility(View.GONE);
}
});
}
}
public void getTypeList() {
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference documentReference = db.collection("watchlist");
documentReference.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(Tag.ITEM, document.getId() + "=>" + document.getData());
String type = document.getString("type");
listAutoComplete = new ListAutoComplete(type);
autoCompleteList.add(listAutoComplete);
}
} else {
Log.w(Tag.ITEM, "error getting documents", task.getException());
}
}
});
}
public String setDate(TextView view) {
java.util.Date today = Calendar.getInstance().getTime();//getting date
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");//formating according to my need
String date = formatter.format(today);
view.setText(date);
return date;
}
}
答案 0 :(得分:1)
单击QR按钮后,您将调用alertDialog.dismiss()和finish()。 自然,这将关闭警报对话框。
要么删除这两个调用,要么在OnResume方法中再次打开对话框。
@Override
protected void onResume() {
super.onResume();
openDialog();
}
private void openDialog(){
final AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button btnScan = v.findViewById(R.id.alertdialog_receipt_scanqr);
btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(CreateReceiptActivity.this, QRScannerActivity.class);
startActivityForResult(i, QR_REQUEST_CODE);
}
});
}
});
alertDialog.show();
}
如果您不希望在最小化并返回时弹出警报,则可以
if(getIntent() != null){
isReturnedFromActivity = getIntent.getBooleanExtra(IS_RETURNED_FROM_ACTIVITY ,false);
if(isReturnedFromActivity){
openDialog();
}
}
}
当然,在调用此活动时,请将IS_RETURNED_FROM_ACTIVITY键设置为True。
答案 1 :(得分:0)
您正在btnScan.setOnClickListener中关闭警报对话框。所以请删除 “ alertDialog.dismiss();”从那里并删除finish()方法也如下:
final AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button btnScan = v.findViewById(R.id.alertdialog_receipt_scanqr);
btnScan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(CreateReceiptActivity.this, QRScannerActivity.class);
startActivityForResult(i, QR_REQUEST_CODE);
}
});
}
});
alertDialog.show();
答案 2 :(得分:0)
我看到您的addItem
单击监听器是调用该对话框显示的内容,所以为什么不单击该按钮以显示该对话框:
//in your QR activity intent :
Intent intent = new Intent(CONTEXT, ACTIVITY.class);
intent.putExtra("FLAG", "showDialog");
startActivity(intent);'
//when you return to your activity check your flag and
// if it is good you will have to show your dialog again
String flag = getIntent().getStringExtra("FLAG");
if(flag == "showDialog"){
addItem.performClick();
}
答案 3 :(得分:0)
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to clean?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.dismiss();
((ActivityName) appContext).onResume();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.dismiss();``
}
});
builder.create().show();
}