import android.content.Context;
import android.content.res.Resources;
+import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TableLayout;
+import android.widget.TableRow;
import android.widget.TextView;
import net.ktnx.mobileledger.model.LedgerTransaction;
+import net.ktnx.mobileledger.model.LedgerTransactionItem;
+import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
+import java.util.Iterator;
import java.util.List;
class TransactionListAdapter
LedgerTransaction tr = transactions.get(position);
Context ctx = holder.row.getContext();
Resources rm = ctx.getResources();
+ try (MobileLedgerDatabase dbh = new MobileLedgerDatabase(ctx)) {
+ try (SQLiteDatabase db = dbh.getReadableDatabase()) {
+ tr.loadData(db);
- holder.tvDescription.setText(String.format("%s\n%s", tr.getDescription(), tr.getDate()));
- TableLayout tbl = holder.row.findViewById(R.id.transaction_row_acc_amounts);
- tbl.removeAllViews();
- for (Iterator<LedgerTransactionItem> it = tr.getItemsIterator(); it.hasNext(); ) {
- LedgerTransactionItem acc = it.next();
- TableRow row = new TableRow(holder.row.getContext());
- TextView child = new TextView(ctx);
- child.setText(acc.getShortAccountName());
- row.addView(child);
- child = new TextView(ctx);
- child.setText(acc.toString());
- row.addView(child);
- tbl.addView(row);
- }
+ holder.tvDescription
+ .setText(String.format("%s\n%s", tr.getDescription(), tr.getDate()));
+ TableLayout tbl = holder.row.findViewById(R.id.transaction_row_acc_amounts);
+ tbl.removeAllViews();
+ for (Iterator<LedgerTransactionItem> it = tr.getItemsIterator(); it.hasNext(); ) {
+ LedgerTransactionItem acc = it.next();
+ TableRow row = new TableRow(holder.row.getContext());
+ TextView child = new TextView(ctx);
+ child.setText(acc.getShortAccountName());
+ row.addView(child);
+ child = new TextView(ctx);
+ child.setText(acc.toString());
+ row.addView(child);
+ tbl.addView(row);
+ }
- if (position % 2 == 0) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
- .setBackgroundColor(rm.getColor(R.color.table_row_even_bg, ctx.getTheme()));
- else holder.row.setBackgroundColor(rm.getColor(R.color.table_row_even_bg));
- }
- else {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
- .setBackgroundColor(rm.getColor(R.color.drawer_background, ctx.getTheme()));
- else holder.row.setBackgroundColor(rm.getColor(R.color.drawer_background));
- }
+ if (position % 2 == 0) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
+ .setBackgroundColor(
+ rm.getColor(R.color.table_row_even_bg, ctx.getTheme()));
+ else holder.row.setBackgroundColor(rm.getColor(R.color.table_row_even_bg));
+ }
+ else {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
+ .setBackgroundColor(
+ rm.getColor(R.color.drawer_background, ctx.getTheme()));
+ else holder.row.setBackgroundColor(rm.getColor(R.color.drawer_background));
+ }
- holder.row.setTag(R.id.POS, position);
+ holder.row.setTag(R.id.POS, position);
+ }
+ }
}
@NonNull
package net.ktnx.mobileledger.model;
+import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import net.ktnx.mobileledger.utils.Digest;
private String description;
private ArrayList<LedgerTransactionItem> items;
private String dataHash;
+ private boolean dataLoaded;
public LedgerTransaction(String id, String date, String description) {
this.id = id;
this.date = date;
this.description = description;
this.items = new ArrayList<>();
this.dataHash = null;
+ dataLoaded = false;
}
public LedgerTransaction(int id, String date, String description) {
this(String.valueOf(id), date, description);
public LedgerTransaction(String date, String description) {
this(null, date, description);
}
+ public LedgerTransaction(int id) {
+ this(id, null, null);
+ }
public void add_item(LedgerTransactionItem item) {
items.add(item);
dataHash = null;
String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);
}
}
+ public void loadData(SQLiteDatabase db) {
+ if (dataLoaded) return;
+
+ try (Cursor cTr = db.rawQuery("SELECT date, description from transactions WHERE " +
+ "id=?",new String[]{id})) {
+ if (cTr.moveToFirst()) {
+ date = cTr.getString(0);
+ description = cTr.getString(1);
+
+ try (Cursor cAcc = db.rawQuery("SELECT account_name, amount, currency FROM " +
+ "transaction_accounts WHERE transaction_id = ?",
+ new String[]{id}))
+ {
+ while (cAcc.moveToNext()) {
+ add_item(
+ new LedgerTransactionItem(cAcc.getString(0), cAcc.getFloat(1),
+ cAcc.getString(2)));
+ }
+ }
+ }
+ }
+
+ }
}
import android.database.sqlite.SQLiteDatabase;
import net.ktnx.mobileledger.model.LedgerTransaction;
-import net.ktnx.mobileledger.model.LedgerTransactionItem;
import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
import java.util.ArrayList;
}
private void reloadTransactions(MobileLedgerDatabase dbh) {
transactions.clear();
- String sql = "SELECT id, date, description FROM transactions";
- sql += " ORDER BY date desc, id desc";
+ String sql = "SELECT id FROM transactions ORDER BY date desc, id desc";
try (SQLiteDatabase db = dbh.getReadableDatabase()) {
try (Cursor cursor = db.rawQuery(sql, null)) {
while (cursor.moveToNext()) {
- LedgerTransaction tr =
- new LedgerTransaction(cursor.getString(0), cursor.getString(1),
- cursor.getString(2));
- try (Cursor cAcc = db.rawQuery("SELECT account_name, amount, currency FROM " +
- "transaction_accounts WHERE transaction_id = ?",
- new String[]{tr.getId()}))
- {
- while (cAcc.moveToNext()) {
- tr.add_item(
- new LedgerTransactionItem(cAcc.getString(0), cAcc.getFloat(1),
- cAcc.getString(2)));
- }
- }
- transactions.add(tr);
+ transactions.add(new LedgerTransaction(cursor.getInt(0)));
}
}
}