每日博客

1.0自“出厂”后家人一直在用,最近出了点问题被家人连续几日吐槽催促我解决,特有了以下2.0.

家庭记账本2.0

 

 

 

 

 

 

 

 

package com.example.myapp0;

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 ListAdapter extends BaseAdapter
{
    List<costList> mList;
    public ListAdapter(List<costList>list){mList=list;}
    @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)
    {
        View view=mLayoutInflater.inflate(R.layout.list_item,null);
        costList item=mList.get(position);
        TextView tv_title=view.findViewById(R.id.title);
        TextView tv_date=view.findViewById(R.id.date);
        TextView tv_money=view.findViewById(R.id.money);
        tv_title.setText(mList.get(position).getTitle());
        tv_date.setText(mList.get(position).getDate());
        tv_money.setText(mList.get(position).getMoney());
        return view;
    }
    private List<costList>getmList;
    private LayoutInflater mLayoutInflater;
    public ListAdapter(Context context,List<costList>list)
    {
        mList=list;
        mLayoutInflater=LayoutInflater.from(context);
    }
}

 

 

package com.example.myapp0;//2.0

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity
{
    private DBHelper helper;
    private  ListView listView;
    private  ImageButton Add;
    private List<costList>list;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }
    private void initData()
    {
        list=new ArrayList<>();
        SQLiteDatabase db=helper.getReadableDatabase();
        Cursor cursor=db.query("account",null,null,null,null, null,null);
        while (cursor.moveToNext())
        {
            costList clist=new costList();
            clist.set_id(cursor.getString(cursor.getColumnIndex("_id")));
            clist.setTitle(cursor.getString(cursor.getColumnIndex("Title")));
            clist.setDate(cursor.getString(cursor.getColumnIndex("Date")));
            clist.setMoney(cursor.getString(cursor.getColumnIndex("Money")));
            list.add(clist);
        }
        listView.setAdapter(new ListAdapter(this,list));
        db.close();
    }
    private void initView()
    {
        helper=new DBHelper(MainActivity.this);
        listView = findViewById(R.id.list);
        Add=findViewById(R.id.add);
    }
    public void addAccount(View view)
    {
        Intent intent=new Intent(MainActivity.this,new_cost.class);
        startActivityForResult(intent,1);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1&&resultCode==1){ this.initData();}
    }
}

 

 

package com.example.myapp0;

import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

public class new_cost extends AppCompatActivity
{
    private DBHelper helper;
    private EditText et_cost_title;
    private EditText et_cost_money;
    private DatePicker dp_cost_date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_cost);
        initView();
    }
    private void initView()
    {
        helper = new DBHelper(new_cost.this);
        et_cost_title = findViewById(R.id.title);
        et_cost_money = findViewById(R.id.money);
        dp_cost_date = findViewById(R.id.date);
    }
    public void okButton(View view)
    {
        String titleStr = et_cost_title.getText().toString().trim();
        String moneyStr = et_cost_money.getText().toString().trim();
        String dateStr = dp_cost_date.getYear() + "-" + (dp_cost_date.getMonth() + 1) + "-" + dp_cost_date.getDayOfMonth();
        if ("".equals(moneyStr))
        {
            Toast toast = Toast.makeText(this, "请填写金额", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
        else {
            SQLiteDatabase db = helper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("Title", titleStr);
            values.put("Money", moneyStr);
            values.put("Date", dateStr);
            long account = db.insert("account", null, values);
            if (account > 0)
            {
                Toast toast = Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                setResult(1);
                finish();
            }
            else {
                Toast toast = Toast.makeText(this, "请重试", Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                db.close();
            }
            setResult(1);
            finish();
        }
    }
}

 

 

 

posted @ 2021-10-17 19:22  谦寻  阅读(104)  评论(0编辑  收藏  举报