Open a new pit, this column will share some simple small apps

Effect is introduced

Use Android built-in database to realize data storage, use AlertDialog to realize pop-up effect, the main function is to trigger events through UI, set the listener to monitor specific events, and the corresponding processing operation, the main operation is database operation, add, delete, change and check.

The content we created in AS

Let’s code these Java classes and XML files one by one

MainActivity

package com.example.app1; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.database.Cursor; import android.os.Bundle; import android.view.ContextMenu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleCursorAdapter; public class MainActivity extends AppCompatActivity { Database phoneDatabase; SimpleCursorAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_main); phoneDatabase=new Database(this); phoneDatabase.open(); ListView lv=findViewById(R.id.listview); Cursor c = phoneDatabase.query(); adapter=new SimpleCursorAdapter(this,R.layout.row_view, c,new String[]{Database.KEY_NAME,Database.KEY_PHONE}, new int[]{R.id.row_view_tv_name,R.id.row_view_tv_phone}); lv.setAdapter(adapter); Button bt_insert=findViewById(R.id.bt_insert); Button bt_reset=findViewById(R.id.bt_reset); bt_insert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addData(); }}); bt_reset.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { phoneDatabase.reset(); updateListView(); }}); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<? > parent, View view, int position, long id) { Database.PhoneCursor c = phoneDatabase.queryById(position+1); c.moveToFirst(); Data pd; Be sad etPhone chtistina georgina rossetti.british poetess if ((.) the equals (" pending ")) {pd = new Data (c.g etName (), "finished"); }else {pd = new Data(c.get_name (), "to finish "); } c.close(); phoneDatabase.update(pd,position+1); updateListView(); }}); registerForContextMenu(lv); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); getMenuInflater().inflate(R.menu.ctx_menu,menu); } @Override public boolean onContextItemSelected(@NonNull MenuItem item) { AdapterView.AdapterContextMenuInfo menuInfo =  (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); long id = menuInfo.id; switch (item.getItemId()){ case R.id.ctx_mod: modifyData(id); break; case R.id.ctx_del: deleteData(id); break; } return super.onContextItemSelected(item); } public void addData(){DataDialog DataDialog = new DataDialog(this," create new target "); DataDialog.showDialog(new Data("", ""), new DataDialog.OnDialogSubmitListener() { @Override public void onSubmit(Data pd) { phoneDatabase.insert(pd); updateListView(); }}); } public void deleteData(long id){ phoneDatabase.delete(id); updateListView(); } public void modifyData(final long id){ Database.PhoneCursor c = phoneDatabase.queryById(id); c.moveToFirst(); Data phoneData = new Data(c.getName(), c.getPhone()); c.close(); DataDialog = new DataDialog(this," target task "+ id); phoneDataDialog.showDialog(phoneData, new DataDialog.OnDialogSubmitListener() { @Override public void onSubmit(Data pd) { phoneDatabase.update(pd,id); updateListView(); }}); } private void updateListView() { adapter.getCursor().requery(); } @Override protected void onDestroy() { super.onDestroy(); phoneDatabase.close(); }}Copy the code

DataDialog

package com.example.app1; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.view.LayoutInflater; import android.view.View; import android.widget.EditText; public class DataDialog { private Context context; private String title; public DataDialog(Context context, String title) { this.context = context; this.title = title; } public interface OnDialogSubmitListener { public void onSubmit(Data pd); } public void showDialog(final Data pd, final OnDialogSubmitListener l){ AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle(title); View v = LayoutInflater.from(context).inflate(R.layout.dialog_view, null, false); final EditText name = v.findViewById(R.id.dialog_view_name); name.setText(pd.getName()); builder.setView(v); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if(l ! =null){ pd.setName(name.getText().toString()); Pd. setPhone(" to be done "); l.onSubmit(pd); }}}); builder.setNegativeButton("Cancel",null); builder.create().show(); }}Copy the code

Database

package com.example.app1; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.CursorWrapper; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class Database { private static final String DB_NAME = "phone_database.db"; private static final String TABLE_NAME = "contact"; public static final String KEY_NAME = "name"; public static final String KEY_PHONE = "phone"; private Context context; private int version = 1; private DatabaseHelper databaseHelper; private SQLiteDatabase db; public Database(Context context) { this.context = context; databaseHelper = new DatabaseHelper(); } public void open() { if (db == null || ! db.isOpen()) { db = databaseHelper.getWritableDatabase(); } } public void close() { if (db ! = null && db.isOpen()) { db.close(); } } public ContentValues getContentValues(Data pd) { ContentValues cv = new ContentValues(); cv.put(KEY_NAME, pd.getName()); cv.put(KEY_PHONE, pd.getPhone()); return cv; } public long insert(Data pd) { return db.insert(TABLE_NAME, null, getContentValues(pd)); } public int delete(long id) { return db.delete(TABLE_NAME, "_id=" + id, null); } public int update(Data pd, long id) { return db.update(TABLE_NAME, getContentValues(pd), "_id=" + id, null); } public void reset() { databaseHelper.resetData(db); } public Cursor query() { String sql = String.format("select * from %s", TABLE_NAME); return db.rawQuery(sql, null); } public Cursor fuzzyQuery(String keyword) { String sql = String.format("select * from %s where %s like ? or %s like ?" , TABLE_NAME, KEY_NAME, KEY_PHONE); return db.rawQuery(sql, new String[]{"%" + keyword + "%", "%" + keyword + "%"}); } public PhoneCursor queryById(long id) { String sql = String.format("select * from %s where _id=%d", TABLE_NAME, id); Cursor cursor = db.rawQuery(sql,null); return new PhoneCursor(cursor); } class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper() { super(context, DB_NAME, null, version); } @Override public void onCreate(SQLiteDatabase db) { String sql = String.format("create table if not exists %s " + "(_id integer primary key autoincrement,%s text,%s text)", TABLE_NAME ,KEY_NAME, KEY_PHONE); db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { resetData(db); } public void resetData(SQLiteDatabase db) { String sql = String.format("drop table if exists %s", TABLE_NAME); db.execSQL(sql); onCreate(db); } } class PhoneCursor extends CursorWrapper { private Cursor cursor; public PhoneCursor(Cursor cursor) { super(cursor); this.cursor = cursor; } public String getName() { return cursor.getString(cursor.getColumnIndex(KEY_NAME)); } public String getPhone() { return cursor.getString(cursor.getColumnIndex(KEY_PHONE)); }}}Copy the code

Data

package com.example.app1; public class Data { private String name; private String phone; public Data(String name, String phone) { this.name = name; this.phone = phone; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; }}Copy the code

dialog_view.xml

<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/dialog_view_name" Android :layout_width="match_parent" Android :layout_height="wrap_content" Android :hint=" task target" android:inputType="textPersonName" android:text="" /> </LinearLayout>Copy the code

my_main.xml

<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" Android :layout_height="wrap_content" Android :text=" Learn." /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/bt_insert" android:layout_width="0dp" Android :layout_height="wrap_content" Android: Layout_weight ="1" Android :text=" create target "/> <Button Android :id="@+id/bt_reset" Android :layout_height="wrap_content" Android :layout_weight="1" Android :text=" reset "/> </LinearLayout> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>Copy the code

row_view.xml

<? The XML version = "1.0" encoding = "utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="5dp" android:paddingBottom="5dp" android:paddingLeft="4dp"> <TextView android:id="@+id/row_view_tv_name" Android :layout_height="wrap_content" Android :layout_weight="1" Android :text=" target task "/> <TextView android:id="@+id/row_view_tv_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" Android :paddingLeft="15dp" Android :text=" to complete "/> </LinearLayout>Copy the code

ctx_menu.xml

<? The XML version = "1.0" encoding = "utf-8"? > < menu XMLNS: android = "http://schemas.android.com/apk/res/android" > < item android: id = "@ + id/ctx_mod android:" title = "change" / > < item android: id = "@ + id/ctx_del android:" title = "delete" / > < / menu >Copy the code