赞
踩
今天通过观看视频,根据老师所讲内容课程介绍和项目创建_Android记账本-慕课网,编译代码。实现了Android Studio记账本里面的增加功能
源代码如下:
CostBean.java:
- package com.example.family;
-
- import java.io.Serializable;
-
- public class CostBean implements Serializable{
- public String costTitle;
- public String costDate;
- public String costMoney;
-
- public String getCostTitle() {
- return costTitle;
- }
-
- public void setCostTitle(String costTitle) {
- this.costTitle = costTitle;
- }
-
- public String getCostDate() {
- return costDate;
- }
-
- public void setCostDate(String costDate) {
- this.costDate = costDate;
- }
-
- public String getCostMoney() {
- return costMoney;
- }
-
- public void setCostMoney(String costMoney) {
- this.costMoney = costMoney;
- }
-
- @Override
- public String toString() {
- return "CostBean{" +
- "costTitle='" + costTitle + '\'' +
- ", costDate='" + costDate + '\'' +
- ", costMoney='" + costMoney + '\'' +
- '}';
- }
- }
CostListAdapter.java:
- package com.example.family;
-
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.TextView;
-
- import java.util.List;
-
- public class CostListAdapter extends BaseAdapter{
-
- private List<CostBean> mList;
- private Context mContext;
- private LayoutInflater mLayoutInflater;
-
- public CostListAdapter(Context context,List<CostBean> list){
- mContext=context;
- mList=list;
- mLayoutInflater=LayoutInflater.from(context);
- }
- @Override
- public int getCount() {
- return mList.size();
- }
-
- @Override
- public Object getItem(int position) {
- return mList.get(position);
- }
-
- @Override
- public long getItemId(int position) {
- return position;
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder viewHolder;
- if(convertView==null){
- viewHolder=new ViewHolder();
- convertView=mLayoutInflater.inflate(R.layout.list_item,null);
- viewHolder.mTvCostTitle=(TextView)convertView.findViewById(R.id.tv_title);
- viewHolder.mTvCostDate=(TextView)convertView.findViewById(R.id.tv_date);
- viewHolder.mTvCostMoney=(TextView)convertView.findViewById(R.id.tv_cost);
- convertView.setTag(viewHolder);
- }else{
- viewHolder=(ViewHolder) convertView.getTag();
- }
- CostBean bean=mList.get(position);
- viewHolder.mTvCostTitle.setText(bean.costTitle);
- viewHolder.mTvCostDate.setText(bean.costDate);
- viewHolder.mTvCostMoney.setText(bean.costMoney);
- return convertView;
- }
-
- private static class ViewHolder{
- public TextView mTvCostTitle;
- public TextView mTvCostDate;
- public TextView mTvCostMoney;
- }
- }
DatabaseHelper.java:
- package com.example.family;
-
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
-
- import androidx.annotation.Nullable;
-
-
- public class DatabaseHelper extends SQLiteOpenHelper {
-
- public static final String COST_MONEY = "cost_money";
- public static final String COST_DATE = "cost_date";
- public static final String COST_TITLE = "cost_title";
- public static final String FAMILY = "family";
-
- public DatabaseHelper(@Nullable Context context) {
- super(context, FAMILY, null, 1);
- }
-
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL("create table if not exists family("+
- "id integer primary key,"+
- "cost_title varchar,"+
- "cost_date varchar,"+
- "cost_money varchar)");
- }
-
- public void insertCost(CostBean costBean){
- SQLiteDatabase database=getWritableDatabase();
- ContentValues cv=new ContentValues();
- cv.put(COST_TITLE,costBean.costTitle);
- cv.put(COST_DATE,costBean.costDate);
- cv.put(COST_MONEY,costBean.costMoney);
- database.insert(FAMILY,null,cv);
- }
-
- public Cursor getAllCostData(){
- SQLiteDatabase database=getWritableDatabase();
- return database.query(FAMILY,null,null,null,null,null,COST_DATE+" ASC");
- }
-
- public void deleteAllData(){
- SQLiteDatabase database=getWritableDatabase();
- database.delete(FAMILY,null,null);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
-
- }
- }
MainActivity.java:
- package com.example.family;
-
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.database.Cursor;
- import android.os.Bundle;
- import com.google.android.material.floatingactionbutton.FloatingActionButton;
- import com.google.android.material.snackbar.Snackbar;
-
- import androidx.appcompat.app.AlertDialog;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.appcompat.widget.Toolbar;
-
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.DatePicker;
- import android.widget.EditText;
- import android.widget.ListView;
-
- import java.io.Serializable;
- import java.util.ArrayList;
- import java.util.List;
- public class MainActivity extends AppCompatActivity {
-
- private List<CostBean> mCostBeanList;
- private DatabaseHelper mDatabaseHelper;
- private CostListAdapter mAdapter;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Toolbar toolbar = findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
-
- mDatabaseHelper=new DatabaseHelper(this);
- mCostBeanList=new ArrayList<>();
- ListView costList=(ListView)findViewById(R.id.lv_main);
- initCostData();
- mAdapter =new CostListAdapter(this,mCostBeanList);
- costList.setAdapter(mAdapter);
-
-
- FloatingActionButton fab = findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
- LayoutInflater inflater=LayoutInflater.from(MainActivity.this);
- View viewDialog=inflater.inflate(R.layout.new_cost_data,null);
- final EditText title=(EditText)viewDialog.findViewById(R.id.et_cost_title);
- final EditText money=(EditText)viewDialog.findViewById(R.id.et_cost_money);
- final DatePicker date=(DatePicker) viewDialog.findViewById(R.id.dp_cost_date);
- builder.setView(viewDialog);
- builder.setTitle("New Cost");
- builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- CostBean costBean=new CostBean();
- costBean.costTitle=title.getText().toString();
- costBean.costMoney=money.getText().toString();
- costBean.costDate=date.getYear() + "-" + (date.getMonth()+1) + "-" +
- date.getDayOfMonth();
- mDatabaseHelper.insertCost(costBean);
- mCostBeanList.add(costBean);
- mAdapter.notifyDataSetChanged();
- }
- });
- builder.setNegativeButton("Cancle",null);
- builder.create().show();
- }
- });
- }
-
- private void initCostData() {
- // mDatabaseHelper.deleteAllData();
- // for (int i=0;i<6;i++) {
- // CostBean costBean=new CostBean();
- // costBean.costTitle=i+"mock";
- // costBean.costDate="2020-2-4";
- // costBean.costMoney="20";
- // mDatabaseHelper.insertCost(costBean);
- // }
- Cursor cursor=mDatabaseHelper.getAllCostData();
- if(cursor!=null){
- while(cursor.moveToNext()){
- CostBean costBean=new CostBean();
- costBean.costTitle=cursor.getString(cursor.getColumnIndex("cost_title"));
- costBean.costDate=cursor.getString(cursor.getColumnIndex("cost_date"));
- costBean.costMoney=cursor.getString(cursor.getColumnIndex("cost_money"));
- mCostBeanList.add(costBean);
- }
- cursor.close();
- }
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- int id = item.getItemId();
- if (id == R.id.action_chart) {
- Intent intent=new Intent(MainActivity.this,ChartsActivity.class);
- intent.putExtra("cost_list", (Serializable) mCostBeanList);
- startActivity(intent);
- return true;
- }
-
-
- return super.onOptionsItemSelected(item);
- }
- }
content_main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- app:layout_behavior="@string/appbar_scrolling_view_behavior"
- tools:showIn="@layout/activity_main"
- tools:context=".MainActivity">
-
- <ListView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/lv_main"/>
-
- </RelativeLayout>
list_item.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="80dp">
- <TextView
- android:id="@+id/tv_title"
- android:layout_width="150dp"
- android:layout_height="80dp"
- android:layout_marginLeft="10dp"
- android:layout_alignParentLeft="true"
- android:gravity="center"
- android:singleLine="true"
- android:textSize="35sp"
- android:ellipsize="marquee"
- android:text="costTitle"/>
-
- <TextView
- android:id="@+id/tv_date"
- android:layout_width="100dp"
- android:layout_height="80dp"
- android:gravity="center"
- android:textSize="20sp"
- android:layout_marginLeft="15dp"
- android:layout_toRightOf="@+id/tv_title"
- android:text="costDate"/>
- <TextView
- android:id="@+id/tv_cost"
- android:layout_width="100dp"
- android:layout_height="80dp"
- android:gravity="center"
- android:layout_alignParentRight="true"
- android:layout_marginRight="20dp"
- android:textSize="30sp"
- android:text="30"/>
-
- </RelativeLayout>
new_cost_data.xml:
- <?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:gravity="center"
- android:layout_height="match_parent">
-
-
- <EditText
- android:id="@+id/et_cost_title"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_margin="4dp"
- android:hint="Cost Title"/>
-
- <EditText
- android:id="@+id/et_cost_money"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="4dp"
- android:hint="Cost Money"/>
-
- <DatePicker
- android:id="@+id/dp_cost_date"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="4dp"
- android:datePickerMode="spinner"
- android:calendarViewShown="false"/>
-
-
- </LinearLayout>
点击右下角“邮件”按钮实现增加。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。