2017-05-12 65 views
0

我在我的(主要活動)中創建了一個按鈕(1)當我點擊按鈕(1)時會發生什麼,它將顯示包含自定義ListView的AlertDialog(1)。在該自定義ListView中有一個按鈕,單擊它時也會顯示一個AlertDialog(2),它包含一個ImageView(可點擊)。我想要做的是如何設置圖像到我的ImageView。這裏是我的代碼:如何在myAdapter中的AlertDialog中創建的ImageView中設置圖像?我使用基本適配器

MainActivity.java - 這是我的按鈕(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的OnClick - 這是我如何填充ListView在我的AlertDialog(1)。

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; 
} 

}

請幫幫我!如果我的代碼中有任何問題,請告訴我。謝謝!

+0

創建自定義對話框類和實現接口按您的要求 –

+0

對不起,先生。你能解釋一下嗎? –

回答

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(); 
相關問題