build.gradle (Project: MyApplication)
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 |
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.1.4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() maven { url 'https://jitpack.io' } } } task clean(type: Delete) { delete rootProject.buildDir } |
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:25.3.1' implementation 'com.github.vansikrishna:Multimager:1.0.6' } |
drawable/border.xml
1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:width="1dp" android:color="#777" /> </shape> |
values/styles.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 |
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style> <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> <style name="TextStyle"> <item name="android:textSize">16sp</item> <item name="android:layout_marginLeft">5dp</item> </style> <style name="EditStyle"> <item name="android:padding">3dp</item> <item name="android:textSize">16sp</item> <item name="android:layout_width">match_parent</item> <item name="android:layout_margin">5dp</item> <item name="android:background">@drawable/border</item> <item name="android:lines">1</item> <item name="android:gravity">top</item> </style> </resources> |
activity_main.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 |
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout 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" tools:context=".MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" app:srcCompat="@drawable/ic_add" /> </android.support.design.widget.CoordinatorLayout> |
content_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?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:gravity="center_horizontal" android:paddingTop="10dp" 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="wrap_content" android:layout_height="wrap_content"> </android.support.v7.widget.RecyclerView> </LinearLayout> |
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 25 26 27 28 29 30 31 32 33 34 35 |
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="10dp" app:cardBackgroundColor="#fafafa" app:cardCornerRadius="5dp" app:cardElevation="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="?android:attr/selectableItemBackground" android:orientation="vertical" android:padding="15dp"> <ImageView android:id="@+id/image_view" android:layout_width="100dp" android:layout_height="50dp" android:layout_gravity="center_horizontal" /> <TextView android:id="@+id/text_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="5dp" android:textSize="18sp" /> </LinearLayout> </android.support.v7.widget.CardView> |
activity_add_product.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 |
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout 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" tools:context=".AddProductActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_add_product" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" app:srcCompat="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout> |
content_add_product.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 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 |
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.NestedScrollView 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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".AddProductActivity" tools:showIn="@layout/activity_add_product"> <GridLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <ImageView android:id="@+id/image_view" android:layout_width="400dp" android:layout_height="200dp" android:scaleType="centerCrop" android:visibility="gone" /> <Button android:id="@+id/button_image" android:text="เลือกภาพ" /> <TextView style="@style/TextStyle" android:text="ชื่อสินค้า" /> <EditText android:id="@+id/edit_name" style="@style/EditStyle" /> <TextView style="@style/TextStyle" android:text="ราคา" /> <EditText android:id="@+id/edit_price" style="@style/EditStyle" android:inputType="number" /> <TextView style="@style/TextStyle" android:text="ลักษณะสินค้า (แยกเป็นข้อๆ)" /> <EditText android:id="@+id/edit_char1" style="@style/EditStyle" /> <EditText android:id="@+id/edit_char2" style="@style/EditStyle" /> <EditText android:id="@+id/edit_char3" style="@style/EditStyle" /> <EditText android:id="@+id/edit_char4" style="@style/EditStyle" /> <EditText android:id="@+id/edit_char5" style="@style/EditStyle" /> <Button android:id="@+id/button_save" android:text="บันทึกข้อมูล" /> </GridLayout> </android.support.v4.widget.NestedScrollView> |
activity_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 |
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout 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" tools:context=".ProductDetailActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_product_detail" /> <!-- <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" app:srcCompat="@android:drawable/ic_dialog_email"/> --> </android.support.design.widget.CoordinatorLayout> |
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 41 42 43 44 45 46 47 48 49 |
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.NestedScrollView 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:padding="16dp" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".ProductDetailActivity" tools:showIn="@layout/activity_product_detail"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <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_marginBottom="10dp" android:layout_marginTop="10dp" android:paddingLeft="30dp" android:textColor="#00f" android:textSize="20sp" /> <WebView android:id="@+id/web_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:paddingLeft="30dp" android:textColor="#090" android:textSize="16sp" /> </LinearLayout> </android.support.v4.widget.NestedScrollView> |
AddProductActivity.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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
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.content.ContentValues; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.net.Uri; import android.provider.MediaStore; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import com.vlk.multimager.activities.GalleryActivity; import com.vlk.multimager.utils.Constants; import com.vlk.multimager.utils.Image; import com.vlk.multimager.utils.Params; import java.io.ByteArrayOutputStream; import java.util.ArrayList; public class AddProductActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_product); 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(); Button btSelectImg = (Button) findViewById(R.id.button_image); btSelectImg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //ส่งอินเทนต์เพื่อเลือกภาพ Intent intent = new Intent(AddProductActivity.this, GalleryActivity.class); Params params = new Params(); params.setPickerLimit(1); //เลือกได้ 1 ภาพ intent.putExtra(Constants.KEY_PARAMS, params); startActivityForResult(intent, Constants.TYPE_MULTI_PICKER); } }); Button buttonSave = (Button) findViewById(R.id.button_save); buttonSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { saveData(); } }); } private void showGallery() { Intent intent = new Intent(Intent.ACTION_PICK); Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; intent.setData(uri); startActivityForResult(intent, 999); } //หลังเลือกภาพเสร็จ ให้นำภาพนั้นไปแสดงใน ImageView ให้เห็นด้วย @Override public void onActivityResult(int rqCode, int rsCode, Intent intent) { if (rqCode != Constants.TYPE_MULTI_PICKER && rsCode != RESULT_OK) { return; } ArrayList<Image> images = intent.getParcelableArrayListExtra( Constants.KEY_BUNDLE_LIST); Uri uri = images.get(0).uri; ImageView imageView = (ImageView) findViewById(R.id.image_view); imageView.setImageURI(uri); imageView.setVisibility(View.VISIBLE); } //เมธอดนี้จะถูกเรียกเมื่อคลิกปุ่มบันทึกข้อมูล private void saveData() { ImageView imageView = (ImageView) findViewById(R.id.image_view); if (imageView.getDrawable() == null) { showToast("ต้องมีรูปภาพ"); return; } EditText editName = (EditText) findViewById(R.id.edit_name); if (editName.getText().toString().trim().equals("")) { showToast("ต้องกำหนดชื่อ"); return; } SQLiteHelper sqLiteHelper = SQLiteHelper.getInstance(this); SQLiteDatabase db = sqLiteHelper.getWritableDatabase(); EditText editPrice = (EditText) findViewById(R.id.edit_price); String name = editName.getText().toString(); String price = editPrice.getText().toString(); ContentValues cv = new ContentValues(); cv.put("pro_name", name); cv.put("price", price); long pro_id = db.insert("product", null, cv); int[] edits = {R.id.edit_char1, R.id.edit_char2, R.id.edit_char3, R.id.edit_char4, R.id.edit_char5}; for (int i = 0; i < edits.length; i++) { String ch = ((EditText) findViewById(edits[i])).getText().toString(); if (ch.trim().equals("")) { continue; } cv = new ContentValues(); cv.put("pro_id", pro_id); cv.put("pro_characteristic", ch); db.insert("characteristic", null, cv); } BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable(); byte[] imgContent = getBytes(bitmapDrawable.getBitmap()); cv = new ContentValues(); cv.put("pro_id", pro_id); cv.put("pro_image", imgContent); db.insert("image", null, cv); showToast("บันทึกข้อมูลเสร็จแล้ว"); } public byte[] getBytes(Bitmap bitmap) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream); return stream.toByteArray(); } private void showToast(String msg) { Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show(); } } |
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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.webkit.WebView; import android.widget.ImageView; import android.widget.TextView; import java.text.NumberFormat; 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(); } }); */ } @Override public void onStart() { super.onStart(); Intent intent = getIntent(); String _id = intent.getStringExtra("_id"); SQLiteHelper sqLiteHelper = SQLiteHelper.getInstance(this); SQLiteDatabase db = sqLiteHelper.getReadableDatabase(); String sql = "SELECT product.*, image.pro_image FROM product " + "LEFT JOIN image " + "ON product._id = image.pro_id " + "WHERE product._id = " + _id; Cursor cursor = db.rawQuery(sql, null); ImageView imageView = (ImageView) findViewById(R.id.image_view); TextView textName = (TextView) findViewById(R.id.text_name); TextView textPrice = (TextView) findViewById(R.id.text_price); WebView webView = (WebView) findViewById(R.id.web_view); if (cursor.moveToNext()) { imageView.setImageBitmap(getImage(cursor.getBlob(3))); textName.setText(cursor.getString(1)); NumberFormat numFormat = NumberFormat.getInstance(); //่java.text.NumberFormat int price = cursor.getInt(2); textPrice.setText("ราคา: " + numFormat.format(price) + " บาท"); } cursor.close(); sql = "SELECT pro_characteristic FROM characteristic WHERE pro_id = " + _id; cursor = db.rawQuery(sql, null); String html = "<ul>"; while (cursor.moveToNext()) { html += "<li>" + cursor.getString(0) + "</li>"; } html += "</ul>"; webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null); cursor.close(); } public Bitmap getImage(byte[] image) { return BitmapFactory.decodeByteArray(image, 0, image.length); } @Override public void onBackPressed() { finish(); } } |
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
package com.phaisarn.myapplication; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; 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.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; 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(); */ startActivity(new Intent(MainActivity.this, AddProductActivity.class)); } }); } @Override public void onStart() { super.onStart(); SQLiteHelper sqLiteHelper = SQLiteHelper.getInstance(this); SQLiteDatabase db = sqLiteHelper.getReadableDatabase(); String sql = "SELECT product.*, image.pro_image FROM product " + "LEFT JOIN image " + "ON product._id = image.pro_id"; Cursor cursor = db.rawQuery(sql, null); ArrayList<RowItem> itemArray = new ArrayList<>(); RowItem item; while (cursor.moveToNext()) { item = new RowItem(); item._id = cursor.getInt(0); item.name = cursor.getString(1); item.image = cursor.getBlob(3); itemArray.add(item); } RecyclerView rcv = (RecyclerView) findViewById(R.id.recycler_view); CustomAdapter adapter = new CustomAdapter(this, itemArray); rcv.setAdapter(adapter); //RecyclerView.ItemDecoration itemDecor = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL); //rcv.addItemDecoration(itemDecor); //LinearLayoutManager layout = new LinearLayoutManager(this, RecyclerView.VERTICAL, false); GridLayoutManager layout = new GridLayoutManager(this, 2); rcv.setLayoutManager(layout); //cursor.close(); } @Override public void onBackPressed() { finishAffinity(); System.exit(0); /* finish(); android.os.Process.killProcess(android.os.Process.myPid()); super.onDestroy(); */ /* Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_HOME); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); */ } @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); } } |
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.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import java.util.ArrayList; public class CustomAdapter extends RecyclerView.Adapter<CustomHolder> { private ArrayList<RowItem> mItems; private Context mContext; public CustomAdapter(Context context, ArrayList<RowItem> 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) { RowItem item = mItems.get(pos); Intent intent = new Intent(mContext, ProductDetailActivity.class); intent.putExtra("_id", String.valueOf(item._id)); //ต้องกำหนดเป็นชนิด String mContext.startActivity(intent); } } }); return vHolder; } @Override public void onBindViewHolder(CustomHolder vHolder, int position) { RowItem item = mItems.get(position); vHolder.imageView.setImageBitmap(getImage(item.image)); vHolder.textView.setText(item.name); } public Bitmap getImage(byte[] image) { return BitmapFactory.decodeByteArray(image, 0, image.length); } } |
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 view) { super(view); imageView = (ImageView) view.findViewById(R.id.image_view); textView = (TextView) view.findViewById(R.id.text_name); } } |
RowItem.java
1 2 3 4 5 6 7 |
package com.phaisarn.myapplication; public class RowItem { public int _id; public String name; public byte[] image; } |
SQLiteHelper.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 |
package com.phaisarn.myapplication; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class SQLiteHelper extends SQLiteOpenHelper { private static SQLiteHelper sqLiteDB; private static final String DB_NAME = "db_product"; private static final int DB_VERSION = 1; private SQLiteHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } public static synchronized SQLiteHelper getInstance(Context context) { if (sqLiteDB == null) { sqLiteDB = new SQLiteHelper(context.getApplicationContext()); } return sqLiteDB; } @Override public void onCreate(SQLiteDatabase db) { String sql = "CREATE TABLE IF NOT EXISTS product(" + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "pro_name TEXT, " + "price INTEGER)"; db.execSQL(sql); sql = "CREATE TABLE IF NOT EXISTS characteristic(" + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "pro_id INTEGER, " + "pro_characteristic TEXT)"; db.execSQL(sql); sql = "CREATE TABLE IF NOT EXISTS image(" + "pro_id INTEGER, " + "pro_image BLOB)"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldV, int newV) { } @Override public void onDowngrade(SQLiteDatabase db, int oldV, int newV) { } } |
รูปที่ใช้