Android开发实战——记账本(4)

摘要:
父母亲mCostBeanList.remove(位置);builder.setNegativeButton(“取消”;builder.create().show();returntrue;}returnsuper.onOptionsItemSelected(项);

开发日志(4)——MainActivity

    在MainActivity中编写了几个方法。首先,点击账本的一条记录可以选择删除他,然后重写了fab,使之在点击他后能够添加记录。还写了删除全部记录的方法、

删除单条记录:

        costList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("删除");
                builder.setMessage("是否删除这笔账单?");
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mDatabaseHelper.deleteOne(mCostBeanList.get(position));
                        mCostBeanList.remove(position);
                        mAdapter.notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("Cancel", null);
                builder.create().show();
            }
        });

删除全部记录

    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;
        }
        else if (id == R.id.delete_all) {
            mDatabaseHelper.deleteAlldata();
            mCostBeanList.clear();
            mAdapter.notifyDataSetChanged();
            return true;
        }
        return super.onOptionsItemSelected(item);

    }

添加记录

        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 inflate = LayoutInflater.from(MainActivity.this);
                View viewDialog = inflate.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 data = (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 = data.getYear() + "-" + (data.getMonth() + 1) + "-" + data.getDayOfMonth();
                        mDatabaseHelper.insertCost(costBean);
                        mCostBeanList.add(costBean);
                        mAdapter.notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("Cancel",null);
                builder.create().show();
            }
        });

下面是MainActivity的全部代码:

package com.example.firstapplication;

import android.app.DatePickerDialog;
import android.content.Context;
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.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
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 Context mContext;
    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);
        initCostDate();
        mAdapter = new CostListAdapter(this, mCostBeanList);
        costList.setAdapter(mAdapter);


        costList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("删除");
                builder.setMessage("是否删除这笔账单?");
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mDatabaseHelper.deleteOne(mCostBeanList.get(position));
                        mCostBeanList.remove(position);
                        mAdapter.notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("Cancel", null);
                builder.create().show();
            }
        });


        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 inflate = LayoutInflater.from(MainActivity.this);
                View viewDialog = inflate.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 data = (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 = data.getYear() + "-" + (data.getMonth() + 1) + "-" + data.getDayOfMonth();
                        mDatabaseHelper.insertCost(costBean);
                        mCostBeanList.add(costBean);
                        mAdapter.notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("Cancel",null);
                builder.create().show();
            }
        });
    }

    private void initCostDate() {

       // mDatabaseHelper.deleteAlldata();
        /*for (int i = 0;i < 6; i++) {
            CostBean costBean = new CostBean();
            costBean.costTitle = i + "mock";
            costBean.costDate = "11-11";
            costBean.costMoney = "20";
            mDatabaseHelper.insertCost(costBean);
        }*/
        Cursor cursor = mDatabaseHelper.getAllCostData();
        if (cursor != null) {
            while (cursor.moveToNext()) {
                CostBean costBean = new CostBean();
                int dataColumnIndex = cursor.getColumnIndex("cost_title");
                costBean.costTitle = cursor.getString(dataColumnIndex  + 0 );
                costBean.costDate = cursor.getString(dataColumnIndex + 1);
                costBean.costMoney = cursor.getString(dataColumnIndex + 2);
                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;
        }
        else if (id == R.id.delete_all) {
            mDatabaseHelper.deleteAlldata();
            mCostBeanList.clear();
            mAdapter.notifyDataSetChanged();
            return true;
        }
        return super.onOptionsItemSelected(item);

    }
}

这样,基本的功能都实现了。下面是效果图

Android开发实战——记账本(4)第1张

Android开发实战——记账本(4)第2张

 明天的话打算把线性表的功能实现

免责声明:文章转载自《Android开发实战——记账本(4)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇win10 优化批处理DD-wrt+Wiwiz搭建私人免费(收费)WiFi认证页面+详细的操作教程下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

CDI services--interceptors(拦截器)

1.拦截器综述 拦截器的功能是定义在Java拦截器规范。 拦截器规范定义了三种拦截点:  业务方法拦截, 生命周期回调侦听, 超时拦截(EJB)方法。 在容器的生命周期中进行拦截  public class DependencyInjectionInterceptor { @PostConstruct public void inject...

iOS进阶之多线程

多线程 注意:iOS关于UI的刷新和添加必须在主线程中操作! pthread的创建方法: pthread_t pthread; //第一个参数 线程指针 //第二个参数 线程的一些属性 //第三个参数 函数指针 用于执行方法 //第四个参数 线程中的传值 pthread_create(&pt...

iOS可变字符串的所有操作

可直接把代码复制即可结合输出看结果加深记忆 //创建一个可变字符串 NSMutableString * ms1 = [[NSMutableString alloc]init];     //可以通过类方法来创建 NSMutableString * ms2 = [NSMutableString string];          //用格式化方法创建一个可变...

OC-字符串

     字符串 一.不可变字典:    1.字符串格式化处理 int age = 20; NSString *name = @"陈真"; NSString *info = [NSString stringWithFormat:@"%@今年%d",name,age];         NSLog(@"%@",info);    2.截取字典       ...

怎么使用lombook下的注解

 实际开发中,每个实体类都需要添加get和set方法。 public class UserEntity { private Long id; private String name; private int age; public Long getId() { return id; } p...

Kafka集群启停脚本参考

对kafka-server-start.sh脚本和kafka-server-stop.sh脚本进行二次封装 #! /bin/bash # Kafka代理节点地址, 如果节点较多可以用一个文件来存储 hosts=(dn1 dn2 dn3) # 打印启动分布式脚本信息 mill=`date "+%N"` tdate=`date "+%Y-%m-%d %H:...