如何在myAdapter中的AlertDialog中创建的ImageView中设置图像?我正在使用Base Adapter

问题描述 投票:0回答:1

我在我的(主要活动)中创建了一个Button(1)当我点击按钮(1)时它会显示一个包含自定义ListView的AlertDialog(1)。在那个Custom ListView中有一个Button,当点击它时它也会显示一个AlertDialog(2),它包含一个ImageView(Clickable)。我要做的是如何将图像设置为我的ImageView。这是我的代码:

MainActivity.java - 这是按钮的OnClick(1)

public void PayInvoice(View view){
    String minAmount = null;

    myDbHelper = new DBHelper(this);
    try {
        myDbHelper.createDataBase();
        myDbHelper.openDataBase();
        CustomerID = "1600050";

        minAmount = myDbHelper.getMinAmountDue(CustomerID);
    } catch (Exception e) {
        e.getMessage();
    }
    myDbHelper.close();

    if (TotalAmountET.getText().equals("") || TotalAmountET.getText().length() == 0) {
        Toast.makeText(this, "PUT PAYMENT AMOUNT FIRST...", Toast.LENGTH_SHORT).show();
    } else if (Float.parseFloat(minAmount) > Float.parseFloat(TotalAmountET.getText().toString()) ) {
        Toast.makeText(this, "TOTAL AMOUNT IS NOT ENOUGH...", Toast.LENGTH_SHORT).show();
    } else {
        AlertDialog.Builder alertdialogbuilder = new AlertDialog.Builder(PaymentHeader.this);
        LayoutInflater inflater = getLayoutInflater();
        View alertLayout = inflater.inflate(R.layout.sales_invoice_list, null);
        alertdialogbuilder.setView(alertLayout);

        final ListView SalesInvoiceList = (ListView) alertLayout.findViewById(R.id.invoiceList);
        final Button Cancel = (Button) alertLayout.findViewById(R.id.cancelBTN);
        final Button PayInvoice = (Button) alertLayout.findViewById(R.id.payinvoiceBTN);

        final AlertDialog alertDialog = alertdialogbuilder.create();
        alertDialog.setCanceledOnTouchOutside(false);
        alertDialog.show();

        myDbHelper = new DBHelper(this);
        try {
            myDbHelper.createDataBase();
            myDbHelper.openDataBase();

            CustomerID = "1600050";

            try {
                invoiceLists = myDbHelper.retrieveInvoices(CustomerID);
                customListView_invoiceList = new CustomListView_InvoiceList
                        (this, invoiceLists,Float.parseFloat(TotalAmountET.getText().toString()));
                SalesInvoiceList.setAdapter(customListView_invoiceList);
            } catch (Exception e) {
                e.getMessage();
            }

        } catch (Exception e) {
            e.getMessage();
        }

        Cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });

        PayInvoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insertPaymentLine();
            }
        });
    }
}

CustomListView_InvoiceList.java - 这是我如何在AlertDialog(1)中填充ListView。

public class CustomListView_InvoiceList extends BaseAdapter {
private Context mContext;
private List<InvoiceList> invoiceLists;
private Float TotalPaymentAmount;
DBHelper myDbHelper;
List DeductionType;
private ArrayAdapter<String> DeductionTypeAdapter;
private final int requestCode = 20;

public CustomListView_InvoiceList(Context mContext, List<InvoiceList> invoiceLists, Float totalPaymentAmount) {
    this.mContext = mContext;
    this.invoiceLists = invoiceLists;
    this.TotalPaymentAmount = totalPaymentAmount;
}

@Override
public int getCount() {
    return invoiceLists.size();
}

@Override
public Object getItem(int position) {
    return invoiceLists.get(position);
}

@Override
public long getItemId(int position) {
    return invoiceLists.get(position).getId();
}

public static class ViewHolder {
    CheckBox SelectInvoiceCB, PayFull, PayPartial;
    TextView SalesInvoiceNo, InvoiceDate, InvoiceAmount, DueDate;
    EditText TotalAmount;
    LinearLayout adddeductionBTN;
    ImageView CaptureForm;
}

@Override
public View getView(final int position, View view, ViewGroup viewGroup) {
    //View v = View.inflate(mContext, R.layout.sales_invoice_custom,null);
    final ViewHolder holder;

    if(view == null)
    {
        view = View.inflate(mContext, R.layout.sales_invoice_custom,null);
        holder = new ViewHolder();

        holder.SelectInvoiceCB = (CheckBox) view.findViewById(R.id.selectInvoiceCB);
        holder.SalesInvoiceNo = (TextView) view.findViewById(R.id.SINo);
        holder.InvoiceDate = (TextView) view.findViewById(R.id.SIDate);
        holder.InvoiceAmount = (TextView) view.findViewById(R.id.SIAmount);
        holder.DueDate = (TextView) view.findViewById(R.id.SIdueDate);
        holder.PayFull = (CheckBox) view.findViewById(R.id.SIFull);
        holder.PayPartial = (CheckBox) view.findViewById(R.id.SIPartial);
        holder.TotalAmount = (EditText) view.findViewById(R.id.SITotalAmount);
        holder.adddeductionBTN = (LinearLayout) view.findViewById(R.id.adddeductionBTN);

        holder.SalesInvoiceNo.setText(invoiceLists.get(position).getSales_Invoice_ID());
        holder.InvoiceDate.setText(invoiceLists.get(position).getInvoice_Date());
        holder.InvoiceAmount.setText(invoiceLists.get(position).getAmount_Due());
        holder.DueDate.setText(invoiceLists.get(position).getDue_Date());

        holder.PayFull.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (holder.PayFull.isChecked()){
                    holder.PayPartial.setChecked(false);
                }

            }
        });

        holder.PayPartial.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (holder.PayPartial.isChecked()){
                    holder.PayFull.setChecked(false);
                }
            }
        });

        try {
            if (TotalPaymentAmount >= Float.parseFloat(holder.InvoiceAmount.getText().toString())) {
                holder.SelectInvoiceCB.setChecked(true);
                holder.PayFull.setChecked(true);
                holder.TotalAmount.setText(holder.InvoiceAmount.getText().toString());

                TotalPaymentAmount = TotalPaymentAmount - Float.parseFloat(holder.InvoiceAmount.getText().toString());
            } else {
                if (TotalPaymentAmount < 1) {
                    holder.TotalAmount.setText("0.00");
                } else {
                    holder.SelectInvoiceCB.setChecked(true);
                    holder.PayPartial.setChecked(true);
                    holder.TotalAmount.setText(String.valueOf(TotalPaymentAmount));
                    TotalPaymentAmount = TotalPaymentAmount - Float.parseFloat(holder.InvoiceAmount.getText().toString());
                    Log.e("","Remaining Payment Amount: " + String.valueOf(TotalPaymentAmount));
                }
            }
        } catch (Exception e) {
            e.getMessage();
        }

        holder.adddeductionBTN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertdialogbuilder = new AlertDialog.Builder(mContext);
                //LayoutInflater inflater = getLayoutInflater();
                View alertLayout = LayoutInflater.from(mContext).inflate(R.layout.deduction_list,null);
                alertdialogbuilder.setView(alertLayout);

                final Spinner TypesOfDeduction = (Spinner) alertLayout.findViewById(R.id.typeOfDeduction);
                final EditText DeductionRemarks = (EditText) alertLayout.findViewById(R.id.deductionRemarks);
                final EditText DeductionAmount = (EditText) alertLayout.findViewById(R.id.deductionAmount);
                final CheckBox DeductionWithForm = (CheckBox) alertLayout.findViewById(R.id.withForm);
                holder.CaptureForm = (ImageView) alertLayout.findViewById(R.id.captureForm);
                final Button Cancel = (Button) alertLayout.findViewById(R.id.cancelBTN);
                final Button AddDeduction = (Button) alertLayout.findViewById(R.id.adddeductionBTN);

                final AlertDialog alertDialog = alertdialogbuilder.create();
                alertDialog.setCanceledOnTouchOutside(false);
                alertDialog.show();

                DeductionRemarks.setText(invoiceLists.get(position).getSales_Invoice_Line_ID());

                myDbHelper = new DBHelper(mContext);
                try {
                    myDbHelper.createDataBase();
                    myDbHelper.openDataBase();

                    DeductionType = myDbHelper.retrieveDeduction();
                    DeductionTypeAdapter = new ArrayAdapter<String>
                            (mContext, R.layout.spinner_single_line, DeductionType);
                    DeductionTypeAdapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
                    TypesOfDeduction.setAdapter(DeductionTypeAdapter);

                    TypesOfDeduction.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            String ImageRequired = "";
                            myDbHelper = new DBHelper(mContext);
                            try {
                                myDbHelper.createDataBase();
                                myDbHelper.openDataBase();
                                ImageRequired = myDbHelper.getDeduction(TypesOfDeduction.getSelectedItem().toString(), "Image Required");
                            } catch (Exception e) {
                                e.getMessage();
                            }
                            myDbHelper.close();

                            if (ImageRequired.equals("YES")) {
                                DeductionWithForm.setEnabled(true);
                                Toast.makeText(mContext,
                                        "Deduction Type: " + TypesOfDeduction.getSelectedItem().toString() +
                                                " Image Required: " + ImageRequired, Toast.LENGTH_SHORT).show();
                            } else {
                                DeductionWithForm.setEnabled(false);
                                Toast.makeText(mContext,
                                        "Deduction Type: " + TypesOfDeduction.getSelectedItem().toString() +
                                                " Image Required: " + ImageRequired, Toast.LENGTH_SHORT).show();
                            }
                        }

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

                        }
                    });

                    DeductionWithForm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            if (DeductionWithForm.isChecked()){
                                holder.CaptureForm.setEnabled(true);
                            } else {
                                holder.CaptureForm.setEnabled(false);
                            }
                        }
                    });

                } catch (Exception e){
                    e.getMessage();
                }
                myDbHelper.close();

                Cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog.dismiss();
                    }
                });

                holder.CaptureForm.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent photoCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        ((Activity) mContext).startActivityForResult(photoCaptureIntent,requestCode);
                    }
                });

            }
        });

        view.setTag(holder);
    }else{
        holder = (ViewHolder) view.getTag();
    }
    return view;
}
}
android listview android-alertdialog baseadapter
1个回答
1
投票

根据您的要求创建自定义对话框类和实现接口,请遵循此示例对话框类代码

  public class AddFlightDialog extends Dialog {


    @Bind(R.id.edtTxtflightnumber)
    EditText edtTxtflightnumber;
    @Bind(R.id.btnAddComment)
    Button btnAddComment;
    @Bind(R.id.btnCancelComment)
    Button btnCancelComment;
    private Context mContext;
    FlightNumberListner flightNumberListner;

    @OnClick(R.id.btnAddComment)
    void btnAddComment() {

        flightNumberListner.addflightnumber(edtTxtflightnumber.getText().toString());

        dismiss();
    }

    @OnClick(R.id.btnCancelComment)
    void closeDialog() {

        flightNumberListner.noflightNumber();
        dismiss();
    }

    String comment = "";

    public AddFlightDialog(String comment, Context context, FlightNumberListner flightNumberListner) {
        super(context, R.style.Theme_Custom);
        this.flightNumberListner = flightNumberListner;
        this.comment = comment;
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_add_flight);
        ButterKnife.bind(CustomDialog.this);
    }

    public interface FlightNumberListner {
        public void addflightnumber(String flightnum);

        public void noflightNumber();
    }
}

对话调用方法

new AddFlightDialog(comment, getActivity(), new AddFlightDialog.FlightNumberListner() {
                @Override
                public void addflightnumber(String flightnum) {
                    //your code
                }

                @Override
                public void noflightNumber() {
                    //your code
                }
            }).show();
© www.soinside.com 2019 - 2024. All rights reserved.