diff --git a/app/src/main/java/com/example/leo/fitnessdiy/Adapter/RecyclerViewAdapter.java b/app/src/main/java/com/example/leo/fitnessdiy/Adapter/RecyclerViewAdapter.java new file mode 100644 index 0000000000000000000000000000000000000000..d6eaf16c7a2703c7bd0af5c7bcbd550093e55a21 --- /dev/null +++ b/app/src/main/java/com/example/leo/fitnessdiy/Adapter/RecyclerViewAdapter.java @@ -0,0 +1,69 @@ +package com.example.leo.fitnessdiy.Adapter; + +import android.support.v7.widget.CardView; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import com.example.leo.fitnessdiy.R; +import com.example.leo.fitnessdiy.model.History; + +import java.util.List; + +/** + * Created by Leo on 16/02/2018. + */ + +public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.HistoryViewHolder> { + private List<History> histories; + + public RecyclerViewAdapter(List<History> h) { + this.histories = h; + } + @Override + public HistoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View view = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.card, parent, false); + + HistoryViewHolder hvh = new HistoryViewHolder(view); + + return hvh; + } + + @Override + public void onBindViewHolder(HistoryViewHolder holder, int position) { + holder.sportName.setText(histories.get(position).getSport_name()); + holder.sportDate.setText(histories.get(position).getSport_date()); + holder.sportStart.setText(histories.get(position).getSport_start()); + holder.sportEnd.setText(histories.get(position).getSport_end()); + } + + @Override + public int getItemCount() { + return histories.size(); + } + + @Override + public void onAttachedToRecyclerView(RecyclerView recyclerView) { + super.onAttachedToRecyclerView(recyclerView); + } + + public static class HistoryViewHolder extends RecyclerView.ViewHolder { + TextView sportName; + TextView sportDate; + TextView sportStart; + TextView sportEnd; + + public HistoryViewHolder(View itemView) { + super(itemView); + + sportName = (TextView) itemView.findViewById(R.id.sport_name); + sportDate = (TextView) itemView.findViewById(R.id.sport_date); + sportStart = (TextView) itemView.findViewById(R.id.sport_start); + sportEnd = (TextView) itemView.findViewById(R.id.sport_end); + } + } + +} diff --git a/app/src/main/java/com/example/leo/fitnessdiy/HIstoryActivity.java b/app/src/main/java/com/example/leo/fitnessdiy/HIstoryActivity.java index 8f0bf66a2e1ae7395fa0ac53f57e053e8ff965f2..9ef37d320f6f5f7f2d8afc43866f198b81957b47 100644 --- a/app/src/main/java/com/example/leo/fitnessdiy/HIstoryActivity.java +++ b/app/src/main/java/com/example/leo/fitnessdiy/HIstoryActivity.java @@ -4,32 +4,68 @@ package com.example.leo.fitnessdiy; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; import android.widget.TextView; +import com.example.leo.fitnessdiy.Adapter.RecyclerViewAdapter; import com.example.leo.fitnessdiy.Utilities.NetworkUtils; +import com.example.leo.fitnessdiy.model.History; import com.example.leo.fitnessdiy.routes.api; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + import java.io.IOException; import java.net.URL; +import java.util.ArrayList; +import java.util.List; public class HIstoryActivity extends AppCompatActivity { public static final String TAG = HIstoryActivity.class.getSimpleName(); - private TextView mHasil; + private RecyclerView mRecyclerView; + private List<History> histories; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_history); - mHasil = (TextView) findViewById(R.id.hasil); - String githubSearchResults = null; + String fetchResults = null; +// try { +// githubSearchResults = NetworkUtils.getResponseFromHttpUrl(new URL(api.HISTORY_URL + "1")); + fetchResults = "[{\"id\":\"1\",\"id_user\":\"1\",\"sport_name\":\"jogging\",\"sport_date\":\"2018-02-14\",\"sport_time_start\":\"09:00:00\",\"sport_time_end\":\"11:00:00\",\"created_at\":null,\"updated_at\":null},{\"id\":\"2\",\"id_user\":\"1\",\"sport_name\":\"jogging\",\"sport_date\":\"2018-02-15\",\"sport_time_start\":\"09:00:00\",\"sport_time_end\":\"11:00:00\",\"created_at\":null,\"updated_at\":null},{\"id\":\"3\",\"id_user\":\"1\",\"sport_name\":\"plank\",\"sport_date\":\"2018-02-16\",\"sport_time_start\":\"09:00:00\",\"sport_time_end\":\"09:45:00\",\"created_at\":null,\"updated_at\":null},{\"id\":\"4\",\"id_user\":\"1\",\"sport_name\":\"push-up\",\"sport_date\":\"2018-02-16\",\"sport_time_start\":\"10:00:00\",\"sport_time_end\":\"10:30:00\",\"created_at\":null,\"updated_at\":null}]"; + initializeData(fetchResults); +// } catch (IOException e) { +// e.printStackTrace(); +// } + + mRecyclerView = (RecyclerView) findViewById(R.id.rv_history); + + LinearLayoutManager layoutManager = new LinearLayoutManager(this); + + mRecyclerView.setLayoutManager(layoutManager); + mRecyclerView.setHasFixedSize(true); + RecyclerViewAdapter rv = new RecyclerViewAdapter(histories); + mRecyclerView.setAdapter(rv); + } + + public void initializeData(String data) { + this.histories = new ArrayList<>(); try { - githubSearchResults = NetworkUtils.getResponseFromHttpUrl(new URL(api.HISTORY_URL + "1")); - mHasil.setText(githubSearchResults); - } catch (IOException e) { + JSONArray parser = new JSONArray(data); + for (int i = 0; i < parser.length(); i++) { + JSONObject json = parser.getJSONObject(i); + histories.add(new History(json.getString("sport_name"), + json.getString("sport_date"), + json.getString("sport_time_start"), + json.getString("sport_time_end"))); + } + } catch (JSONException e) { e.printStackTrace(); } } diff --git a/app/src/main/java/com/example/leo/fitnessdiy/model/History.java b/app/src/main/java/com/example/leo/fitnessdiy/model/History.java new file mode 100644 index 0000000000000000000000000000000000000000..647708c296ccabe7a79cc85c55795fffbef80c46 --- /dev/null +++ b/app/src/main/java/com/example/leo/fitnessdiy/model/History.java @@ -0,0 +1,51 @@ +package com.example.leo.fitnessdiy.model; + +/** + * Created by Leo on 16/02/2018. + */ + +public class History { + private String sport_name; + private String sport_date; + private String sport_start; + private String sport_end; + + public History(String name, String date, String start, String end) { + this.sport_name = name; + this.sport_date = date; + this.sport_start = start; + this.sport_end = end; + } + + public String getSport_name() { + return sport_name; + } + + public void setSport_name(String sport_name) { + this.sport_name = sport_name; + } + + public String getSport_date() { + return sport_date; + } + + public void setSport_date(String sport_date) { + this.sport_date = sport_date; + } + + public String getSport_start() { + return sport_start; + } + + public void setSport_start(String sport_start) { + this.sport_start = sport_start; + } + + public String getSport_end() { + return sport_end; + } + + public void setSport_end(String sport_end) { + this.sport_end = sport_end; + } +} diff --git a/app/src/main/res/layout/activity_history.xml b/app/src/main/res/layout/activity_history.xml index 2f8a9aa3948668b9c4d28f39446270a9cc473b9e..b982d3c6ef1f2bdae574d6de7f81da931d124b5d 100644 --- a/app/src/main/res/layout/activity_history.xml +++ b/app/src/main/res/layout/activity_history.xml @@ -1,15 +1,16 @@ <?xml version="1.0" encoding="utf-8"?> -<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:card_view="http://schemas.android.com/tools" - tools:context="com.example.leo.fitnessdiy.HIstoryActivity"> + tools:context="com.example.leo.fitnessdiy.HIstoryActivity" + android:orientation="vertical"> - <TextView + <android.support.v7.widget.RecyclerView android:layout_width="match_parent" - android:layout_height="wrap_content" - android:id="@+id/hasil"/> + android:layout_height="match_parent" + android:id="@+id/rv_history"/> -</RelativeLayout> +</LinearLayout> diff --git a/app/src/main/res/layout/card.xml b/app/src/main/res/layout/card.xml index 9e862793b660b20e3a9af5bb5daf0b38535a264e..b2f4f92a437f9a0ac903224f1e5fe1c8d839212e 100644 --- a/app/src/main/res/layout/card.xml +++ b/app/src/main/res/layout/card.xml @@ -1,50 +1,33 @@ <?xml version="1.0" encoding="utf-8"?> -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" - android:layout_height="wrap_content"> + android:layout_height="wrap_content" + android:orientation="vertical" + android:padding="16dp"> - <android.support.v7.widget.CardView - android:id="@+id/cardview" - android:layout_width="match_parent" - android:layout_height="match_parent" - android:layout_gravity="center" - android:elevation="4dp" - card_view:cardUseCompatPadding="true" - card_view:cardElevation="5dp" - card_view:cardCornerRadius="1dp" - > + <TextView + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:id="@+id/sport_name"/> - <RelativeLayout + <TextView android:layout_width="match_parent" - android:layout_height="match_parent"> + android:layout_height="wrap_content" + android:id="@+id/sport_date" + android:layout_below="@id/sport_name"/> - <TextView - android:id="@+id/nama_olahraga" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:paddingLeft="5dp" - android:paddingRight="5dp" - android:paddingTop="5dp" - android:textColor="@color/cardview_dark_background" - android:textSize="12sp" - android:textStyle="normal" - android:typeface="sans" /> + <TextView + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:id="@+id/sport_start" + android:layout_below="@id/sport_date"/> - <TextView - android:id="@+id/tanggal_olahraga" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:layout_below="@id/nama_olahraga" - android:paddingLeft="5dp" - android:paddingRight="5dp" - android:paddingTop="5dp" - android:textColor="@color/cardview_dark_background" - android:textSize="12sp" - android:textStyle="normal" - android:typeface="sans" /> - </RelativeLayout> + <TextView + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:id="@+id/sport_end" + android:layout_below="@id/sport_start"/> - </android.support.v7.widget.CardView> -</LinearLayout> \ No newline at end of file +</RelativeLayout> \ No newline at end of file