实战演练-记账本App(四)
今天完成了收入功能的相关操作
1、创建IncomeBean.java
package com.example.mm; public class IncomeBean { public String incomeTitle; public String incomeDate; public String incomeMoney; }
2、创建数据库
package com.example.mm; 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 DatabaseHelper1 extends SQLiteOpenHelper { public static final String Income_TITLE = "income_title"; public static final String Income_DATE = "income_date"; public static final String Income_MONEY = "income_money"; public static final String IMOOC_Income1 = "imooc_income1"; public DatabaseHelper1(@Nullable Context context) { super(context, "imooc_daily1", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table if not exists imooc_income1(" + "id integer primary key," + "income_title varchar," + "income_date varchar," + "income_money varchar)"); } public void insertIncome(IncomeBean incomeBean) { SQLiteDatabase database = getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put(Income_TITLE, incomeBean.incomeTitle); cv.put(Income_DATE, incomeBean.incomeDate); cv.put(Income_MONEY, incomeBean.incomeMoney); database.insert(IMOOC_Income1, null, cv); } public Cursor getAllCostData() { SQLiteDatabase database = getWritableDatabase(); return database.query(IMOOC_Income1, null, null, null, null, null, Income_DATE + " ASC"); } public void deleteAllData() { SQLiteDatabase database = getWritableDatabase(); database.delete(IMOOC_Income1, null, null); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
3、IncomeListAdapter.java
package com.example.mm; 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 IncomeListAdapter extends BaseAdapter { private List<IncomeBean> mList; public Context mContext; private LayoutInflater mlayoutInflater; public IncomeListAdapter(Context context, List<IncomeBean> 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.mTvIncomeTitle = convertView.findViewById(R.id.tv_title); viewHolder.mTvIncomeDate = convertView.findViewById(R.id.tv_date); viewHolder.mTvIncomeMoney = convertView.findViewById(R.id.tv_cost); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } IncomeBean bean = mList.get(position); viewHolder.mTvIncomeTitle.setText(bean.incomeTitle); viewHolder.mTvIncomeDate.setText(bean.incomeDate); viewHolder.mTvIncomeMoney.setText(bean.incomeMoney); return convertView; } private static class ViewHolder { public TextView mTvIncomeTitle; public TextView mTvIncomeDate; public TextView mTvIncomeMoney; } }
4、SkipActivity1.java
package com.example.mm; import android.app.DatePickerDialog; import android.content.DialogInterface; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; 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.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class SkipActivity1 extends AppCompatActivity { private List<IncomeBean> mIncomeBeanList; private DatabaseHelper1 mDatabaseHelper; private IncomeListAdapter mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mIncomeBeanList = new ArrayList<>(); mDatabaseHelper = new DatabaseHelper1(this); ListView incomeList = (ListView) findViewById(R.id.lv_main); mAdapter = new IncomeListAdapter(this, mIncomeBeanList); incomeList.setAdapter(mAdapter); initIncomeData(); //删除所有 Button del = (Button) findViewById(R.id.del); del.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { creatdialog(); } }); //退出 Button tui = (Button) findViewById(R.id.tui); tui.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog(); } }); //添加 Button btn2 = (Button) findViewById(R.id.btn2); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(SkipActivity1.this); LayoutInflater inflater = LayoutInflater.from(SkipActivity1.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("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { IncomeBean incomeBean = new IncomeBean(); incomeBean.incomeTitle = title.getText().toString(); incomeBean.incomeMoney = money.getText().toString(); incomeBean.incomeDate = date.getYear() + "-" + (date.getMonth() + 1) + "-" + date.getDayOfMonth(); mDatabaseHelper.insertIncome(incomeBean); mIncomeBeanList.add(incomeBean); mAdapter.notifyDataSetChanged(); } }); builder.setNegativeButton("取消", null); builder.create().show(); } }); } private void initIncomeData() { //mDatabaseHelper.deleteAllData(); // for (int i=0;i<6;i++) { // CostBean costBean=new CostBean(); // costBean.costTitle=i+"mock"; // costBean.costMoney="20"; // mDatabaseHelper.insertCost(costBean); // } Cursor cursor = mDatabaseHelper.getAllCostData(); if ( cursor != null ) { while (cursor.moveToNext()) { IncomeBean incomeBean = new IncomeBean(); incomeBean.incomeTitle = cursor.getString(cursor.getColumnIndex("income_title")); incomeBean.incomeDate = cursor.getString(cursor.getColumnIndex("income_date")); incomeBean.incomeMoney = cursor.getString(cursor.getColumnIndex("income_money")); mIncomeBeanList.add(incomeBean); } cursor.close(); } } private void creatdialog() { AlertDialog.Builder b = new AlertDialog.Builder(SkipActivity1.this); //设置提示框内容 b.setMessage("确认删除?"); //设置标题栏 b.setTitle("提示"); b.setPositiveButton("确认", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { mDatabaseHelper.deleteAllData(); finish(); Toast.makeText(SkipActivity1.this, "删除成功", Toast.LENGTH_SHORT).show(); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(SkipActivity1.this, "删除失败", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); b.create().show(); } private void dialog() { AlertDialog.Builder builder = new AlertDialog.Builder(SkipActivity1.this); builder.setMessage("确认要退出么?"); builder.setTitle("提示"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); Toast.makeText(SkipActivity1.this, "退出成功", Toast.LENGTH_SHORT).show(); finish(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }); builder.show(); } @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); } }
其效果和支出的相关操作一样,就不附效果图了。