今天把功能完善了,记事本大体完成:
AddContent:
package bjfu.it.sun.cashbook; import android.app.Activity; import android.content.ContentValues; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import java.text.SimpleDateFormat; import java.util.Date; public class AddContent extends Activity implements View.OnClickListener { private String val;//用来做接收 private Button submitbtn,canclebtn; private EditText context,coast; private CashDB cashDB ;//创建数据库对象准备添加数据 private SQLiteDatabase dbWriter;//创造可添加的选项 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_content);//绑定视图 val=getIntent() .getStringExtra("flag") ;//接收完成 submitbtn=findViewById(R.id.submit) ; canclebtn=findViewById(R.id.cancel) ; context=findViewById(R.id.context) ; coast=findViewById(R.id.coast) ; submitbtn.setOnClickListener(this) ; canclebtn.setOnClickListener(this) ; cashDB=new CashDB(this); //进行实例化 dbWriter=cashDB.getWritableDatabase();// //获取到当前可添加的写入权限 } @Override public void onClick(View v) { switch (v.getId() ){ case R.id.submit: addDB(); finish() ; break; case R.id.cancel: finish() ; break; } } //添加数据 private void addDB(){ ContentValues cv=new ContentValues() ; cv.put(CashDB .CONTENT , context.getText().toString() ); cv.put(CashDB .COAST , coast .getText().toString() ); cv.put(CashDB .TIME , getTime() ); dbWriter .insert(CashDB .TABLE_NAME ,null,cv) ; } //获取当前设备的时间 public String getTime(){ SimpleDateFormat format =new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss") ; Date curData=new Date();//实例化 String str =format .format(curData);//获取时间 return str; } }
CashDB:
package bjfu.it.sun.cashbook; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import androidx.annotation.Nullable; public class CashDB extends SQLiteOpenHelper { public static final String TABLE_NAME="cash";//表名 public static final String CONTENT="context";//花费的内容,表的列 public static final String COAST="coast";//花费的钱 public static final String ID="_id";//id public static final String TIME="time";//时间 //构造方法 public CashDB(@Nullable Context context) { //第一个参数context,第二个是库名,第三个factory传一个空,第四个版本号第一次创建所以是1 super(context, TABLE_NAME, null, 1); } //创建数据库 @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table "+TABLE_NAME+"(" +ID+" INTEGER PRIMARY KEY AUTOINCREMENT," +CONTENT+" TEXT NOT NULL," +COAST+" TEXT NOT NULL," +TIME +" TEXT NOT NULL)" ); } //更新 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
list_item:
package bjfu.it.sun.cashbook; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; public class list_item extends AppCompatActivity { private String val;//用来做接收 private ListView lv; private CashDB cashDB; private SQLiteDatabase dbReader; private MyAdapter adapter; Cursor cursor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_item); val=getIntent() .getStringExtra("flag") ;//接收完成 initView(); } //初始化操作,初始化view public void initView(){ lv = (ListView) findViewById(R.id.list2); cashDB=new CashDB(this) ; dbReader = cashDB.getReadableDatabase();//获取数据库读取的权限 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { cursor.moveToPosition(position );//获取当前cursor所指定的当前position //通过intent把当前数据传到另一个页面(详情页面) Intent i=new Intent(list_item .this,SelectAct.class) ;//第一个是当前页面,第二个是跳转页面 i.putExtra(CashDB.ID ,cursor.getInt(cursor .getColumnIndex(CashDB.ID) ) );//传递的内容 i.putExtra(CashDB.CONTENT ,cursor .getString(cursor .getColumnIndex(CashDB.CONTENT) ) ); i.putExtra(CashDB.COAST ,cursor .getString(cursor .getColumnIndex(CashDB.COAST ) ) ); i.putExtra(CashDB.TIME ,cursor .getString(cursor .getColumnIndex(CashDB.TIME) ) ); startActivity(i) ;//执行跳转 } }) ; } public void selectDB(){ //Cursor cursor = dbReader.query(CashDB.TABLE_NAME, null, null, null, null, null, null); cursor=dbReader .query(CashDB .TABLE_NAME ,null,null,null,null,null,null); adapter = new MyAdapter(this, cursor) ; lv.setAdapter(adapter); } @Override protected void onResume() { super.onResume(); selectDB(); } }
MainActivity:
package bjfu.it.sun.cashbook; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.ListView; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button addbtn; //创建Intent 标识从MainActivity跳转到AddContent private Intent i; private ListView lv; private MyAdapter adapter; private CashDB cashDB; private SQLiteDatabase dbReader; Cursor cursor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } //初始化操作,初始化view public void initView(){ lv = (ListView) findViewById(R.id.list2); addbtn=findViewById(R.id.addbtn);//初始化 addbtn .setOnClickListener(this);//添加监听事件 cashDB=new CashDB(this) ; dbReader = cashDB.getReadableDatabase();//获取数据库读取的权限 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { cursor.moveToPosition(position );//获取当前cursor所指定的当前position //通过intent把当前数据传到另一个页面(详情页面) Intent i=new Intent(MainActivity .this,SelectAct.class) ;//第一个是当前页面,第二个是跳转页面 i.putExtra(CashDB.ID ,cursor.getInt(cursor .getColumnIndex(CashDB.ID) ) );//传递的内容 i.putExtra(CashDB.CONTENT,cursor .getString(cursor .getColumnIndex(CashDB.CONTENT) ) ); i.putExtra(CashDB.TIME,cursor .getString(cursor .getColumnIndex(CashDB.TIME) ) ); i.putExtra(CashDB.COAST,cursor .getString(cursor .getColumnIndex(CashDB.COAST ) ) ); startActivity(i) ;//执行跳转 } }) ; } //添加一个复写方法 @Override public void onClick(View v) { switch (v.getId()) { case R.id.addbtn: i = new Intent(this, AddContent.class); i.putExtra("flag", "1");//传递的内容 startActivity(i);//执行跳转 break; case R.id.button2: i = new Intent(this, list_item.class); i.putExtra("flag", "2");//传递的内容 startActivity(i);//执行跳转 break; } } public void selectDB(){ //Cursor cursor = dbReader.query(CashDB.TABLE_NAME, null, null, null, null, null, null); cursor=dbReader .query(CashDB .TABLE_NAME ,null,null,null,null,null,null); adapter = new MyAdapter(this, cursor) ; lv.setAdapter(adapter); } @Override protected void onResume() { super.onResume(); selectDB(); } /* //创建方法用于添加具体内容 public void addDB(){ ContentValues cv=new ContentValues() ; cv.put(CashDB.CONTENT ,"Hello"); cv.put(CashDB.COAST ,"33"); cv.put(CashDB.TIME ,getTime()); //写入数据库 dbWriter.insert(CashDB.TABLE_NAME ,null,cv) ; } */ /* //获取当前设备的时间 public String getTime(){ SimpleDateFormat format =new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss") ; Date curData=new Date();//实例化 String str =format .format(curData);//获取时间 return str; } */ }
MyAdapter:
package bjfu.it.sun.cashbook; import android.content.Context; import android.database.Cursor; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.TextView; public class MyAdapter extends BaseAdapter { private Context context; private Cursor cursor; private LinearLayout layout; private LayoutInflater mLayoutInflater; public MyAdapter (Context context, Cursor cursor){ this.context = context; this.cursor = cursor; } @Override public int getCount() { return cursor.getCount(); } @Override public Object getItem(int position) { return cursor.getPosition(); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = LayoutInflater.from(context); layout = (LinearLayout) inflater.inflate(R.layout.call, null); TextView contenttv = (TextView) layout.findViewById(R.id.s_content); TextView coasttv = (TextView) layout.findViewById(R.id.coast); TextView timetv = (TextView) layout.findViewById(R.id.s_time); cursor.moveToPosition(position);//cursor需要自己查询 //获取的内容 String content = cursor.getString(cursor.getColumnIndex("context")); String coast = cursor.getString(cursor.getColumnIndex("coast")); String time = cursor.getString(cursor.getColumnIndex("time")); //传递 contenttv.setText(content); coasttv.setText(coast) ; timetv.setText(time); return layout; } }
SelectAct:
package bjfu.it.sun.cashbook; import android.app.Activity; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class SelectAct extends Activity implements View.OnClickListener { private Button s_delete,s_back,s_tijiao; private TextView content,time,coast; private CashDB cashDB; private int id; private SQLiteDatabase dbWrite; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_select); //加载视图 //System .out.println(getIntent() .getIntExtra(CashDB.ID ,0) ) ; s_delete=findViewById(R.id.del ) ;//按钮找id s_back=findViewById(R.id.cancel) ; s_tijiao=findViewById(R.id.tijiao) ; content = (TextView) findViewById(R.id.s_content ); time = (TextView) findViewById(R.id.s_time ); coast = (TextView) findViewById(R.id.coast); cashDB =new CashDB(this);//数据库初始化 dbWrite =cashDB.getWritableDatabase() ;//获得写的权限 /* s_back .setOnClickListener(this) ;//加上监听事件 s_delete .setOnClickListener(this) ; */ //展示内容 content .setText(getIntent().getStringExtra(CashDB .CONTENT) );//内容 time .setText(getIntent().getStringExtra(CashDB .TIME) );//时间 coast .setText(getIntent().getStringExtra(CashDB .COAST ) );//花费 } @Override public void onClick(View v) { switch (v.getId() ){ case R.id.del : deleteDate(); finish(); break; case R.id.cancel: finish(); break; case R.id.tijiao : //通过intent把当前数据传到另一个页面(详情页面) Intent i=new Intent(SelectAct .this,UpaActivity.class) ;//第一个是当前页面,第二个是跳转页面 i.putExtra(CashDB.ID ,getIntent() .getIntExtra(CashDB .ID ,0) );//传递的内容 i.putExtra(CashDB.CONTENT , getIntent().getStringExtra(CashDB .CONTENT) ); i.putExtra(CashDB.COAST , getIntent().getStringExtra(CashDB .COAST ) ); i.putExtra(CashDB.TIME , getIntent().getStringExtra(CashDB .TIME) ); startActivity(i) ;//执行跳转 finish(); break; } } public void deleteDate(){ System .out.println(getIntent() .getIntExtra(CashDB.ID ,0) ) ; dbWrite .delete(CashDB.TABLE_NAME ,"_id="+getIntent() .getIntExtra(CashDB .ID ,0),null ); } }
UpaActivity:
package bjfu.it.sun.cashbook; import android.app.Activity; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class SelectAct extends Activity implements View.OnClickListener { private Button s_delete,s_back,s_tijiao; private TextView content,time,coast; private CashDB cashDB; private int id; private SQLiteDatabase dbWrite; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_select); //加载视图 //System .out.println(getIntent() .getIntExtra(CashDB.ID ,0) ) ; s_delete=findViewById(R.id.del ) ;//按钮找id s_back=findViewById(R.id.cancel) ; s_tijiao=findViewById(R.id.tijiao) ; content = (TextView) findViewById(R.id.s_content ); time = (TextView) findViewById(R.id.s_time ); coast = (TextView) findViewById(R.id.coast); cashDB =new CashDB(this);//数据库初始化 dbWrite =cashDB.getWritableDatabase() ;//获得写的权限 /* s_back .setOnClickListener(this) ;//加上监听事件 s_delete .setOnClickListener(this) ; */ //展示内容 content .setText(getIntent().getStringExtra(CashDB .CONTENT) );//内容 time .setText(getIntent().getStringExtra(CashDB .TIME) );//时间 coast .setText(getIntent().getStringExtra(CashDB .COAST ) );//花费 } @Override public void onClick(View v) { switch (v.getId() ){ case R.id.del : deleteDate(); finish(); break; case R.id.cancel: finish(); break; case R.id.tijiao : //通过intent把当前数据传到另一个页面(详情页面) Intent i=new Intent(SelectAct .this,UpaActivity.class) ;//第一个是当前页面,第二个是跳转页面 i.putExtra(CashDB.ID ,getIntent() .getIntExtra(CashDB .ID ,0) );//传递的内容 i.putExtra(CashDB.CONTENT , getIntent().getStringExtra(CashDB .CONTENT) ); i.putExtra(CashDB.COAST , getIntent().getStringExtra(CashDB .COAST ) ); i.putExtra(CashDB.TIME , getIntent().getStringExtra(CashDB .TIME) ); startActivity(i) ;//执行跳转 finish(); break; } } public void deleteDate(){ System .out.println(getIntent() .getIntExtra(CashDB.ID ,0) ) ; dbWrite .delete(CashDB.TABLE_NAME ,"_id="+getIntent() .getIntExtra(CashDB .ID ,0),null ); } }
activity_add_content.xml:
<?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:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="clip_horizontal" tools:context=".AddContent"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="0dp" android:layout_height="0dp" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <EditText android:id="@+id/context" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="内容" android:inputType="textPersonName" /> <EditText android:id="@+id/coast" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="花费" android:inputType="textPersonName" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:id="@+id/submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="提交" /> <Button android:id="@+id/cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="返回" /> </LinearLayout> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
activity_list_item.xml:
<?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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".list_item"> <ListView android:id="@+id/list2" android:layout_width="match_parent" android:layout_height="match_parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml:
<?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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/addbtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="查看" /> <ListView android:id="@+id/list2" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
activity_select.xml:
<?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:layout_width="match_parent" android:layout_height="wrap_content" tools:context=".SelectAct"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent"> <LinearLayout android:id="@+id/linearLayout3" android:layout_width="match_parent" android:layout_height="150dp" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <TextView android:id="@+id/s_content" android:layout_width="match_parent" android:layout_height="50dp" android:text="TextView" /> <TextView android:id="@+id/coast" android:layout_width="match_parent" android:layout_height="50dp" android:text="TextView" /> <TextView android:id="@+id/s_time" android:layout_width="match_parent" android:layout_height="50dp" android:text="TextView" /> </LinearLayout> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="horizontal"> <Button android:id="@+id/tijiao" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="onClick" android:text="修改" /> <Button android:id="@+id/del" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="onClick" android:text="删除" /> <Button android:id="@+id/cancel" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="onClick" android:text="返回" /> </LinearLayout> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
activity_upa.xml:
<?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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".UpaActivity"> <LinearLayout android:id="@+id/linearLayout" android:layout_width="0dp" android:layout_height="0dp" android:orientation="vertical" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <EditText android:id="@+id/u_context" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="内容" android:inputType="textPersonName" /> <EditText android:id="@+id/u_coast" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="支出或收入" android:inputType="textPersonName" /> <EditText android:id="@+id/u_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="时间" android:inputType="textPersonName" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Button android:id="@+id/u_submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="onClick" android:text="Button" /> <Button android:id="@+id/u_cancle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="onClick" android:text="Button" /> </LinearLayout> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
call.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:ignore="MissingDefaultResource"> <TextView android:id="@+id/s_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> <TextView android:id="@+id/coast" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> <TextView android:id="@+id/s_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout>