2 * Copyright © 2018 Damyan Ivanov.
3 * This file is part of Mobile-Ledger.
4 * Mobile-Ledger is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * Mobile-Ledger is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.database.sqlite.SQLiteDatabase;
23 import android.support.annotation.NonNull;
24 import android.support.v7.widget.RecyclerView;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.LinearLayout;
29 import android.widget.TableLayout;
30 import android.widget.TableRow;
31 import android.widget.TextView;
33 import net.ktnx.mobileledger.model.LedgerTransaction;
34 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
35 import net.ktnx.mobileledger.utils.Globals;
36 import net.ktnx.mobileledger.utils.MLDB;
38 import java.util.Iterator;
39 import java.util.List;
41 class TransactionListAdapter
42 extends RecyclerView.Adapter<TransactionListAdapter.TransactionRowHolder> {
43 private List<LedgerTransaction> transactions;
45 TransactionListAdapter(List<LedgerTransaction> transactions) {
46 this.transactions = transactions;
49 public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
50 LedgerTransaction tr = transactions.get(position);
51 Context ctx = holder.row.getContext();
53 try (SQLiteDatabase db = MLDB.getReadableDatabase(ctx)) {
57 .setText(String.format("%s\n%s", tr.getDescription(), tr.getDate()));
58 TableLayout tbl = holder.row.findViewById(R.id.transaction_row_acc_amounts);
60 for (LedgerTransactionAccount acc : tr.getAccounts()) {
61 TableRow row = new TableRow(holder.row.getContext());
62 TextView child = new TextView(ctx);
63 child.setText(acc.getShortAccountName());
65 child = new TextView(ctx);
66 child.setText(acc.toString());
71 if (position % 2 == 0) {
72 holder.row.setBackgroundColor(Globals.table_row_even_bg);
75 holder.row.setBackgroundColor(Globals.table_row_odd_bg);
78 holder.row.setTag(R.id.POS, position);
84 public TransactionRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
85 View row = LayoutInflater.from(parent.getContext())
86 .inflate(R.layout.transaction_list_row, parent, false);
87 return new TransactionRowHolder(row);
91 public int getItemCount() {
92 return transactions.size();
94 class TransactionRowHolder extends RecyclerView.ViewHolder {
95 TextView tvDescription;
96 TableLayout tableAccounts;
98 public TransactionRowHolder(@NonNull View itemView) {
100 this.row = (LinearLayout) itemView;
101 this.tvDescription = itemView.findViewById(R.id.transaction_row_description);
102 this.tableAccounts = itemView.findViewById(R.id.transaction_row_acc_amounts);