在适配器中两次获得列表的第一名

时间:2019-04-25 11:12:19

标签: android listview

我想对每一行的余额求和,但是我两次都获得了listview的第一名,其他人没有重复我如何解决这个问题?

这是我的适配器代码。

公共类LedgerClientData扩展了ArrayAdapter实现的View.OnClickListener {

private ArrayList<LedgerModel.T0> dataSet;
Activity mContext;
private int lastPosition = -1;
LayoutInflater inflater;
private ArrayList<Double> debitList;
private ArrayList<Double> creditList;
private static Double debitsum=0.0;
private static Double creditSum=0.0;


private static class ViewHolder {

    TextView tvNarrationText, tvDate, tvBalance, tvDebitCredit;


}

public LedgerClientData(ArrayList<LedgerModel.T0> data, Activity context) {
    super(context, R.layout.item_ledger_report, data);
    //super(context,R.layout.);

    this.dataSet = data;
    this.mContext = context;
    inflater = LayoutInflater.from(context);

}

@Override
public void onClick(View v) {

    //int position = (Integer) v.getTag();
    //Object object = getItem(position);
    //LedgerModel.T0 t0 = (LedgerModel.T0) object;


}

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

@Override
public LedgerModel.T0 getItem(int position) {
    return super.getItem(position);
}

@Override
public long getItemId(int position) {
    return super.getItemId(position);
}

public View getView(int position, View convertView, ViewGroup parent) {
    LedgerModel.T0 dataModel = dataSet.get(position);
    // Check if an existing view is being reused, otherwise inflate the view
    ViewHolder viewHolder; // view lookup cache stored in tag

    debitList = new ArrayList<>();
    creditList = new ArrayList<>();

    final View result;

    if (convertView == null) {

        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.item_ledger_report, parent, false);

        viewHolder.tvNarrationText = (TextView) convertView.findViewById(R.id.tv_narration_txt);
        viewHolder.tvDate = (TextView) convertView.findViewById(R.id.tv_date);
        viewHolder.tvBalance = (TextView) convertView.findViewById(R.id.tv_balance);
        viewHolder.tvDebitCredit = (TextView) convertView.findViewById(R.id.tv_debit_credit);


        result = convertView;

        convertView.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) convertView.getTag();
        result = convertView;
    }


    //lastPosition = position;

    String date = dataModel.getVOUCHERDATE();
    Double credit = dataModel.getCRAMT();
    Double debit = dataModel.getDRAMT();


    debitsum = debitsum + debit;
    creditSum = creditSum + credit;
    Double balance = debitsum - creditSum;

    Log.e("credit =="+position,String.valueOf(credit) );
    Log.e("debit==="+position,String.valueOf(debit) );
    Log.e("balance===",String.valueOf(balance) );

    if (Math.round(debitsum) - Math.round(creditSum) > 0) {

        viewHolder.tvBalance.setText(balance.toString() + " DR");
    } else {
        viewHolder.tvBalance.setText(balance.toString() + " CR");

    }
    double creditRounded = Math.round(credit * 100.0) / 100.0;
    double debitRounded = Math.round(debit * 100.0) / 100.0;


    if ((debitRounded - creditRounded) > 0) {

        viewHolder.tvDebitCredit.setText(String.valueOf(debitRounded) + "DR");


        // lbl.Text = lbl.Text + " DR";
    } else


        viewHolder.tvDebitCredit.setText(String.valueOf(creditRounded) + "CR");
        viewHolder.tvNarrationText.setText(dataModel.getNarration());
        viewHolder.tvDate.setText(dataModel.getBILLDATE());


    // Return the completed view to render on screen
    return convertView;
}

}

这是我要传递清单的活动。

private void ledgerReportData(){

    final APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);
    final LedgerModel ledgerModel = new LedgerModel(String.valueOf(100168), "242", "A", "03/01/2018", "01/01/2019");
    Call<LedgerModel> call1 = apiInterface.createLedgerReport(ledgerModel);
    call1.enqueue(new Callback<LedgerModel>() {
        @SuppressLint("ResourceType")
        @Override
        public void onResponse(Call<LedgerModel> call, Response<LedgerModel> response) {

            LedgerModel user1 = response.body();

            Log.e("in response", "");

            progressBar.setVisibility(View.GONE);





                ArrayList<LedgerModel.T0> tolist = (ArrayList<LedgerModel.T0>) user1.getT0();


                ledgerClientData = new LedgerClientData(tolist, SearchLedgerReport.this);
                lstClientScheme.setAdapter(ledgerClientData);





            //pbar.setVisibility(View.GONE);


        }

        @Override
        public void onFailure(Call<LedgerModel> call, Throwable t) {

            Log.e("Error is==", t.getMessage());
            //pbar.setVisibility(View.GONE);
        }


    });

}

我正在传递适配器中的对象列表,如何解决呢?仅第一个零位在重复。 我正在传递适配器中的对象列表,如何解决呢?只有第一个零位在重复。

0 个答案:

没有答案