diff --git a/build.gradle b/build.gradle index 7c6b66b..da2846f 100644 --- a/build.gradle +++ b/build.gradle @@ -52,6 +52,7 @@ dependencies { // better version of Java's HttpURLConnection compile 'com.squareup.okhttp:okhttp:2.4.0' compile 'com.google.code.gson:gson:2.3.1' + compile 'com.j256.ormlite:ormlite-android:4.48' // TODO: requires special build of robolectric right now. working on this... testCompile('org.robolectric:robolectric:2.4') { diff --git a/src/main/java/nyc/c4q/Books.java b/src/main/java/nyc/c4q/Books.java new file mode 100644 index 0000000..1b06ff0 --- /dev/null +++ b/src/main/java/nyc/c4q/Books.java @@ -0,0 +1,87 @@ +package nyc.c4q; + +import com.j256.ormlite.field.DatabaseField; +import com.j256.ormlite.table.DatabaseTable; + +@DatabaseTable +public class Books { + @DatabaseField(generatedId = true) + private int id = 1; + @DatabaseField + private String title; + @DatabaseField + private String author; + @DatabaseField + private long isbn; + @DatabaseField + private long isbn13; + @DatabaseField + private String publisher; + @DatabaseField + private int publishyear; + + public Books(String title, String author, String isbn, String isbn13, String publisher, int publishyear) { + this.title = title; + this.author = author; + this.isbn = isbn; + this.isbn13 = isbn13; + this.publisher = publisher; + this.publishyear = publishyear; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public long getIsbn() { + return isbn; + } + + public void setIsbn(long isbn) { + this.isbn = isbn; + } + + public long getIsbn13() { + return isbn13; + } + + public void setIsbn13(long isbn13) { + this.isbn13 = isbn13; + } + + public String getPublisher() { + return publisher; + } + + public void setPublisher(String publisher) { + this.publisher = publisher; + } + + public int getPublishyear() { + return publishyear; + } + + public void setPublishyear(int publishyear) { + this.publishyear = publishyear; + } +} diff --git a/src/main/java/nyc/c4q/CustomAdapter.java b/src/main/java/nyc/c4q/CustomAdapter.java new file mode 100644 index 0000000..b83446c --- /dev/null +++ b/src/main/java/nyc/c4q/CustomAdapter.java @@ -0,0 +1,33 @@ +package nyc.c4q; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +/** + * Created on 8/30/15. + */ +class CustomAdapter extends ArrayAdapter { + + CustomAdapter(Context context, Person[] PEOPLE) { + super(context, R.layout.listitem_member, PEOPLE); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + LayoutInflater personInflater = LayoutInflater.from(getContext()); + View customView = personInflater.inflate(R.layout.listitem_member, parent, false); + + Person name = getItem(position); + TextView personName = (TextView) customView.findViewById(R.id.text_name); + Person house = getItem(position); + TextView personHouse = (TextView)customView.findViewById(R.id.text_house); + + personName.setText((CharSequence) name); + personHouse.setText((CharSequence) house); + return convertView; + } +} diff --git a/src/main/java/nyc/c4q/DatabaseHelper.java b/src/main/java/nyc/c4q/DatabaseHelper.java new file mode 100644 index 0000000..9543ab5 --- /dev/null +++ b/src/main/java/nyc/c4q/DatabaseHelper.java @@ -0,0 +1,81 @@ +package nyc.c4q; + +import android.content.Context; +import android.database.sqlite.SQLiteDatabase; +import android.os.Build; + +import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; +import com.j256.ormlite.dao.Dao; +import com.j256.ormlite.support.ConnectionSource; +import com.j256.ormlite.table.TableUtils; + +import java.sql.SQLException; +import java.util.List; + +public class DatabaseHelper extends OrmLiteSqliteOpenHelper { + + private static final String MYDB = "mydb.db"; + private static final int VERSION = 1; + private static DatabaseHelper mHelper; + + private DatabaseHelper getInstance(Context context) { + if (mHelper == null) { + mHelper = new DatabaseHelper(context.getApplicationContext()); + return mHelper; + } + return mHelper; + } + + public DatabaseHelper(Context context) { + super(context, MYDB, null, VERSION); + } + + @Override + public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) { + try { + TableUtils.createTable(connectionSource, Books.class); + TableUtils.createTable(connectionSource, Members.class); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) { + try { + TableUtils.dropTable(connectionSource, Books.class, true); + TableUtils.dropTable(connectionSource, Members.class, true); + onCreate(database, connectionSource); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + public void insertMemberRow(int id, String name, int dob_month, int dob_day, int dob_year, String city, String state) { + Members member = new Members( name, dob_month, dob_day, dob_year, city, state); + try { + getDao(Members.class).create(member); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public void insertBookRow(int id, String title, String author, String isbn, String isbn13, String publisher, int publishyear) { + Books book = new Books( title, author, isbn, isbn13, publisher, publishyear); + try { + getDao(Books.class).create(book); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public List loadBookData() throws SQLException { + List isbn = getDao(Books.class).queryForAll(); + return isbn; + } + + public List loadMemberData() throws SQLException { + List name = getDao(Members.class).queryForAll(); + return name; + } +} diff --git a/src/main/java/nyc/c4q/FragmentCalculator.java b/src/main/java/nyc/c4q/FragmentCalculator.java new file mode 100644 index 0000000..ef09607 --- /dev/null +++ b/src/main/java/nyc/c4q/FragmentCalculator.java @@ -0,0 +1,73 @@ +package nyc.c4q; + +import android.os.Bundle; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; +import android.widget.EditText; + +/** + * Created on 8/30/15. + */ +public class FragmentCalculator extends Fragment { + + EditText distance, time_min, time_sec, pace_min, pace_sec; + Button calculate; + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.fragment_pace_calculator, container, false); + + distance = (EditText)view.findViewById(R.id.input_distance); + time_min = (EditText)view.findViewById(R.id.input_time_min); + time_sec = (EditText)view.findViewById(R.id.input_time_sec); + pace_min = (EditText)view.findViewById(R.id.input_pace_min); + pace_sec = (EditText)view.findViewById(R.id.input_pace_sec); + calculate = (Button)view.findViewById(R.id.button_calculate); + + calculate.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + + double distan = Integer.parseInt(FragmentCalculator.this.distance.getText().toString()); + int numTimeMin = Integer.parseInt(time_min.getText().toString()); + int numTimeSec = (int) (Integer.parseInt(time_sec.getText().toString()) * 0.01); + int numPaceMin = Integer.parseInt(pace_min.getText().toString()); + int numPaceSec = (int) (Integer.parseInt(pace_sec.getText().toString()) * 0.01); + + double time = numTimeMin + numTimeSec; + double pace = numPaceMin + numPaceSec; + + if (time >1 && pace >1) { + FragmentCalculator.this.distance.setText(time / pace + ""); + } + else if (time >1 && distan>1){ + pace_min.setText((int)time/distan+""); + pace_sec.setText(((time/distan)-(int)(time/distan))+""); + } + else if (pace >1 && distan>1){ + time_min.setText((int)pace*distan+""); + time_sec.setText((pace*distan)-(int)pace*distan+""); + } + else if (time==0||pace==0||distan==0||time==1||pace==1||distan==1){ + distance.setText(""); + time_min.setText(""); + time_sec.setText(""); + pace_min.setText(""); + pace_sec.setText(""); + } + else { + distance.setText(""); + time_min.setText(""); + time_sec.setText(""); + pace_min.setText(""); + pace_sec.setText(""); + } + } + }); + + + return view; + } +} diff --git a/src/main/java/nyc/c4q/LibraryActivity.java b/src/main/java/nyc/c4q/LibraryActivity.java index ca2a050..7a15b87 100644 --- a/src/main/java/nyc/c4q/LibraryActivity.java +++ b/src/main/java/nyc/c4q/LibraryActivity.java @@ -1,14 +1,21 @@ package nyc.c4q; import android.app.Activity; +import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.widget.EditText; +import com.google.gson.JsonArray; + +import org.json.JSONException; +import org.json.JSONObject; + public class LibraryActivity extends Activity { public EditText inputParameter; + private DatabaseHelper mHelper; @Override protected void onCreate(Bundle savedInstanceState) { @@ -18,6 +25,38 @@ protected void onCreate(Bundle savedInstanceState) { inputParameter = (EditText) findViewById(R.id.input_parameter); } + private class MyDatabaseTask extends AsyncTask { + @Override + protected Void doInBackground(Void... params) { + mHelper = DatabaseHelper.getInstance(getApplicationContext()); + if (mHelper.fetchAllBooks() == null) { + JsonArray bookArray = new JsonArray(); + for (int i =0; i parent, View view, int position, long id) { + Toast.makeText(ListActivity.this, "click on listview", Toast.LENGTH_LONG).show(); + } + }); + + nameButton.setTag(1); + nameButton.setText(R.string.last_first); + nameButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + + final int status = (Integer) v.getTag(); + if (status == 1) { + nameButton.setText(R.string.first_last); + v.setTag(0); + // sort name by frist-last + } else { + nameButton.setText(R.string.last_first); + v.setTag(1); + //sort name by last-first + } + + + } + + }); + } } diff --git a/src/main/java/nyc/c4q/Members.java b/src/main/java/nyc/c4q/Members.java new file mode 100644 index 0000000..352e5cb --- /dev/null +++ b/src/main/java/nyc/c4q/Members.java @@ -0,0 +1,91 @@ +package nyc.c4q; + +import com.j256.ormlite.field.DatabaseField; +import com.j256.ormlite.table.DatabaseTable; + +@DatabaseTable +public class Members { + @DatabaseField(generatedId = true) + private int id = 0; + @DatabaseField + private String name; + @DatabaseField + private int dob_month; + @DatabaseField + private int dob_day; + @DatabaseField + private int dob_year; + @DatabaseField + private String city; + @DatabaseField + private String state; + + public Members() { + + } + + public Members(String name, int dob_month, int dob_day, int dob_year, String city, String state) { + this.name = name; + this.dob_month = dob_month; + this.dob_day = dob_day; + this.dob_year = dob_year; + this.city = city; + this.state = state; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getDob_month() { + return dob_month; + } + + public void setDob_month(int dob_month) { + this.dob_month = dob_month; + } + + public int getDob_day() { + return dob_day; + } + + public void setDob_day(int dob_day) { + this.dob_day = dob_day; + } + + public int getDob_year() { + return dob_year; + } + + public void setDob_year(int dob_year) { + this.dob_year = dob_year; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } +} diff --git a/src/main/java/nyc/c4q/PaceCalculatorActivity.java b/src/main/java/nyc/c4q/PaceCalculatorActivity.java index 5c0616e..34dc069 100644 --- a/src/main/java/nyc/c4q/PaceCalculatorActivity.java +++ b/src/main/java/nyc/c4q/PaceCalculatorActivity.java @@ -2,13 +2,19 @@ import android.support.v4.app.FragmentActivity; import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; public class PaceCalculatorActivity extends FragmentActivity { + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pace_calculator); + + FragmentCalculator fragmentCalculator = (FragmentCalculator) getSupportFragmentManager().findFragmentById(R.id.fragment_pace_calculator); } } diff --git a/src/main/res/layout/activity_pace_calculator.xml b/src/main/res/layout/activity_pace_calculator.xml index ed76b3e..1d0e022 100644 --- a/src/main/res/layout/activity_pace_calculator.xml +++ b/src/main/res/layout/activity_pace_calculator.xml @@ -4,5 +4,12 @@ android:id="@+id/activity_pace_calculator" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context="nyc.c4q.PaceCalculatorActivity" - /> + tools:context="nyc.c4q.PaceCalculatorActivity"> + + + + + + + +