AlertDialog无法通过其他方法访问对话框视图

时间:2019-04-02 08:30:16

标签: android

我仍在学习android并尝试进行包含alertdialog的活动,我的问题是如何在dialog方法之外访问alertdialog视图? (我想将我的进度条设置为在调用另一种方法时消失了)

这是代码,当我想将alertdialog的进度条可见性设置为在loadItem()中时,有2种方法:1. OpenDialog(alertdialog)和2.loadItem(用于从Firestore数据库获取值)。                             pbSearchItem.setVisibility(View.GONE); pbsearchditem变成红色/找不到视图:

public void openDialog(){
        LayoutInflater li = CreateReceiptActivity.this.getLayoutInflater();
        final View v = li.inflate(R.layout.alertdialog_create_receipt, null);
        final Builder builder = new Builder(CreateReceiptActivity.this);
        builder.setView(v);
        final CardView cvQtyPriceBox = v.findViewById(R.id.alertdialog_receipt_cv_qtyprice);
        final EditText addItemType = v.findViewById(R.id.alertdialog_receipt_type);
        addItemType.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
        final EditText addItemMerk = v.findViewById(R.id.alertdialog_receipt_merk);
        final ProgressBar pbSearchItem = v.findViewById(R.id.alertdialog_receipt_pb);
        final Button searchitemBtn = v.findViewById(R.id.alertdialog_receipt_btn_search_typemerk);
        addItemMerk.setFilters(new InputFilter[]{new InputFilter.AllCaps()});
        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);
        final RecyclerView recyclerSearchedItem = v.findViewById(R.id.alertdialog_receipt_recycler);
        recyclerSearchedItem.setHasFixedSize(true);
        recyclerSearchedItem.setLayoutManager(new LinearLayoutManager(this));
        alertDialogAdapter = new ReceiptAlertDialogAdapter(this, receiptItemList);
        recyclerSearchedItem.setAdapter(alertDialogAdapter);
        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.show();
        searchitemBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchitemType = addItemType.getText().toString();
                searchitemMerk = addItemMerk.getText().toString();

                if (searchitemType.isEmpty()){
                    searchitemMerkfilter = db.collection("watchlist").whereEqualTo("merk",searchitemMerk).get();
                    pbSearchItem.setVisibility(View.VISIBLE);
                    loadItem();
                    alertDialog.show();
                    receiptItemList.clear();

                }
                else if(!searchitemType.isEmpty()) {
                    searchtypemerk = searchitemType + " - "+searchitemMerk;
                    searchitemMerkfilter = db.collection("watchlist").whereEqualTo("type",searchtypemerk).get();
                    pbSearchItem.setVisibility(View.VISIBLE);
                    loadItem();
                    alertDialog.show();
                    receiptItemList.clear();

                }
            }
        });
        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);
                receiptItemList.add(listReceiptItem);
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
                alertDialog.dismiss();
                qrResult = null;
                Toast.makeText(CreateReceiptActivity.this, "barang tertambah", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void loadItem() {
        searchitemMerkfilter
                .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 date = document.getString("date");
                                String type = document.getString("type");
                                String qty = document.getString("qty");
                                String price = document.getString("price");
                                listReceiptItem = new ListReceiptItem(type, qty, price);
                                receiptItemList.add(listReceiptItem);
                            }
                            pbSearchItem.setVisibility(View.GONE);
                        } else {
                            Log.w(Tag.ITEM, "error getting documents", task.getException());
                        }
                    }
                });
    }

2 个答案:

答案 0 :(得分:0)

您可以在Activity中以相同方式使用findViewById,例如

alertDialog.findViewById(R.id.pb_progress).setVisible(View.INVISIBLE);

答案 1 :(得分:0)

您可以做到

 View v = LayoutInflater.from(getApplicationContext()).inflate(R.layout.alertdialog_create_receipt, null);

 final Builder builder = new Builder(CreateReceiptActivity.this);
        builder.setView(v);