build.gradle (Module: app)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
apply plugin: 'com.android.application' android { compileSdkVersion 26 defaultConfig { applicationId "com.phaisarn.myapplication" minSdkVersion 21 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:design:26.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.android.support:cardview-v7:26.1.0' implementation 'com.koushikdutta.ion:ion:2.+' } |
content_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version="1.0" encoding="utf-8"?> <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" android:background="#def" android:padding="16dp" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" tools:showIn="@layout/activity_main"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> </LinearLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.app.ProgressDialog; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.koushikdutta.async.future.FutureCallback; import com.koushikdutta.ion.Ion; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); fab.hide(); final ProgressDialog dialog = new ProgressDialog(MainActivity.this); dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); dialog.setMessage("Please Wait..."); dialog.setIndeterminate(true); dialog.show(); final ArrayList<CustomItem> itemArray = new ArrayList<>(); final String baseUrl = "http://10.0.2.2/pro-android/server-side-db/"; Ion.with(this) .load(baseUrl + "product-list.php") .asJsonArray() .setCallback(new FutureCallback<JsonArray>() { @Override public void onCompleted(Exception e, JsonArray result) { dialog.dismiss(); JsonObject jsObject; for (int i = 0; i < result.size(); i++) { CustomItem item = new CustomItem(); jsObject = (JsonObject) result.get(i); item.id = jsObject.get("id").getAsInt(); item.name = jsObject.get("name").getAsString(); item.image = jsObject.get("image").getAsString(); item.baseUrl = baseUrl; itemArray.add(item); } RecyclerView rcv = (RecyclerView) findViewById(R.id.recycler_view); CustomAdapter adapter = new CustomAdapter(MainActivity.this, itemArray); rcv.setAdapter(adapter); LinearLayoutManager layout = new LinearLayoutManager(MainActivity.this); rcv.setLayoutManager(layout); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } |
บรรทัดที่ 47 : http://10.0.2.2/pro-android/server-side-db/
บรรทัดที่ 50 : product-list.php
content_product_detail.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?xml version="1.0" encoding="utf-8"?> <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" android:background="#fff" android:orientation="vertical" android:padding="16dp" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".ProductDetailActivity" tools:showIn="@layout/activity_product_detail"> <ImageView android:id="@+id/image_view" android:layout_width="400dp" android:layout_height="200dp" android:layout_gravity="center_horizontal" android:scaleType="centerCrop" /> <TextView android:id="@+id/text_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginBottom="10dp" android:layout_marginTop="10dp" android:textColor="#00f" android:textSize="24sp" /> <TextView android:id="@+id/text_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" android:textColor="#090" android:textSize="18sp" /> </LinearLayout> |
ProductDetailActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.app.ProgressDialog; import android.content.Intent; import android.widget.ImageView; import android.widget.TextView; import com.google.gson.JsonObject; import com.koushikdutta.async.future.FutureCallback; import com.koushikdutta.ion.Ion; public class ProductDetailActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_product_detail); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); fab.hide(); Intent intent = getIntent(); String id = intent.getStringExtra("id"); final ProgressDialog dialog = new ProgressDialog(ProductDetailActivity.this); dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); dialog.setMessage("Please Wait..."); dialog.setIndeterminate(true); dialog.show(); final String baseUrl = "http://10.0.2.2/pro-android/server-side-db/"; Ion.with(this) .load(baseUrl + "product-detail.php") .setBodyParameter("id", id) .asJsonObject() .setCallback(new FutureCallback<JsonObject>() { @Override public void onCompleted(Exception e, JsonObject result) { dialog.dismiss(); ImageView imageView = (ImageView) findViewById(R.id.image_view); String imgFile = result.get("image").getAsString(); Ion.with(ProductDetailActivity.this) .load(baseUrl + "images/" + imgFile) .intoImageView(imageView); TextView textName = (TextView) findViewById(R.id.text_name); textName.setText(result.get("name").getAsString()); TextView textPrice = (TextView) findViewById(R.id.text_price); textPrice.setText("ราคา " + result.get("price").getAsString() + " บาท"); } }); } } |
layout/item_layout.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="3dp" android:background="#fff" android:columnCount="2"> <ImageView android:id="@+id/image_view" android:layout_width="100dp" android:layout_height="50dp" android:layout_margin="8dp" /> <TextView android:id="@+id/text_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_columnWeight="1" android:layout_gravity="center_vertical|fill_horizontal" android:layout_marginLeft="10dp" android:textSize="20sp" /> </GridLayout> |
CustomAdapter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
package com.phaisarn.myapplication; import android.content.Context; import android.content.Intent; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.koushikdutta.ion.Ion; import java.util.ArrayList; public class CustomAdapter extends RecyclerView.Adapter<CustomHolder> { private ArrayList<CustomItem> mItems; private Context mContext; public CustomAdapter(Context context, ArrayList<CustomItem> items) { mContext = context; mItems = items; } @Override public int getItemCount() { return mItems.size(); } @Override public CustomHolder onCreateViewHolder(ViewGroup vg, int type) { final LayoutInflater inflater = LayoutInflater.from(mContext); View view = inflater.inflate(R.layout.item_layout, vg, false); final CustomHolder vHolder = new CustomHolder(view); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int pos = vHolder.getAdapterPosition(); if (pos != RecyclerView.NO_POSITION) { CustomItem item = mItems.get(pos); Intent intent = new Intent(mContext, ProductDetailActivity.class); intent.putExtra("id", String.valueOf(item.id)); mContext.startActivity(intent); } } }); return vHolder; } @Override public void onBindViewHolder(CustomHolder vHolder, int position) { CustomItem item = mItems.get(position); String img = item.image; String baseUrl = item.baseUrl; Ion.with(mContext) .load(baseUrl + "/images/" + img) .intoImageView(vHolder.imageView); vHolder.textView.setText(item.name); } } |
CustomHolder.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.phaisarn.myapplication; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.ImageView; import android.widget.TextView; public class CustomHolder extends RecyclerView.ViewHolder { public ImageView imageView; public TextView textView; public CustomHolder(View v) { super(v); imageView = (ImageView) v.findViewById(R.id.image_view); textView = (TextView) v.findViewById(R.id.text_name); } } |
CustomItem.java
1 2 3 4 5 6 7 8 |
package com.phaisarn.myapplication; public class CustomItem { public int id; public String name; public String image; public String baseUrl; } |