From 6b457e451724f7533970e5699eba2912a62a70f3 Mon Sep 17 00:00:00 2001 From: Benny Handfast Date: Mon, 2 Feb 2015 23:09:12 +0100 Subject: [PATCH] Using custom adapters for the table activities. --- .../dexdrip/Tables/BgReadingTable.java | 65 +++++++++++++++++-- .../dexdrip/Tables/CalibrationDataTable.java | 61 +++++++++++++++-- .../main/res/layout/raw_data_list_item.xml | 12 ++-- 3 files changed, 121 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/Tables/BgReadingTable.java b/app/src/main/java/com/eveningoutpost/dexdrip/Tables/BgReadingTable.java index a3c4698b..ab82c9fc 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/Tables/BgReadingTable.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/Tables/BgReadingTable.java @@ -1,15 +1,25 @@ package com.eveningoutpost.dexdrip.Tables; import android.app.ListActivity; +import android.content.Context; import android.database.Cursor; import android.os.Bundle; import android.support.v4.widget.DrawerLayout; +import android.text.Layout; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CursorAdapter; import android.widget.SimpleCursorAdapter; +import android.widget.TextView; import com.activeandroid.Cache; +import com.eveningoutpost.dexdrip.Models.BgReading; import com.eveningoutpost.dexdrip.NavigationDrawerFragment; import com.eveningoutpost.dexdrip.R; +import java.util.Date; + public class BgReadingTable extends ListActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks { private String menu_name = "BG Data Table"; @@ -36,13 +46,58 @@ public void onNavigationDrawerItemSelected(int position) { private void getData() { Cursor cursor = Cache.openDatabase().rawQuery("Select * from BgReadings order by _ID desc limit 50", null); - SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, - R.layout.raw_data_list_item, - cursor, - new String[] { "calculated_value", "age_adjusted_raw_value", "raw_data", "time_since_sensor_started" }, - new int[] { R.id.raw_data_id, R.id.raw_data_value , R.id.raw_data_slope, R.id.raw_data_timestamp }); +// SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, +// R.layout.raw_data_list_item, +// cursor, +// new String[] { "calculated_value", "age_adjusted_raw_value", "raw_data", "time_since_sensor_started" }, +// new int[] { R.id.raw_data_id, R.id.raw_data_value , R.id.raw_data_slope, R.id.raw_data_timestamp }); + + CursorAdapter adapter = new BgReadingCursorAdapter(this, cursor, 0); this.setListAdapter(adapter); } + + public static class BgReadingCursorAdapterViewHolder { + TextView raw_data_id; + TextView raw_data_value; + TextView raw_data_slope; + TextView raw_data_timestamp; + + public BgReadingCursorAdapterViewHolder(View root) { + raw_data_id = (TextView) root.findViewById(R.id.raw_data_id); + raw_data_value = (TextView) root.findViewById(R.id.raw_data_value); + raw_data_slope = (TextView) root.findViewById(R.id.raw_data_slope); + raw_data_timestamp = (TextView) root.findViewById(R.id.raw_data_timestamp); + } + } + + public static class BgReadingCursorAdapter extends CursorAdapter { + BgReading bgReading = new BgReading(); + + public BgReadingCursorAdapter(Context context, Cursor c, int flags) { + super(context, c, flags); + } + + @Override + public View newView(Context context, Cursor cursor, ViewGroup parent) { + final View view = LayoutInflater.from(context).inflate(R.layout.raw_data_list_item, parent, false); + + final BgReadingCursorAdapterViewHolder holder = new BgReadingCursorAdapterViewHolder(view); + view.setTag(holder); + + return view; + } + + @Override + public void bindView(View view, Context context, Cursor cursor) { + bgReading.loadFromCursor(cursor); + + final BgReadingCursorAdapterViewHolder tag = (BgReadingCursorAdapterViewHolder) view.getTag(); + tag.raw_data_id.setText(Double.toString(bgReading.calculated_value)); + tag.raw_data_value.setText(Double.toString(bgReading.age_adjusted_raw_value)); + tag.raw_data_slope.setText(Double.toString(bgReading.raw_data)); + tag.raw_data_timestamp.setText(new Date(bgReading.timestamp).toString()); + } + } } diff --git a/app/src/main/java/com/eveningoutpost/dexdrip/Tables/CalibrationDataTable.java b/app/src/main/java/com/eveningoutpost/dexdrip/Tables/CalibrationDataTable.java index 6ca194b2..5f07f995 100644 --- a/app/src/main/java/com/eveningoutpost/dexdrip/Tables/CalibrationDataTable.java +++ b/app/src/main/java/com/eveningoutpost/dexdrip/Tables/CalibrationDataTable.java @@ -1,13 +1,18 @@ package com.eveningoutpost.dexdrip.Tables; import android.app.ListActivity; +import android.content.Context; import android.database.Cursor; import android.os.Bundle; import android.support.v4.widget.DrawerLayout; +import android.view.LayoutInflater; import android.view.View; -import android.widget.SimpleCursorAdapter; +import android.view.ViewGroup; +import android.widget.CursorAdapter; +import android.widget.TextView; import com.activeandroid.Cache; +import com.eveningoutpost.dexdrip.Models.Calibration; import com.eveningoutpost.dexdrip.NavigationDrawerFragment; import com.eveningoutpost.dexdrip.R; @@ -43,11 +48,13 @@ public void onNavigationDrawerItemSelected(int position) { private void getData() { Cursor cursor = Cache.openDatabase().rawQuery("Select * from Calibration order by timestamp desc", null); - SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, - R.layout.raw_data_list_item, - cursor, - new String[] { "bg", "estimate_raw_at_time_of_calibration", "slope", "intercept" }, - new int[] { R.id.raw_data_id, R.id.raw_data_value , R.id.raw_data_slope, R.id.raw_data_timestamp }); +// SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, +// R.layout.raw_data_list_item, +// cursor, +// new String[] { "bg", "estimate_raw_at_time_of_calibration", "slope", "intercept" }, +// new int[] { R.id.raw_data_id, R.id.raw_data_value , R.id.raw_data_slope, R.id.raw_data_timestamp }); + + CalibrationDataCursorAdapter adapter = new CalibrationDataCursorAdapter(this, cursor, 0); this.setListAdapter(adapter); // ListView listView = (ListView) findViewById(R.id.list); @@ -55,4 +62,46 @@ private void getData() { } + public static class CalibrationDataCursorAdapterViewHolder { + TextView raw_data_id; + TextView raw_data_value; + TextView raw_data_slope; + TextView raw_data_timestamp; + + public CalibrationDataCursorAdapterViewHolder(View root) { + raw_data_id = (TextView) root.findViewById(R.id.raw_data_id); + raw_data_value = (TextView) root.findViewById(R.id.raw_data_value); + raw_data_slope = (TextView) root.findViewById(R.id.raw_data_slope); + raw_data_timestamp = (TextView) root.findViewById(R.id.raw_data_timestamp); + } + } + + public static class CalibrationDataCursorAdapter extends CursorAdapter { + Calibration bgReading = new Calibration(); + + public CalibrationDataCursorAdapter(Context context, Cursor c, int flags) { + super(context, c, flags); + } + + @Override + public View newView(Context context, Cursor cursor, ViewGroup parent) { + final View view = LayoutInflater.from(context).inflate(R.layout.raw_data_list_item, parent, false); + + final CalibrationDataCursorAdapterViewHolder holder = new CalibrationDataCursorAdapterViewHolder(view); + view.setTag(holder); + + return view; + } + + @Override + public void bindView(View view, Context context, Cursor cursor) { + bgReading.loadFromCursor(cursor); + + final CalibrationDataCursorAdapterViewHolder tag = (CalibrationDataCursorAdapterViewHolder) view.getTag(); + tag.raw_data_id.setText(Double.toString(bgReading.bg)); + tag.raw_data_value.setText(Double.toString(bgReading.estimate_raw_at_time_of_calibration)); + tag.raw_data_slope.setText(Double.toString(bgReading.slope)); + tag.raw_data_timestamp.setText(Double.toString(bgReading.intercept)); + } + } } diff --git a/app/src/main/res/layout/raw_data_list_item.xml b/app/src/main/res/layout/raw_data_list_item.xml index fc539b65..8394291b 100644 --- a/app/src/main/res/layout/raw_data_list_item.xml +++ b/app/src/main/res/layout/raw_data_list_item.xml @@ -7,24 +7,24 @@ android:id="@+id/raw_data_list_item"> + android:textSize="24sp" /> + android:textSize="12sp"/> + android:textSize="12sp"/> + android:textSize="12sp"/> \ No newline at end of file