Skip to content

Commit

Permalink
Using custom adapters for the table activities.
Browse files Browse the repository at this point in the history
  • Loading branch information
bhandfast committed Mar 1, 2015
1 parent dec1295 commit 6b457e4
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -43,16 +48,60 @@ 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);
// listView.setAdapter(adapter);
}


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));
}
}
}
12 changes: 6 additions & 6 deletions app/src/main/res/layout/raw_data_list_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
android:id="@+id/raw_data_list_item">

<TextView android:id="@+id/raw_data_id"
android:layout_width="132dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24dp" />
android:textSize="24sp" />

<TextView android:id="@+id/raw_data_value"
android:layout_width="87dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12dp"/>
android:textSize="12sp"/>

<TextView android:id="@+id/raw_data_slope"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12dp"/>
android:textSize="12sp"/>

<TextView android:id="@+id/raw_data_timestamp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12dp"/>
android:textSize="12sp"/>


</LinearLayout>

0 comments on commit 6b457e4

Please sign in to comment.