实战演练-记账本App(三)

 继上一篇内容,今天继续完成家庭记账本app的开发。

今天主要完成了有关支出功能的相关操作,实现了新的支出添加、删除、退出功能

一、数据库等创建

1、创建CostBean.java

package com.example.mm;

public class CostBean {
    public String costTitle;
    public String costDate;
    public String costMoney;
}

2、创建CostListAdapter.java继承BaseAdapter

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 CostListAdapter extends BaseAdapter {


    private List<CostBean> mList;
    public Context mContext;
    private LayoutInflater mlayoutInflater;


    public CostListAdapter(Context context,List<CostBean>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.mTvCostTitle=convertView.findViewById(R.id.tv_title);
            viewHolder.mTvCostDate=convertView.findViewById(R.id.tv_date);
            viewHolder.mTvCostMoney=convertView.findViewById(R.id.tv_cost);
            convertView.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder)convertView.getTag();
        }
        CostBean bean=mList.get(position);
        viewHolder.mTvCostTitle.setText(bean.costTitle);
        viewHolder.mTvCostDate.setText(bean.costDate);
        viewHolder.mTvCostMoney.setText(bean.costMoney);
        return convertView;
    }

    private static class ViewHolder{
        public TextView mTvCostTitle;
        public TextView mTvCostDate;
        public TextView mTvCostMoney;
    }
}

3、创建DatabaseHelper.java文件 也就是数据库的创建以及相关操作

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 DatabaseHelper extends SQLiteOpenHelper {


    public static final String COST_TITLE = "cost_title";
    public static final String COST_DATE = "cost_date";
    public static final String COST_MONEY = "cost_money";
    public static final String IMOOC_COST = "imooc_cost";

    public DatabaseHelper(@Nullable Context context) {
        super(context, "imooc_daily",null , 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table if not exists imooc_cost("+
                "id integer primary key,"+
                "cost_title varchar,"+
                "cost_date varchar,"+
                "cost_money varchar)");
    }
//数据录入
    public void insertCost(CostBean costBean){
        SQLiteDatabase database=getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put(COST_TITLE,costBean.costTitle);
        cv.put(COST_DATE,costBean.costDate);
        cv.put(COST_MONEY,costBean.costMoney);
        database.insert(IMOOC_COST, null, cv);
    }
//显示所有数据
    public Cursor getAllCostData(){
        SQLiteDatabase database=getWritableDatabase();
        return database.query(IMOOC_COST,null,null,null,null,null,COST_DATE +" ASC");
    }
//删除数据
    public void deleteAllData(){
        SQLiteDatabase database=getWritableDatabase();
        database.delete(IMOOC_COST,null,null);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

4、接下来就要在SkipActivity.java文件进行相关的相关的操作了。

package com.example.mm;

import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
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 SkipActivity 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);
mCostBeanList = new ArrayList<>();
mDatabaseHelper = new DatabaseHelper(this);
ListView costList = (ListView) findViewById(R.id.lv_main);
mAdapter = new CostListAdapter(this, mCostBeanList);
costList.setAdapter(mAdapter);
initCostData();
//删除所有
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(SkipActivity.this);
LayoutInflater inflater = LayoutInflater.from(SkipActivity.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) {
CostBean costBean = new CostBean();
costBean.costTitle = title.getText().toString();
costBean.costMoney = money.getText().toString();
costBean.costDate = date.getYear() + "-" + (date.getMonth() + 1) + "-" + date.getDayOfMonth();
mDatabaseHelper.insertCost(costBean);
mCostBeanList.add(costBean);
mAdapter.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.costMoney="20";
// 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();
}
}

private void creatdialog() {
AlertDialog.Builder b = new AlertDialog.Builder(SkipActivity.this);

//设置提示框内容
b.setMessage("确认删除?");
//设置标题栏
b.setTitle("提示");
b.setPositiveButton("确认", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
mDatabaseHelper.deleteAllData();
finish();
Toast.makeText(SkipActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(SkipActivity.this, "删除失败", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
b.create().show();
}

private void dialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(SkipActivity.this);
builder.setMessage("确认要退出么?");
builder.setTitle("提示");
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {

dialog.dismiss();
Toast.makeText(SkipActivity.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);
}
}

效果图:

 

 

 

 以上就是今天所完成的内容。。。

posted @ 2020-01-23 22:00  MoooJL  阅读(376)  评论(0编辑  收藏  举报