ADD file via upload

master
pyowf4gx3 3 years ago
parent 3fb5324fed
commit 5fb43891d8

@ -0,0 +1,193 @@
package com.example.account_book;
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.AdapterView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
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);
initCostDate();
mAdapter = new CostListAdapter(this, mCostBeanList);
costList.setAdapter(mAdapter);
//单击每一个item实现删除功能
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("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mDatabaseHelper.toDelete(mCostBeanList.get(position));
mCostBeanList.remove(position);
mAdapter.notifyDataSetChanged();//通知list_view刷新
}
});
builder.setNegativeButton("取消",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 inflater = LayoutInflater.from(MainActivity.this);
View viewDialog = inflater.inflate(R.layout.new_cost, null);//联系new_cost布局才能使用布局中的数据元素
final EditText title = (EditText) viewDialog.findViewById(R.id.ct_cost_title);
final EditText money = (EditText) viewDialog.findViewById(R.id.ct_cost_money);
final DatePicker date = (DatePicker) viewDialog.findViewById(R.id.ct_cost_date);
builder.setView(viewDialog);//将布局设置给dialog
builder.setTitle("新建支出");//dialog的标题
builder.setPositiveButton("确定", 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();
//储存date时特别要注意我们后面要根据costDate排序所以要将其标准化
costBean.costDate = date.getYear()+"-";
if(date.getMonth()+1<10) costBean.costDate+="0";
costBean.costDate+=(date.getMonth()+1)+"-";
if(date.getDayOfMonth()<10) costBean.costDate+="0";
costBean.costDate+=date.getDayOfMonth();
//当输入为空时提醒
if("".equals(costBean.costTitle) || "".equals(costBean.costMoney) || "".equals(costBean.costDate))
{
Toast.makeText(MainActivity.this,"信息不完整",Toast.LENGTH_SHORT).show();
return;
}
if(costBean.costMoney.length()>4)
{
Toast.makeText(MainActivity.this,"金额过大",Toast.LENGTH_SHORT).show();
return;
}
mDatabaseHelper.insertCost(costBean);//将数据插入数据库
mCostBeanList.add(costBean);//将list改变才能刷新
mAdapter.notifyDataSetChanged();//通知list_view刷新这样才能显示新的账单
}
});
builder.setNegativeButton("取消", null);//取消时退出
builder.create().show();//显示dialog即自定义的布局
}
});
}
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();//数据库中插入所有数据并显示到list_view中
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) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
{
//noinspection SimplifiableIfStatement
if (id == R.id.action_chart) {
Intent intent = new Intent(MainActivity.this, ChartsActivity.class);
intent.putExtra("cost_list", (Serializable) mCostBeanList);//将消费金额序列化传给chart
startActivity(intent);
return true;
}
// else if (id == R.id.action_about){
// Intent intent_a = new Intent(getApplicationContext(),AboutActivity.class);
// startActivity(intent_a);
// return true;
//
// }
else if (id == R.id.action_about){
Intent intent_a = new Intent(getApplicationContext(),web.class);
startActivity(intent_a);
return true;
}
else if (id == R.id.anction_delete_all){
mDatabaseHelper.deleteAllData();
mCostBeanList.clear();
mAdapter.notifyDataSetChanged();
return true;
}
}
return super.onOptionsItemSelected(item);
}
}
Loading…
Cancel
Save