Android Notepad applet development

Keywords: Android Database SQL Java

In recent days, I have made a notepad applet with my current Android development knowledge. I would like to share the development process here, hoping to help beginners like me.

The development tool is Android studio, the background language is java, and the database used is Android SQLite database. The functions and renderings are as follows:

Main interface, long press to delete:

 

 

 

 

Click the plus sign to add:

 

Click View on the main page, this page includes modification and deletion functions:

 

Main technologies used: database storage used for data storage. My previous blog talked about the basic operation of Android SQLite; ListView component is used for data display, and intent technology is used for data transmission, with the help of intent for jump between pages; click a line on the homepage, pass its id to the viewing page through intent, and read data from the database according to id for display , deletion is also a database operation based on id.

The entire project file has been synchronized to GitHub. Please take https://github.com/liuleliu/textbook

Here is the main code:

Database auxiliary class:

package com.example.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseHelp extends SQLiteOpenHelper{
    private static final String DATABASENAME ="tip1";//Database name
    private static final int DATABASEVERSION =1;//Database version
    private static final String TABLENAME="tip1";//Table name
    public DataBaseHelp(Context context)//Definition construction
    {
    super(context,DATABASENAME,null,DATABASEVERSION);    //Call parent class construction
    }
    public void onCreate(SQLiteDatabase db)
    {
        String sql="CREATE TABLE "+TABLENAME+"("+
                "id   INTEGER PRIMARY KEY AUTOINCREMENT,"+                   //Set auto grow columns
                "name  VARCHAR(50) NOT NULL,"+
                "text  VARCHAR(50) NOT NULL)";
    db.execSQL(sql);    //Execute sql statement
    }
    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
    {
        String sql="DROP TABLE IF EXISTS "+TABLENAME;
        db.execSQL(sql);
        this.onCreate(db);//Create table

    }
}

Operation class

package com.example.myapplication;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OperateTable {
    private static final String TABLENAME ="tip1";
    private SQLiteDatabase db=null;
    public OperateTable(SQLiteDatabase db)
    {
        this.db=db;
    }
    public void insert(String name,String text)
    {
        String sql="INSERT INTO "+TABLENAME+" (name,text) VALUES ('"+name+"','"+text+"')";
        this.db.execSQL(sql);


    }
    public void delete(String id)
    {
        String sql="DELETE FROM "+TABLENAME+" WHERE id='"+id+"'";
        this.db.execSQL(sql);


    }
    public void updata(String id,String name,String text)
    {
        String sql="UPDATE "+TABLENAME+" SET name ='"+name+"',text='"+text+"' WHERE id='"+id+"'";
        this.db.execSQL(sql);
    }
    public List<Map<String,Object>> getdata()
    {List<Map<String,Object>>list=new ArrayList<Map<String,Object>>();
        Map<String,Object> map=new HashMap<String,Object>();

        String sql="SELECT id,name,text FROM "+TABLENAME;
        Cursor result =this.db.rawQuery(sql,null);
        for(result.moveToFirst();!result.isAfterLast();result.moveToNext())
        {
            map=new HashMap<String,Object>();
            map.put("id",result.getInt(0));
            map.put("tt",result.getString(1));
            list.add(map);
        }
        return  list;}
        public tip t(String id)
        {
            tip t=new tip();

            String sql="SELECT name,text FROM "+TABLENAME+" WHERE id ='"+id+"'";
            Cursor result =this.db.rawQuery(sql,null);
            result.moveToFirst();
            t.setName(result.getString(0));
            t.setText(result.getString(1));
            return t;
        }
}

  

The data display and interaction of the home page involve the use of ListView

The update of data needs to overload the onResume() function to implement

Layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/add"
        android:layout_width="59dp"
        android:layout_height="57dp"
        android:layout_marginEnd="28dp"
        android:clickable="true"
        app:backgroundTint="#2894FF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.807"
        app:srcCompat="@android:drawable/ic_input_add" />

    <ListView
        android:id="@+id/vi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="389dp"
        app:layout_constraintBottom_toTopOf="@+id/add"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.034"></ListView>

</androidx.constraintlayout.widget.ConstraintLayout>

Here is the layout of each line

    <TextView
        android:id="@+id/id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="39dp"
        android:layout_marginBottom="24dp"
        android:text="TextView"
        android:textColor="#00000000"
        app:layout_constraintBottom_toBottomOf="@+id/pic"
        app:layout_constraintStart_toStartOf="parent" />

    <ImageView
        android:id="@+id/pic"
        android:layout_width="142dp"
        android:layout_height="101dp"
        android:src="@mipmap/ic_launcher_round"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Then it is the operation in the main class, assigning values to the ListView, setting interaction events (stand-alone, long press)

package com.example.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
private OperateTable mytable =null;
private SQLiteOpenHelper helper=null;
private FloatingActionButton add=null;
private String info=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        helper=new DataBaseHelp(this);
        helper.getWritableDatabase();
        MainActivity.this.mytable=new OperateTable(MainActivity.this.helper.getWritableDatabase());


        SimpleAdapter adapter = new SimpleAdapter(this,this.mytable.getdata(), R.layout.activity_main
                , new String[]{"id","tt"},
                new int[]{R.id.id,R.id.tt});
        ListView listView=(ListView)findViewById(R.id.vi);
        add=(FloatingActionButton)findViewById(R.id.add);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClick());//Register Click to listen
listView.setOnItemLongClickListener(new OnItemLongClick());//Registrar press monitor
add.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent it=new Intent(MainActivity.this,add.class);
        MainActivity.this.startActivity(it);
    }
});
    }
   
    private class OnItemClick implements AdapterView.OnItemClickListener
    { public void onItemClick(AdapterView<?>arg0, View arg1, int arg2, long arg3){
        ListView list = (ListView) findViewById(R.id.vi);
        HashMap<String,Object> map=(HashMap<String,Object>)list.getItemAtPosition(arg2);

        info=map.get("id").toString();
        Intent it=new Intent(MainActivity.this,receive.class);
        it.putExtra("info",info);//Transfer data to receive
        MainActivity.this.startActivity(it);



    }



    }
    private class OnItemLongClick implements AdapterView.OnItemLongClickListener
    {
        public boolean onItemLongClick(AdapterView<?>arg0, View arg1, int arg2, long arg3){

            ListView list = (ListView) findViewById(R.id.vi);
            HashMap<String,Object> map=(HashMap<String,Object>)list.getItemAtPosition(arg2);
            String name;
            info=map.get("id").toString();
            name=map.get("tt").toString();
            AlertDialog myAlertDialog = new AlertDialog.Builder(MainActivity.this)
                    .setTitle("confirm" )
                    .setMessage("OK to delete“"+name+""Do you?" )
                    .setPositiveButton("yes" , new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            mytable.delete(info);
                            Toast.makeText(getApplicationContext(),"Delete successful",Toast.LENGTH_SHORT).show();
                            onResume();
                        }
                    })
                    .setNegativeButton("no" , null)
                    .show();

            return true;


        }

    }

//Every time I go back to the home page, it will be executed to update the data
    public void onResume() {
        super.onResume();  // Always call the superclass method first

        SimpleAdapter adapter = new SimpleAdapter(this,MainActivity.this.mytable.getdata(), R.layout.activity_main
                , new String[]{"id","tt"},
                new int[]{R.id.id,R.id.tt});
        ListView listView=(ListView)findViewById(R.id.vi);
        add=(FloatingActionButton)findViewById(R.id.add);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new OnItemClick());
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent it=new Intent(MainActivity.this,add.class);
                MainActivity.this.startActivity(it);
            }
        });
    }

}

  

It is easy to add, that is to get data and call database operation to save data

layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/linearLayout3"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/aname"
        android:layout_width="283dp"
        android:layout_height="0dp"
        android:layout_marginStart="60dp"
        android:layout_marginTop="26dp"
        android:layout_marginBottom="461dp"
        android:hint="Title"
        android:maxLines="1"
        android:maxLength="25"
        app:layout_constraintBottom_toTopOf="@+id/save"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"></EditText>

    <EditText
        android:id="@+id/atext"
        android:layout_width="335dp"
        android:layout_height="426dp"
        android:layout_marginStart="43dp"
        android:layout_marginBottom="84dp"
        android:hint="content"
        android:minLines="5"
        android:gravity="top"
        android:background="@null"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/aname"></EditText>

    <Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="17dp"
        android:text="Preservation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/atext"
        app:layout_constraintTop_toBottomOf="@+id/aname" />

</androidx.constraintlayout.widget.ConstraintLayout>

  

Activity class

package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;

import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


import android.widget.Toast;



public class add extends AppCompatActivity {
private  OperateTable mytable =null;
    private  SQLiteOpenHelper helper=null;

    private EditText name=null;
    private EditText text=null;
    private  Button save=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add);

        this.name=(EditText)super.findViewById(R.id.aname);
        this.text=(EditText)super.findViewById(R.id.atext);
        Button save=( Button) super.findViewById(R.id.save);

        helper=new DataBaseHelp(this);
        helper.getWritableDatabase();
        mytable=new OperateTable(helper.getWritableDatabase());
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(name.getText().toString().equals("")){Toast.makeText(getApplicationContext(),"Please enter a title",Toast.LENGTH_SHORT).show();}
               else{ mytable.insert(name.getText().toString(),text.getText().toString());//Save data
                Toast.makeText(getApplicationContext(),"Save successfully",Toast.LENGTH_SHORT).show();
                add.this.finish();//End the current Activity and return to the main page}

            }
        });

    }


}

View the page, receive the id from the home page through intent, obtain the corresponding data and display

Layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/name"
        android:layout_width="303dp"
        android:layout_height="46dp"
        android:layout_marginStart="60dp"
        android:layout_marginTop="36dp"
        android:layout_marginBottom="14dp"
        android:hint="Title"
        android:text="TextView"
        android:maxLength="25"
        app:layout_constraintBottom_toTopOf="@+id/divider"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/text"
        android:layout_width="308dp"
        android:layout_height="400dp"
        android:layout_marginStart="52dp"
        android:layout_marginEnd="52dp"
        android:layout_marginBottom="63dp"
        android:background="@null"
        android:gravity="top"
        android:hint="content"
        android:minLines="5"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/name"
        app:layout_constraintVertical_bias="1.0" />

    <View
        android:id="@+id/divider"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="1dp"
        android:layout_marginTop="83dp"
        android:layout_marginEnd="1dp"
        android:layout_marginBottom="433dp"

        android:background="?android:attr/listDivider"
        app:layout_constraintBottom_toTopOf="@+id/delete"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="29dp"
        android:layout_marginBottom="6dp"
        android:text="delete"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/divider" />

    <Button
        android:id="@+id/edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="29dp"
        android:layout_marginBottom="7dp"
        android:text="Save changes"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Activities

package com.example.myapplication;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.content.Intent;
import android.widget.Toast;

public class receive extends AppCompatActivity {

    private EditText name=null;
    private EditText text=null;
    private Button delete=null;
    private Button edit=null;
    private OperateTable mytable =null;
    private SQLiteOpenHelper helper=null;
    private String info=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.receive);

        this.name=(EditText)super.findViewById(R.id.name);
        this.text=(EditText)super.findViewById(R.id.text);
        this.delete=(Button)super.findViewById(R.id.delete);
        this.edit=(Button)super.findViewById(R.id.edit);

    Intent it=super.getIntent();
    info=it.getStringExtra("info");//Get id of homepage delivery

        helper=new DataBaseHelp(this);
        helper.getWritableDatabase();
      mytable=new OperateTable(helper.getWritableDatabase());
    tip t=mytable.t(info);
   name.setText(t.getName());
   text.setText(t.getText());
   delete.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           AlertDialog myAlertDialog = new AlertDialog.Builder(receive.this)
                   .setTitle("confirm" )
                   .setMessage("OK to delete“"+name.getText()+""Do you?" )
                   .setPositiveButton("yes" , new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int whichButton) {
                           mytable.delete(info);
                           Toast.makeText(getApplicationContext(),"Delete successful",Toast.LENGTH_SHORT).show();
                         receive.this.finish();
                       }
                   })
                   .setNegativeButton("no" , null)
                   .show();
       }
   });
   edit.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           mytable.updata(info,name.getText().toString(),text.getText().toString());
           Toast.makeText(getApplicationContext(),"Modified success",Toast.LENGTH_SHORT).show();
           receive.this.finish();
       }
   });
    }


}

Posted by arimakidd on Thu, 23 Jan 2020 06:21:50 -0800