SQL Lite Create, Delete
SQL Lite Create, Delete
11600
WordListSQLIneractive
Hasil Program :
Code Program :
AndroidManifest.java
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/r
es/android"
package="com.example.wordlistsqlineractive">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/
Theme.WordListSQLIneractive">
<activity
android:name=".EditWordActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.wordlistsqlineractive;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;
import
com.google.android.material.floatingactionbutton.FloatingActionButton
;
if (requestCode == WORD_EDIT) {
if (resultCode == RESULT_OK) {
String word = data.getStringExtra(EditWordActivity.EXTRA_REPLY);
if (id == WORD_ADD) {
mDB.insert(word);
} else if (id >= 0) {
mDB.update(id, word);
}
// Update the UI.
mAdapter.notifyDataSetChanged();
} else {
Toast.makeText(
getApplicationContext(),
R.string.empty_not_saved,
Toast.LENGTH_LONG).show();
}
}
}
}
}
EditWordActivity.java
package com.example.wordlistsqlineractive;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_word);
WordListAdapter.java
package com.example.wordlistsqlineractive;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
@Override
public WordViewHolder onCreateViewHolder(ViewGroup
parent, int viewType) { View itemView =
mInflater.inflate(R.layout.activity_word_item, parent,
false);
return new WordViewHolder(itemView);
}
@Override
public void onBindViewHolder(WordViewHolder
holder, int position) { WordItem current =
mDB.query(position);
holder.wordItemView.setText(current.getWord());
// Keep a reference to the view holder for the click
listener final WordViewHolder h = holder; // needs to be
final for use in callback // Attach a click listener to the
DELETE button.
holder.delete_button.setOnClickListener(new
MyButtonOnClickListener( current.getId(), null) {
@Override
public void onClick(View v ) {
// You have to get the position like this, you can't
hold a reference
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext,
EditWordActivity.class); intent.putExtra(EXTRA_ID,
id);
intent.putExtra(EXTRA_POSITION, h.getAdapterPosition());
intent.putExtra(EXTRA_WORD, word);
@Override
public int getItemCount() {
return (int) mDB.count();
}
}
WordListOpenHelper.java
package com.example.wordlistsqlineractive;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
// Column names...
public static final String KEY_ID = "_id";
public static final String KEY_WORD = "word";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(WORD_LIST_TABLE_CREATE);
fillDatabaseWithData(db);
// We cannot initialize mWritableDB and mReadableDB here,
because this creates an infinite
// loop of on Create being repeatedly called.
}
try {
if (mReadableDB == null) {mReadableDB =
getReadableDatabase();} cursor =
mReadableDB.rawQuery(query, null);
cursor.moveToFirst();
entry.setId(cursor.getInt(cursor.getColumnIndex(KEY_ID)
));
entry.setWord(cursor.getString(cursor.getColumnIndex(KEY
_WORD))); } catch (Exception e) {
Log.d(TAG, "QUERY EXCEPTION! " + e.getMessage());
} finally {
// Must close cursor and db now that we are
done with it. cursor.close();
return entry;
}
}
mNumberOfRowsUpdated = mWritableDB.update(WORD_LIST_TABLE,
//table to change
values, // new values to insert
KEY_ID + " = ?", // selection criteria
for row (in this case, the _id column)
new String[]{String.valueOf(id)}); //selection args;
the actual value of the id
} catch (Exception e) {
Log.d (TAG, "UPDATE EXCEPTION! " +
e.getMessage()); }
return mNumberOfRowsUpdated;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
Log.w(WordListOpenHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to
" + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + WORD_LIST_TABLE);
onCreate(db);
}
}
WordItem.java
package com.example.wordlistsqlineractive;
public WordItem() {}
MyButtonOnClickListener.java
package com.example.wordlistsqlineractive;
import android.view.View;
/**
* Instantiated for the Edit and Delete
buttons in WordListAdapter. */
public class MyButtonOnClickListener implements
View.OnClickListener { private static final String TAG =
View.OnClickListener.class.getSimpleName();
int id;
String word;
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://
schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
<com.google.android.material.floatingactionbutton.Fl
oatingActionButton android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/ic_add_24dp"
android:focusable="true"
android:contentDescription="@string/todo" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
activity_edit_word.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/a
ndroid" android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit_word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:hint="@string/hint_word"
android:inputType="textAutoComplete"
android:padding="@dimen/small_padding"
android:layout_marginBottom="@dime
n/big_padding"
android:layout_marginTop="@dimen/bi
g_padding"
android:textSize="18sp" />
<Button
android:id="@+id/button_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/teal_700"
android:onClick="returnReply"
android:text="@string/button_save"
android:textColor="@color/white" />
</LinearLayout>
activity_word_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/a
ndroid" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dp">
<TextView
android:id="@+id/word"
android:layout_width="match_parent"
style="@style/word_title" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/delete_button"
android:layout_width="match_parent"
android:layout_height="@dimen/button_height"
android:layout_weight="2"
android:background="@color/teal_700"
android:text="@string/button_delete"
android:textColor="@color/white" />
<Button
android:id="@+id/edit_button"
android:layout_width="match_parent"
android:layout_height="@dimen/button_height"
android:layout_weight="1"
android:background="@color/teal_700"
android:text="@string/button_edit"
android:textColor="@color/white"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="@dimen/divider_height"
android:background="@color/teal_700" />
</LinearLayout>
WordListSQLIneractiveWithSearch
Hasil Program :
Code Program :
AndroidManifest.java
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/r
es/android"
package="com.example.wordlistsqlinteractivewith
search">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/
Theme.WordListSQLInteractiveWithSearch">
<activity android:name=".SearchActivity" />
<activity android:name=".EditWordActivity"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity.java
package com.example.wordlistsqlinteractivewithsearch;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import
com.google.android.material.floatingactionbutton.Floatin
gActionButton; public class MainActivity extends
AppCompatActivity {
private static final String TAG =
MainActivity.class.getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* Inflates the menu, and adds items to the action bar
if it is present. *
* @param menu Menu to inflate.
* @return Returns true if the menu inflated.
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search:
// Starts search activity.
Intent intent = new
Intent(getBaseContext(),com.example.wordlistsqlinteractivewit
hsearch.SearchActivit y.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
if (requestCode == WORD_EDIT) {
if (resultCode == RESULT_OK) {
String word = data.getStringExtra(EditWordActivity.EXTRA_REPLY);
if (id == WORD_ADD) {
mDB.insert(word);
} else if (id >= 0) {
mDB.update(id, word);
}
// Update the UI.
mAdapter.notifyDataSetChanged();
} else {
Toast.makeText(
getApplicationContext(),
R.string.empty_word_not_saved,
Toast.LENGTH_LONG).show();
}
}
}
}
}
EditWordActivity.java
package com.example.wordlistsqlinteractivewithsearch;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_word);
WordListAdapter.java
package com.example.wordlistsqlinteractivewithsearch;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
/**
* Custom view holder with a text view and two buttons.
*/
class WordViewHolder extends RecyclerView.ViewHolder {
public final TextView wordItemView;
Button delete_button;
Button edit_button;
@Override
public WordViewHolder onCreateViewHolder(ViewGroup
parent, int viewType) { View itemView =
mInflater.inflate(R.layout.activity_word_item, parent,
false);
return new WordViewHolder(itemView);
}
@Override
public void onBindViewHolder(WordViewHolder
holder, int position) { WordItem current =
mDB.query(position);
holder.wordItemView.setText(current.getWord());
// Keep a reference to the view holder for the click
listener final WordViewHolder h = holder; // needs to be
final for use in callback // Attach a click listener to the
DELETE button.
holder.delete_button.setOnClickListener(new
MyButtonOnClickListener( current.getId(), null) {
@Override
public void onClick(View v ) {
// You have to get the position like this, you can't
hold a reference
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext,
EditWordActivity.class); intent.putExtra(EXTRA_ID, id);
intent.putExtra(EXTRA_POSITION,
h.getAdapterPosition());
intent.putExtra(EXTRA_WORD, word);
@Override
public int getItemCount() {
return (int) mDB.count();
}
}
WordListOpenHelper.java
package com.example.wordlistsqlinteractivewithsearch;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
// Column names...
public static final String KEY_ID = "_id";
public static final String KEY_WORD = "word";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(WORD_LIST_TABLE_CREATE);
fillDatabaseWithData(db);
}
try {
if (mReadableDB == null) {
mReadableDB = getReadableDatabase();
}
cursor = mReadableDB.rawQuery(query, null);
cursor.moveToFirst();
entry.setId(cursor.getInt(cursor.getColumnIndex(KEY_ID)
));
entry.setWord(cursor.getString(cursor.getColumnIndex(KEY
_WORD))); } catch (Exception e) {
Log.d(TAG, "QUERY EXCEPTION! " + e); // Just log the
exception } finally {
// Must close cursor and db now that we are
done with it. cursor.close();
return entry;
}
}
mNumberOfRowsUpdated = mWritableDB.update(WORD_LIST_TABLE,
//table to change
values, // new values to insert
KEY_ID + " = ?", // selection criteria
for row (in this case, the _id column)
new String[]{String.valueOf(id)}); //selection args;
the actual value of the id
} catch (Exception e) {
Log.d (TAG, "UPDATE EXCEPTION! " + e);
}
return mNumberOfRowsUpdated;
}
/**
* Deletes one entry identified by its id.
*
* @param id ID of the entry to delete.
* @return The number of rows deleted. Since we are
deleting by id, this should be 0 or 1.
*/
public int delete(int id) {
int deleted = 0;
try {
if (mWritableDB == null) {
mWritableDB = getWritableDatabase();
}
deleted = mWritableDB.delete(WORD_LIST_TABLE,
//table name KEY_ID + " =? ", new String[]
{String.valueOf(id)}); } catch (Exception e) {
Log.d (TAG, "DELETE EXCEPTION! " +
e); } return deleted;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion) {
Log.w(WordListOpenHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to
" + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + WORD_LIST_TABLE);
onCreate(db);
}
}
WordItem.java
package com.example.wordlistsqlinteractivewithsearch;
public WordItem() {}
MyButtonOnClickListener.java
package com.example.wordlistsqlinteractivewithsearch;
import android.view.View;
/**
* Instantiated for the Edit and Delete
buttons in WordListAdapter. */
public class MyButtonOnClickListener implements
View.OnClickListener { private static final String TAG =
View.OnClickListener.class.getSimpleName();
int id;
String word;
SearchActivity.java
package com.example.wordlistsqlinteractivewithsearch;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
mEditWordView = ((EditText)
findViewById(R.id.search_word)); mTextView =
((TextView) findViewById(R.id.search_result));
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://
schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
<com.google.android.material.floatingactionbutton.Fl
oatingActionButton android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/ic_add_24dp"
android:focusable="true"
android:contentDescription="@string/todo" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
activity_edit_word.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/a
ndroid" android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edit_word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:hint="@string/hint_word"
android:inputType="textAutoComplete"
android:padding="@dimen/small_padding"
android:layout_marginBottom="@dime
n/big_padding"
android:layout_marginTop="@dimen/bi
g_padding"
android:textSize="18sp" />
<Button
android:id="@+id/button_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/teal_700"
android:onClick="returnReply"
android:text="@string/button_save"
android:textColor="@color/white" />
</LinearLayout>
activity_word_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/a
ndroid" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dp">
<TextView
android:id="@+id/word"
android:layout_width="match_parent"
style="@style/word_title" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/delete_button"
android:layout_width="match_parent"
android:layout_height="@dimen/
button_height"
android:layout_weight="2"
android:background="@color/teal_700"
android:text="@string/button_delete"
android:textColor="@color/white" />
<Button
android:id="@+id/edit_button"
android:layout_width="match_parent"
android:layout_height="@dimen/
button_height"
android:layout_weight="1"
android:background="@color/teal_700"
android:text="@string/button_edit"
android:textColor="@color/white"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="@dimen/divider_height"
android:background="@color/teal_700" />
</LinearLayout>
activity_search.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/a
ndroid" android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/search_word"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:hint="@string/hint_word"
android:inputType="textAutoComplete"
android:padding="@dimen/small_padding"
android:layout_marginBottom="@dime
n/big_padding"
android:layout_marginTop="@dimen/bi
g_padding"
android:textSize="18sp" />
<Button
android:id="@+id/button_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/teal_700"
android:onClick="showResult"
android:text="@string/button_search"
android:textColor="@color/white" />
<TextView
android:id="@+id/search_result"
android:layout_width="match_parent"
android:layout_height="300dp"
android:textSize="18sp"
android:hint="@string/search_results"/>
</LinearLayout>
Hasil Program :
Code Program :
Hasil Program :
Code Program :