EditText在适配器调用时不显示值,而EditText在滚动listView时丢失值。

时间:2017-11-17 16:13:53

标签: android listview android-edittext baseadapter android-viewholder

我的第一个问题是,在我的活动中,我在适配器中传递金额,以使用它在我的适配器中设置编辑文本值,但它没有显示。 我的第二个问题是当我在适配器中更改编辑文本的值时,当我滚动列表视图时,该值会丢失。

这是我在适配器中的代码:

public class CustomListView_InvoiceList extends BaseAdapter {

private Context mContext;
private List<InvoiceList> invoiceLists;
private float TotalPaymentAmount;

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

public static class ViewHolder {
    CheckBox SelectInvoiceCB, PayFull, PayPartial;
    TextView SalesInvoiceNo, InvoiceDate, InvoiceAmount, AmountDue, DueDate, CreditMemoID, CreditMemoDate, CreditMemoReason;
    LinearLayout LL2, LL3;
    EditText TotalAmount;
    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.AmountDue = (TextView) view.findViewById(R.id.SIAmountDue);
        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.CreditMemoID = (TextView) view.findViewById(R.id.creditMemoID);
        holder.CreditMemoDate = (TextView) view.findViewById(R.id.creditMemoDate);
        holder.CreditMemoReason = (TextView) view.findViewById(R.id.creditMemoReason);
        holder.LL2 = (LinearLayout) view.findViewById(R.id.ll2);
        holder.LL3 = (LinearLayout) view.findViewById(R.id.ll3);

        holder.SelectInvoiceCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                invoiceLists.get(getPosition).setSelectInvoice(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
            }
        });

        holder.PayFull.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                invoiceLists.get(getPosition).setPayFull(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                if (holder.PayFull.isChecked()) {
                    holder.PayPartial.setChecked(false);
                }
            }
        });

        holder.PayPartial.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                invoiceLists.get(getPosition).setPayPartial(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
                if (holder.PayPartial.isChecked()) {
                    holder.PayFull.setChecked(false);
                }
            }
        });

        view.setTag(holder);
        view.setTag(R.id.SITotalAmount, holder.TotalAmount);
        view.setTag(R.id.selectInvoiceCB, holder.SelectInvoiceCB);
        view.setTag(R.id.SIFull, holder.PayFull);
        view.setTag(R.id.SIPartial, holder.PayPartial);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    InvoicePopulate(view,holder, position);

    return view;
}

public void InvoicePopulate(final View view, final ViewHolder holder, final int position) {
    dbHelper = new DBHelper(mContext);

    Calendar c = Calendar.getInstance();
    SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
    final String formattedDate = df1.format(c.getTime());

    try {
        dbHelper.openDataBase();

        // LAYOUT VISIBILITY FOR CREDIT MEMOs
        try {
            if (invoiceLists.get(position).getCredit_Memo_ID().equals(null) || invoiceLists.get(position).getCredit_Memo_ID().isEmpty()
                    || invoiceLists.get(position).getCredit_Memo_ID().length() < 1
                    || invoiceLists.get(position).getCredit_Memo_ID().trim().equals("")){
                holder.CreditMemoID.setVisibility(View.GONE);
                holder.CreditMemoDate.setVisibility(View.GONE);
                holder.CreditMemoReason.setVisibility(View.GONE);
                holder.LL2.setVisibility(View.GONE);
                holder.LL3.setVisibility(View.GONE);
            } else {
                holder.LL2.setVisibility(View.VISIBLE);
                holder.LL3.setVisibility(View.VISIBLE);
                holder.CreditMemoID.setVisibility(View.VISIBLE);
                holder.CreditMemoDate.setVisibility(View.VISIBLE);
                holder.CreditMemoReason.setVisibility(View.VISIBLE);

                holder.CreditMemoID.setText(invoiceLists.get(position).getCredit_Memo_ID());
                holder.CreditMemoDate.setText(invoiceLists.get(position).getCredit_Memo_Date().substring(0,10));
                holder.CreditMemoReason.setText(invoiceLists.get(position).getCredit_Memo_Reason());
            }
        } catch (Exception e) {
            e.getMessage();
        }

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

        float invAmount = 0;
        invAmount = Math.round(Float.parseFloat(invoiceLists.get(position).getInvoice_Amount())*100.00)/(float)100.00;
        holder.InvoiceAmount.setText(String.format("%,.2f",invAmount));
        holder.AmountDue.setText(String.format("%,.2f",invAmount));
        //holder.InvoiceAmount.setText(invoiceLists.get(position).getInvoice_Amount());

        holder.TotalAmount.setTag(position);
        holder.SelectInvoiceCB.setTag(position); // This line is important.
        holder.PayFull.setTag(position); // This line is important.
        holder.PayPartial.setTag(position); // This line is important.


        holder.TotalAmount.setText(invoiceLists.get(position).getAmount_Paid());
        holder.SelectInvoiceCB.setChecked(invoiceLists.get(position).isSelectInvoice());
        holder.PayFull.setChecked(invoiceLists.get(position).isPayFull());
        holder.PayPartial.setChecked(invoiceLists.get(position).isPayPartial());

        try {
            if (invoiceLists.get(position).getAmount_Paid().toString().equals("") ||
                    invoiceLists.get(position).getAmount_Paid().toString().equals("0.0") ||
                    Float.parseFloat(invoiceLists.get(position).getAmount_Paid().toString()) < 1) {
                invAmount = 0;
                invAmountDue = 0;
                invAmountPaid = 0;
                //invAmount = Float.parseFloat(invoiceLists.get(position).getInvoice_Amount());
                invAmount = Math.round(Float.parseFloat(invoiceLists.get(position).getInvoice_Amount())*100.00)/(float)100.00;
                invAmountDue = invAmount - invAmountPaid;
                Log.e("Without AmountPaid ", "Amount Due : " + String.valueOf(invAmountDue));
            } else {
                invAmount = 0;
                invAmountDue = 0;
                invAmountPaid = Math.round(Float.parseFloat(invoiceLists.get(position).getAmount_Paid())*100.00)/(float)100.00;
                //invAmount = Float.parseFloat(invoiceLists.get(position).getInvoice_Amount());
                invAmount = Math.round(Float.parseFloat(invoiceLists.get(position).getInvoice_Amount())*100.00)/(float)100.00;
                invAmountDue = invAmount - invAmountPaid;
                Log.e("With AmountPaid ", "Amount Due : " + String.valueOf(invAmountDue));
            }

            holder.AmountDue.setText(String.format("%,.2f",invAmountDue));
            //holder.InvoiceAmount.setText(invoiceLists.get(position).getInvoice_Amount());
        } catch (Exception e) {
            e.getMessage();
        }

        /*final float finalInvAmount = invAmountDue;
        holder.PayFull.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (holder.PayFull.isChecked()) {
                    holder.PayPartial.setChecked(false);
                    if (holder.SelectInvoiceCB.isChecked()) {
                        invoiceStatusValue = "PAID_FULL";
                        holder.TotalAmount.setText(String.valueOf(Math.round(finalInvAmount*100.00)/100.00));
                        //holder.TotalAmount.setText(holder.InvoiceAmount.getText().toString());
                    }
                }
            }
        });

        holder.PayPartial.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (holder.PayPartial.isChecked()) {
                    holder.PayFull.setChecked(false);
                    if (holder.SelectInvoiceCB.isChecked()) {
                        invoiceStatusValue = "PAID_PARTIAL";
                        holder.TotalAmount.setText("0.00");
                    }
                }
            }
        });*/

        if (TotalPaymentAmount >= Float.parseFloat(String.valueOf(invAmountDue))) {
            holder.SelectInvoiceCB.setChecked(true);
            holder.PayFull.setChecked(true);
            holder.PayFull.setClickable(true);
            holder.PayPartial.setClickable(true);

            holder.TotalAmount.setText(String.valueOf(Math.round(invAmountDue * 100.00) / 100.00));

            System.out.println("TotalPaymentAmount: " + TotalPaymentAmount);
            System.out.println("invAmountDue: " + invAmountDue);

            if (Float.parseFloat(String.valueOf(invAmountDue)) < 1) {
                Log.e("Computations : ", TotalPaymentAmount + " + " + String.valueOf(Float.parseFloat(String.valueOf(invAmountDue))));
                Log.e("Equals ", "TotalPaymentAmount = " + TotalPaymentAmount);
            } else {
                TotalPaymentAmount = TotalPaymentAmount - Float.parseFloat(String.valueOf(invAmountDue));
            }
        } else {
            if (TotalPaymentAmount < 1) {
                holder.TotalAmount.setText("0.00");
                /*holder.PayFull.setClickable(false);
                holder.PayPartial.setClickable(false);*/

            } else {
                holder.SelectInvoiceCB.setChecked(true);
                holder.PayPartial.setChecked(true);
                holder.PayFull.setClickable(true);
                holder.PayPartial.setClickable(true);
                holder.TotalAmount.setText(String.valueOf(Math.round(TotalPaymentAmount * 100.00) / 100.00));

                if ((TotalPaymentAmount - Float.parseFloat(String.valueOf(invAmountDue))) < 0) {
                    TotalPaymentAmount = (float) 0.0;
                } else {
                    TotalPaymentAmount = TotalPaymentAmount - Float.parseFloat(String.valueOf(invAmountDue));
                }
            }
        }

    } catch (Exception e) {
        e.getMessage();
        System.out.println("Custom Invoice List: " + e);
    } finally {
        dbHelper.close();
    }

}

1 个答案:

答案 0 :(得分:0)

首先,我建议使用RecyclerView而不是ListView,一个简单的实现将避免滚动问题。 我推荐这些链接:
https://developer.android.com/training/material/lists-cards.html https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html

其次,方法invoicePopulate *在每次创建视图时打开SQLite数据库,这会产生严重的性能问题,因为滚动视图会被重新创建,并且最终会生成多个实例,最终会出现错误,您将无法访问数据库。为此,我建议您先前计算数据并将其传递给已准备好的适配器。

最好使用适配器只显示信息,如果你需要计算之前尝试这样做。