Created
November 30, 2020 07:27
-
-
Save edward1986/60928dbaa1e035cbfb30056f6ecc22da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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:orientation="vertical"> | |
<EditText android:id="@+id/editTextWord" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentTop="true" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentStart="true" | |
android:hint="Word"/> | |
<EditText android:id="@+id/editTextDefinition" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/editTextWord" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentStart="true" | |
android:hint="Definition"/> | |
<Button android:id="@+id/buttonAddUpdate" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Save" | |
android:layout_alignParentRight="true" | |
android:layout_alignParentTop="true" /> | |
<ListView android:id="@+id/listView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/et_definition" | |
android:layout_alignParentLeft="true" | |
android:layout_alignParentBottom="true" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.loader; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.CursorAdapter; | |
import android.widget.TextView; | |
public class DictionaryAdapter extends CursorAdapter { | |
public DictionaryAdapter(Context context, Cursor c, int flags) { | |
super(context, c, flags); | |
} | |
@Override | |
public View newView(Context context, Cursor cursor, ViewGroup parent) { | |
return LayoutInflater.from(context) | |
.inflate(android.R.layout.simple_list_item_1, parent, false); | |
} | |
@Override | |
public void bindView(View view, Context context, Cursor cursor) { | |
TextView textView = view.findViewById(android.R.id.text1); | |
textView.setText(cursor.getString(getCursor().getColumnIndex("word"))); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.loader; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
public class DictionaryDatabase extends SQLiteOpenHelper { | |
private static final String DATABASE_NAME = "dictionary.db"; | |
private static final String TABLE_DICTIONARY = "dictionary"; | |
private static final String FIELD_WORD = "word"; | |
private static final String FIELD_DEFINITION = "definition"; | |
private static final int DATABASE_VERSION = 1; | |
DictionaryDatabase(Context context) { | |
super(context, DATABASE_NAME, null, DATABASE_VERSION); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
db.execSQL("CREATE TABLE " + TABLE_DICTIONARY + | |
"(_id integer PRIMARY KEY," + | |
FIELD_WORD + " TEXT, " + | |
FIELD_DEFINITION + " TEXT);"); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
//Handle database upgrade as needed | |
} | |
public void saveRecord(String word, String definition) { | |
long id = findWordID(word); | |
if (id > 0) { | |
updateRecord(id, word, definition); | |
} else { | |
addRecord(word, definition); | |
} | |
} | |
public long addRecord(String word, String definition) { | |
SQLiteDatabase db = getWritableDatabase(); | |
ContentValues values = new ContentValues(); | |
values.put(FIELD_WORD, word); | |
values.put(FIELD_DEFINITION, definition); | |
return db.insert(TABLE_DICTIONARY, null, values); | |
} | |
public int updateRecord(long id, String word, String definition) { | |
SQLiteDatabase db = getWritableDatabase(); | |
ContentValues values = new ContentValues(); | |
values.put("_id", id); | |
values.put(FIELD_WORD, word); | |
values.put(FIELD_DEFINITION, definition); | |
return db.update(TABLE_DICTIONARY, values, "_id = ?", new String[]{String.valueOf(id)}); | |
} | |
public int deleteRecord(long id) { | |
SQLiteDatabase db = getWritableDatabase(); | |
return db.delete(TABLE_DICTIONARY, "_id = ?", new String[]{String.valueOf(id)}); | |
} | |
public long findWordID(String word) { | |
long returnVal = -1; | |
SQLiteDatabase db = getReadableDatabase(); | |
Cursor cursor = db.rawQuery( | |
"SELECT _id FROM " + TABLE_DICTIONARY + " WHERE " + FIELD_WORD + " = ?", | |
new String[]{word}); | |
if (cursor.getCount() == 1) { | |
cursor.moveToFirst(); | |
returnVal = cursor.getInt(0); | |
} | |
return returnVal; | |
} | |
public String getDefinition(long id) { | |
String returnVal = ""; | |
SQLiteDatabase db = getReadableDatabase(); | |
Cursor cursor = db.rawQuery( | |
"SELECT definition FROM " + TABLE_DICTIONARY + " WHERE _id = ?", | |
new String[]{String.valueOf(id)}); | |
if (cursor.getCount() == 1) { | |
cursor.moveToFirst(); | |
returnVal = cursor.getString(0); | |
} | |
return returnVal; | |
} | |
public Cursor getWordList() { | |
SQLiteDatabase db = getReadableDatabase(); | |
String query = "SELECT _id, " + FIELD_WORD + | |
" FROM " + TABLE_DICTIONARY + " ORDER BY " + FIELD_WORD + | |
" ASC"; | |
return db.rawQuery(query, null); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.loader; | |
import android.content.Context; | |
import android.database.Cursor; | |
import androidx.loader.content.CursorLoader; | |
public class DictionaryLoader extends CursorLoader { | |
Context mContext; | |
public DictionaryLoader(Context context) { | |
super(context); | |
mContext = context; | |
} | |
@Override | |
public Cursor loadInBackground() { | |
DictionaryDatabase db = new DictionaryDatabase(mContext); | |
return db.getWordList(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.loader; | |
import android.database.Cursor; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.ListView; | |
import android.widget.Toast; | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.loader.app.LoaderManager; | |
import androidx.loader.content.Loader; | |
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{ | |
EditText mEditTextWord; | |
EditText mEditTextDefinition; | |
DictionaryDatabase mDB; | |
ListView mListView; | |
DictionaryAdapter mAdapter; | |
@Override | |
protected void onCreate(Bundle savedInstanceState){ | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mDB = new DictionaryDatabase(this); | |
mEditTextWord = findViewById(R.id.editTextWord); | |
mEditTextDefinition = findViewById(R.id.editTextDefinition); | |
Button buttonAddUpdate = findViewById(R.id.buttonAddUpdate); | |
buttonAddUpdate.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
saveRecord(); | |
} | |
}); | |
mListView = findViewById(R.id.listView); | |
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
Toast.makeText(MainActivity.this, | |
mDB.getDefinition(id), | |
Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
mListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { | |
@Override | |
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { | |
Toast.makeText(MainActivity.this, "Records deleted = " + mDB.deleteRecord(id), | |
Toast.LENGTH_SHORT).show(); | |
LoaderManager.getInstance(MainActivity.this).restartLoader(0, null, MainActivity.this); | |
return true; | |
} | |
}); | |
LoaderManager.getInstance(this).initLoader(0, null, this); | |
mAdapter = new DictionaryAdapter(this, mDB.getWordList(), 0); | |
mListView.setAdapter(mAdapter); | |
} | |
private void saveRecord() { | |
mDB.saveRecord(mEditTextWord.getText().toString(), mEditTextDefinition.getText().toString()); | |
mEditTextWord.setText(""); | |
mEditTextDefinition.setText(""); | |
LoaderManager.getInstance(MainActivity.this).restartLoader(0, null, MainActivity.this); | |
} | |
@Override | |
public Loader<Cursor> onCreateLoader(int id, Bundle args){ | |
return new DictionaryLoader(this); | |
} | |
@Override | |
public void onLoadFinished(Loader<Cursor> loader, Cursor data){ | |
mAdapter.swapCursor(data); | |
} | |
@Override | |
public void onLoaderReset(Loader<Cursor> loader) { | |
mAdapter.swapCursor(null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment