个人冲刺(五)——家庭记账本
冲刺任务:完成CostBean.java和MainActivity.java
MainActivity.java
package com.example.jizhangben; import android.content.DialogInterface; import android.database.Cursor; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.SearchView; import android.support.v7.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.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private List<CostBean> mCostBeanList; private DatabaseHelper mDatabaseHelper; private CostListAdapter adapter; @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); mCostBeanList=new ArrayList<>(); ListView costList= (ListView) findViewById(R.id.lv_main); initCostData(); adapter = new CostListAdapter(this, mCostBeanList); costList.setAdapter(adapter); 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 inflate=LayoutInflater.from(MainActivity.this); View viewDialog=inflate.inflate(R.layout.new_cost_data,null); final EditText tltle= (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("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { CostBean costBean=new CostBean(); costBean.CostTitle=tltle.getText().toString(); costBean.CostMoney=money.getText().toString(); costBean.CostDate=date.getYear()+"-"+(date.getMonth()+1)+"-"+ date.getDayOfMonth(); mDatabaseHelper.insertCost(costBean); mCostBeanList.add(costBean); adapter.notifyDataSetChanged(); } }); builder.setNegativeButton("取消",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="11-11"; // costBean.CostMoney="21"; // 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) { // 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_settings) { return true; } return super.onOptionsItemSelected(item); } }
CostBean.java
package com.example.jizhangben;
public class CostBean {
public String CostTitle;
public String CostDate;
public String CostMoney;
}