class database
package adehodijah.example.com.pertemuan4e; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; /** * Created by AdeHodijah on 3/12/2018. */ public class DatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "dbIstilah5"; private static final int DATABASE_VERSION = 1; public static final String ISTILAH = "istilah"; public static final String ARTI = "arti"; public static final String KETERANGAN = "keterangan"; //Constructor DataKamus untuk initiate database public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { String sql = "CREATE TABLE tbIstilah(_id integer primary key, istilah text null, arti text null, keterangan text null);"; db.execSQL(sql); sql = "INSERT INTO tbIstilah (_id, istilah, arti, keterangan) VALUES ('1', 'run', 'rajin untuk nuhun', 'lauren');"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
class set get database
package adehodijah.example.com.pertemuan4e; /** * Created by AdeHodijah on 3/15/2018. */ public class KamusEntity { private String dtIstilah; private String dtArti; private String dtKeterangan; public KamusEntity(String dtIstilah, String dtArti, String dtKeterangan) { this.dtIstilah = dtIstilah; this.dtArti = dtArti; this.dtKeterangan = dtKeterangan; } public String getDtIstilah() { return dtIstilah; } public void setDtIstilah(String dtIstilah) { this.dtIstilah = dtIstilah; } public String getDtArti() { return dtArti; } public void setDtArti(String dtArti) { this.dtArti = dtArti; } public String getDtKeterangan() { return dtKeterangan; } public void setDtKeterangan(String dtKeterangan) { this.dtKeterangan = dtKeterangan; } }class adapterpackage adehodijah.example.com.pertemuan4e; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.List; /** * Created by AdeHodijah on 3/15/2018. */ public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private List<KamusEntity> mData; private LayoutInflater mInflater; // data is passed into the constructor MyRecyclerViewAdapter(Context context, List<KamusEntity> data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // inflates the row layout from xml when needed @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = mInflater.inflate(R.layout.rowistilah, parent, false); return new ViewHolder(view); } // binds the data to the TextView in each row @Override public void onBindViewHolder(ViewHolder holder, int position) { KamusEntity kamus = mData.get(position); holder.myTextIstilah.setText(kamus.getDtIstilah()); holder.myTextArti.setText(kamus.getDtArti()); holder.myTextKeterangan.setText(kamus.getDtKeterangan()); } // total number of rows @Override public int getItemCount() { return mData.size(); } // stores and recycles views as they are scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder { TextView myTextIstilah; TextView myTextArti; TextView myTextKeterangan; ViewHolder(View itemView) { super(itemView); myTextIstilah = (TextView) itemView.findViewById(R.id.istilah); myTextArti = (TextView) itemView.findViewById(R.id.arti); myTextKeterangan = (TextView) itemView.findViewById(R.id.keterangan); } } }layout untuk menampung objek database<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingBottom="5sp" android:paddingTop="5sp"> <TextView android:id="@+id/istilah" android:layout_width="100sp" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:text="ISTILAH" /> <TextView android:id="@+id/arti" android:layout_width="100sp" android:layout_height="wrap_content" android:layout_toRightOf="@+id/istilah" android:text="ARTI" /> <TextView android:id="@+id/keterangan" android:layout_width="100sp" android:layout_height="wrap_content" android:layout_toRightOf="@+id/arti" android:text="KETERANGAN" /> </RelativeLayout>layout main<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="#ecf0f1" > <Button android:id="@+id/buttonAdd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" style="?android:attr/borderlessButtonStyle" android:drawableLeft="@drawable/icon_add" android:text="Tambah Data Baru" /> <android.support.v7.widget.RecyclerView android:id="@+id/listData" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/buttonAdd" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout>class mainpackage adehodijah.example.com.pertemuan4e; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import java.util.ArrayList; public class ListActivity extends AppCompatActivity { private DatabaseHelper dbhelper; private SQLiteDatabase db = null; private Cursor kamusCursor = null; private MyRecyclerViewAdapter adapter; RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); dbhelper = new DatabaseHelper(this); db = dbhelper.getReadableDatabase(); ArrayList<KamusEntity> istilahArtiKeterangan = new ArrayList<>(); kamusCursor = db.rawQuery("SELECT _ID, ISTILAH, ARTI, KETERANGAN FROM tbIstilah ORDER BY ISTILAH", null); kamusCursor.moveToFirst(); for (int cc=0; cc < kamusCursor.getCount(); cc++){ kamusCursor.moveToPosition(cc); istilahArtiKeterangan.add(new KamusEntity(kamusCursor.getString(1),kamusCursor.getString(2),kamusCursor.getString(3))); } recyclerView = (RecyclerView) findViewById(R.id.listData); recyclerView.setLayoutManager(new LinearLayoutManager(this)); adapter = new MyRecyclerViewAdapter(this, istilahArtiKeterangan); recyclerView.setAdapter(adapter); } }menambahkan aksi klik pada adapterclass adapterpackage adehodijah.example.com.pertemuan4e; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.List; /** * Created by AdeHodijah on 3/15/2018. */ public class MyRecyclerViewClickAdapter extends RecyclerView.Adapter<MyRecyclerViewClickAdapter.ViewHolder> { private List<KamusEntity> mData; private LayoutInflater mInflater; private ItemClickListener mClickListener; // data is passed into the constructor MyRecyclerViewClickAdapter(Context context, List<KamusEntity> data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // inflates the row layout from xml when needed @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = mInflater.inflate(R.layout.rowistilah, parent, false); return new ViewHolder(view); } // binds the data to the TextView in each row @Override public void onBindViewHolder(ViewHolder holder, int position) { KamusEntity kamus = mData.get(position); holder.myTextIstilah.setText(kamus.getDtIstilah()); holder.myTextArti.setText(kamus.getDtArti()); holder.myTextKeterangan.setText(kamus.getDtKeterangan()); } // total number of rows @Override public int getItemCount() { return mData.size(); } // stores and recycles views as they are scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView myTextIstilah; TextView myTextArti; TextView myTextKeterangan; ViewHolder(View itemView) { super(itemView); myTextIstilah = (TextView) itemView.findViewById(R.id.istilah); myTextArti = (TextView) itemView.findViewById(R.id.arti); myTextKeterangan = (TextView) itemView.findViewById(R.id.keterangan); itemView.setOnClickListener(this);//event click } @Override public void onClick(View view) { if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition()); } } // convenience method for getting data at click position KamusEntity getItem(int id) { return mData.get(id); } // allows clicks events to be caught void setClickListener(ItemClickListener itemClickListener) { this.mClickListener = itemClickListener; } // parent activity will implement this method to respond to click events public interface ItemClickListener { void onItemClick(View view, int position); } }class mainpackage adehodijah.example.com.pertemuan4e; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Toast; import java.util.ArrayList; public class ListClickActivity extends AppCompatActivity implements MyRecyclerViewClickAdapter.ItemClickListener{ private DatabaseHelper dbhelper; private SQLiteDatabase db = null; private Cursor kamusCursor = null; private MyRecyclerViewClickAdapter adapter; RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); dbhelper = new DatabaseHelper(this); db = dbhelper.getReadableDatabase(); ArrayList<KamusEntity> istilahArtiKeterangan = new ArrayList<>(); kamusCursor = db.rawQuery("SELECT _ID, ISTILAH, ARTI, KETERANGAN FROM tbIstilah ORDER BY ISTILAH", null); kamusCursor.moveToFirst(); for (int cc=0; cc < kamusCursor.getCount(); cc++){ kamusCursor.moveToPosition(cc); istilahArtiKeterangan.add(new KamusEntity(kamusCursor.getString(1),kamusCursor.getString(2),kamusCursor.getString(3))); } recyclerView = (RecyclerView) findViewById(R.id.listData); recyclerView.setLayoutManager(new LinearLayoutManager(this)); adapter = new MyRecyclerViewClickAdapter(this, istilahArtiKeterangan); recyclerView.setAdapter(adapter); adapter.setClickListener(this); } @Override public void onItemClick(View view, int position) { Toast.makeText(this, "You clicked " + adapter.getItem(position).getDtIstilah() + " on row number " + position, Toast.LENGTH_SHORT).show(); } }melakukan refresh data pada adapter ketika merubah isi recyclerViewclass adapterpackage adehodijah.example.com.pertemuan4e; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.List; /** * Created by AdeHodijah on 3/15/2018. */ public class MyRecyclerViewClickRefreshAdapter extends RecyclerView.Adapter<MyRecyclerViewClickRefreshAdapter.ViewHolder> { private List<KamusEntity> mData; private LayoutInflater mInflater; private ItemClickListener mClickListener; public void refreshDataAdapter(List<KamusEntity> newData){ mData.clear(); mData = newData; } // data is passed into the constructor MyRecyclerViewClickRefreshAdapter(Context context, List<KamusEntity> data) { this.mInflater = LayoutInflater.from(context); this.mData = data; } // inflates the row layout from xml when needed @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = mInflater.inflate(R.layout.rowistilah, parent, false); return new ViewHolder(view); } // binds the data to the TextView in each row @Override public void onBindViewHolder(ViewHolder holder, int position) { KamusEntity kamus = mData.get(position); holder.myTextIstilah.setText(kamus.getDtIstilah()); holder.myTextArti.setText(kamus.getDtArti()); holder.myTextKeterangan.setText(kamus.getDtKeterangan()); } // total number of rows @Override public int getItemCount() { return mData.size(); } // stores and recycles views as they are scrolled off screen public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { TextView myTextIstilah; TextView myTextArti; TextView myTextKeterangan; ViewHolder(View itemView) { super(itemView); myTextIstilah = (TextView) itemView.findViewById(R.id.istilah); myTextArti = (TextView) itemView.findViewById(R.id.arti); myTextKeterangan = (TextView) itemView.findViewById(R.id.keterangan); itemView.setOnClickListener(this);//event click } @Override public void onClick(View view) { if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition()); } } // convenience method for getting data at click position KamusEntity getItem(int id) { return mData.get(id); } // allows clicks events to be caught void setClickListener(ItemClickListener itemClickListener) { this.mClickListener = itemClickListener; } // parent activity will implement this method to respond to click events public interface ItemClickListener { void onItemClick(View view, int position); } }class mainpackage adehodijah.example.com.pertemuan4e; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.util.ArrayList; public class ListClickRefreshActivity extends AppCompatActivity implements MyRecyclerViewClickRefreshAdapter.ItemClickListener{ private DatabaseHelper dbhelper; private SQLiteDatabase db = null; private Cursor kamusCursor = null; private MyRecyclerViewClickRefreshAdapter adapter; RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); dbhelper = new DatabaseHelper(this); db = dbhelper.getReadableDatabase(); showData(); Button btn=(Button)findViewById(R.id.buttonAdd); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent inte = new Intent(ListClickRefreshActivity.this, AddActivity.class); startActivity(inte); } }); } public void showData(){ ArrayList<KamusEntity> istilahArtiKeterangan = new ArrayList<>(); kamusCursor = db.rawQuery("SELECT _ID, ISTILAH, ARTI, KETERANGAN FROM tbIstilah ORDER BY ISTILAH", null); kamusCursor.moveToFirst(); for (int cc=0; cc < kamusCursor.getCount(); cc++){ kamusCursor.moveToPosition(cc); istilahArtiKeterangan.add(new KamusEntity(kamusCursor.getString(1),kamusCursor.getString(2),kamusCursor.getString(3))); } recyclerView = (RecyclerView) findViewById(R.id.listData); recyclerView.setLayoutManager(new LinearLayoutManager(this)); adapter = new MyRecyclerViewClickRefreshAdapter(this, istilahArtiKeterangan); recyclerView.setAdapter(adapter); adapter.setClickListener(this); } @Override public void onItemClick(View view, int position) { Toast.makeText(this, "You clicked " + adapter.getItem(position).getDtIstilah() + " on row number " + position, Toast.LENGTH_SHORT).show(); } @Override protected void onResume() { super.onResume(); refreshData(); } public void refreshData(){ ArrayList<KamusEntity> istilahArtiKeterangan = new ArrayList<>(); kamusCursor = db.rawQuery("SELECT _ID, ISTILAH, ARTI, KETERANGAN FROM tbIstilah ORDER BY ISTILAH", null); kamusCursor.moveToFirst(); for (int cc=0; cc < kamusCursor.getCount(); cc++){ kamusCursor.moveToPosition(cc); istilahArtiKeterangan.add(new KamusEntity(kamusCursor.getString(1),kamusCursor.getString(2),kamusCursor.getString(3))); } adapter.refreshDataAdapter(istilahArtiKeterangan); adapter.notifyDataSetChanged(); } }Activity untuk menambah datalayout AddActivity<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Istilah:" > </TextView> <EditText android:id="@+id/txtEditIstilah" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" > </EditText> <TextView android:id="@+id/TextView03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Arti:" > </TextView> <EditText android:id="@+id/txtEditArti" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="" > </EditText> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Keterangan:" > </TextView> <EditText android:id="@+id/txtEditKeterangan" android:layout_width="fill_parent" android:layout_height="wrap_content" > </EditText> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btnTranslate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Save" > </Button> <Button android:id="@+id/btnBack" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Back" > </Button> </LinearLayout> </LinearLayout>class AddActivitypackage adehodijah.example.com.pertemuan4e; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class AddActivity extends AppCompatActivity { protected Cursor cursor; DatabaseHelper dbHelper; Button button1, button2; EditText text1, text2, text3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add); dbHelper = new DatabaseHelper(this); text1 = (EditText) findViewById(R.id.txtEditIstilah); text2 = (EditText) findViewById(R.id.txtEditArti); text3 = (EditText) findViewById(R.id.txtEditKeterangan); button1 = (Button) findViewById(R.id.btnTranslate); button2 = (Button) findViewById(R.id.btnBack); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub SQLiteDatabase db = dbHelper.getWritableDatabase(); db.execSQL("INSERT INTO tbIstilah (istilah, arti, keterangan) VALUES('"+text1.getText().toString()+"','"+text2.getText().toString()+"','" +text3.getText().toString()+"' )"); Toast.makeText(getApplicationContext(), "Berhasil", Toast.LENGTH_LONG).show(); finish(); } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub finish(); } }); } }
Komentar
Posting Komentar