家庭小账本——主函数以及其他方法的完善

  前几天进行到数据库的编写及测试之后,遇到了一些问题,之后从新学习了一遍有关数据库的知识,学完之后继续完成小账本的开发。

完成  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:singleLine="true"
        android:ellipsize="marquee"
        android:textSize="35sp"
        android:gravity="center"
        android:text="costTitle"/>

    <TextView
        android:id="@+id/tv_date"
        android:layout_width="wrap_content"
        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="wrap_content"
        android:layout_height="80dp"
        android:gravity="center"
        android:layout_alignParentRight="true"
        android:textSize="30sp"
        android:layout_marginRight="20dp"
        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="wrap_content"
        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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:id="@+id/dp_cost_date"
        android:datePickerMode="spinner"
        android:calendarViewShown="false"/>


</LinearLayout>

最后进行主函数的编写

package com.example.ok;

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> mCostBeenList;
    private DatabaseHelper mDatabaseHelper;
    private CostListAdapter mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mDatabaseHelper=new DatabaseHelper(this);

        mCostBeenList=new ArrayList<>();
        initCostData();
        ListView costList= (ListView) findViewById(R.id.lv_main);

        mAdapter=new CostListAdapter(this,mCostBeenList);
        costList.setAdapter(mAdapter);

        FloatingActionButton fab = (FloatingActionButton) 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("新的花费");
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        CostBean coseBean=new CostBean();
                        coseBean.costTitle=title.getText().toString();
                        coseBean.costMoney=money.getText().toString();
                        coseBean.costDate=date.getYear()+"-"+(date.getMonth()+1)+"-"+date.getDayOfMonth();
                        mDatabaseHelper.insertCost(coseBean);
                        mCostBeenList.add(coseBean);

                        mAdapter.notifyDataSetChanged();
                    }
                });

                builder.setNegativeButton("Cancel",null);

                //勿忘!!!
                builder.create().show();
            }
        });

        //打开SQLiteStudio
        //SQLiteStudioService.instance().start(this);

    }

    private void initCostData() {
        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"));
                mCostBeenList.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,ChartActivity.class);
//          intent.putExtra("cost_list", (Serializable) mCostBeenList);
//           startActivity(intent);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        //SQLiteStudioService.instance().stop();
        super.onDestroy();
    }
}

测试结果为  

家庭小账本——主函数以及其他方法的完善                                                                家庭小账本——主函数以及其他方法的完善

相关推荐