How to implement a simple word book Android App based on Java
This article analyzes "how to implement a simple word book Android App based on Java". The content is detailed and easy to understand. Friends who are interested in "how to achieve a simple word book Android App based on Java" can follow the editor's train of thought to read it slowly and deeply. I hope it will be helpful to you after reading. Let's learn more about "how to implement a simple word book Android App based on Java" with the editor.
Layout design
Word book main interface
Code AddDanciActivity.java
Activity of the main interface of the word book
Import androidx.appcompat.app.AppCompatActivity;import android.content.ContentValues;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;import java.util.ArrayList;import java.util.HashMap;import java.util.Map; public class AddDanciActivity extends AppCompatActivity {private EditText wordedit; private EditText yisiedit; private Button add_btn Private Button quxiao_btn; private Button shanchu_btn; private ListView listview; private DBOpenHelper dbOpenHelper;// statement @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_add_danci); dbOpenHelper = new DBOpenHelper (AddDanciActivity.this, "db_dict", null, 1); / / instantiate, create database wordedit = (EditText) findViewById (R.id.addword_edit) Yisiedit = (EditText) findViewById (R.id.fanyiword_edit); listview = (ListView) findViewById (R.id.add_list); add_btn = (Button) findViewById (R.id.add_btn); quxiao_btn = (Button) findViewById (R.id.quxiao_btn); shanchu_btn = (Button) findViewById (R.id.shanchu_btn) Quxiao_btn.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {Toast.makeText (AddDanciActivity.this, "return to the word book main interface", Toast.LENGTH_SHORT) .show (); finish ();}}) Shanchu_btn.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {String word = wordedit.getText () .toString (); String ys = yisiedit.getText () .toString () If (word.equals (")) {Toast.makeText (AddDanciActivity.this," empty word ", Toast.LENGTH_SHORT). Show ();} else {deleteData (dbOpenHelper.getReadableDatabase (), word); Toast.makeText (AddDanciActivity.this," deleted successfully ", Toast.LENGTH_SHORT) .show () }); add_btn.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {String word = wordedit.getText () .toString (); String ys = yisiedit.getText () .toString () If (word.equals (") | | ys.equals (")) {Toast.makeText (AddDanciActivity.this, "filled in or interpreted as empty", Toast.LENGTH_SHORT) .show ();} else {insertData (dbOpenHelper.getReadableDatabase (), word, ys) / / insert new words Toast.makeText (AddDanciActivity.this, "successful addition of new words", Toast.LENGTH_SHORT). Show (); renew ();} / insert data method private void insertData (SQLiteDatabase sqLiteDatabase, String word, String ys) {ContentValues values = new ContentValues () Values.put ("word", word); / Save the word values.put ("detail", ys); sqLiteDatabase.insert ("tb_dict", null, values); / / perform the insert operation renew ();} private void deleteData (SQLiteDatabase sqLiteDatabase, String word) {ContentValues values = new ContentValues (); String [] args = {String.valueOf (word)} SqLiteDatabase.delete ("tb_dict", "word=?", args); / / perform delete operation renew ();} @ Override protected void onDestroy () {super.onDestroy (); if (dbOpenHelper! = null) {dbOpenHelper.close () / / close}} public void renew () {Cursor cursor = dbOpenHelper.getReadableDatabase (). Query ("tb_dict", null, null); ArrayList resultList = new ArrayList (); while (cursor.moveToNext ()) {Map map = new HashMap (); map.put ("word", cursor.getString (1)) Map.put ("interpret", cursor.getString (2)); resultList.add (map);} if (resultList = = null | | resultList.size () = = 0) {Toast.makeText (AddDanciActivity.this, "Unfortunately, there is no relevant record!" , Toast.LENGTH_SHORT) .show ();} else {SimpleAdapter simpleAdapter = new SimpleAdapter (AddDanciActivity.this, resultList, R.layout.item, new String [] {"word", "interpret"}, new int [] {R.id.textView, R.id.textView2}); listview.setAdapter (simpleAdapter);}} @ Override protected void onStart () {super.onStart () Renew ();}} DBOpenHelper.java
SQLite database is used, and Android comes with a lightweight database, which is very easy to use.
Import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log;import androidx.annotation.Nullable; public class DBOpenHelper extends SQLiteOpenHelper {final String CREATE_TABLE_SQL = "create table tb_dict (_ id integer primary key autoincrement,word,detail)"; / / define the public DBOpenHelper (@ Nullable Context context, @ Nullable String name, @ Nullable SQLiteDatabase.CursorFactory factory, int version) {super (context, name, null, version) that creates the table. } @ Override public void onCreate (SQLiteDatabase db) {db.execSQL (CREATE_TABLE_SQL); / / create a datasheet of words} @ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {Log.i ("dictionary", "--version update" + oldVersion + "-->" + newVersion);}}
Effect picture
What are the characteristics of Java? what are the characteristics of Java? what is the 1.Java language that represents the static object-oriented programming language? it implements the object-oriented theory and allows programmers to carry out complex programming in an elegant way of thinking. 2.Java has the characteristics of simplicity, object-oriented, distributed, security, platform independence and portability, dynamic and so on. 3. Using Java, you can write desktop applications, Web applications, distributed systems, embedded system applications, and so on.
On how to achieve a simple word based on Java Android App to share here, I hope that the above content can make you improve. If you want to learn more knowledge, please pay more attention to the editor's updates. Thank you for following the website!