安卓开发实战-记账本APP(四)
今天实现的内容有:添加账本信息,个人头像的切换,密码的修改,退出登录。
添加账本信息有三个功能:
①记一笔支出项目
②记一笔收入项目
③清空所有项目
在此期间遇到的困难有:Activity与Fragment之间数值的传递问题,我利用Bundle对象来进行传值,但是布局文件中fragment里的name=“Account_Fragment”,这里暂时没有想到如何传值。
因此暂时将name=”Find_Message”这个还未改变的一个fragment,当再次点击Account图标时,就可以传进去参数。
未解决的事:
①记完账本后,按日期排序一直不对
②初始Fragment没办法传参
明天要做的事:
先将上述问题解决,在设置长按项目进行单独删除。
统计总的收入与支出的对比
找到合适的图标进行替换
学习hellochart,将数据以饼状图展示。
今日成果:
点击更换头像:会有几个头像供选择
当点击后自动跳回个人信息界面:
其次是更换密码:需要输入原密码与新密码
再者就是记账的页面:有支出,收入,删除所有,支出的会显示红色,收入的会显示绿色
当点击收入时,会弹出一个框来进行记账
同理,当点击支出时,也会弹出一个框
当点击删除所有时,会将所有账目全部删除。
Account_Fragment:记账的Fragment
package com.example.countbook; import android.app.AlertDialog; import android.app.Fragment; import android.content.DialogInterface; import android.database.Cursor; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.ListView; import android.widget.RadioButton; import androidx.annotation.Nullable; import java.util.ArrayList; import java.util.List; public class Account_Fragment extends Fragment { private List<Account> list; private AccountOperator accountOperator; private AccountAdapter accountAdapter; Button btn_shouru; Button btn_zhichu; Button btn_delete; Bundle bundle; String username; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); bundle=getArguments(); username=bundle.getString("username"); //Log.i("username",username); } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { View view=inflater.inflate(R.layout.account_fragment,null); btn_shouru=(Button)view.findViewById(R.id.btn_shouru); btn_zhichu=(Button)view.findViewById(R.id.btn_zhichu); btn_delete=(Button)view.findViewById(R.id.btn_delete); accountOperator=new AccountOperator(view.getContext()); list=new ArrayList<>(); ListView AccountList=(ListView)view.findViewById(R.id.lv_main); inin(username); accountAdapter=new AccountAdapter(view.getContext(),list); AccountList.setAdapter(accountAdapter); btn_zhichu.setOnClickListener(l); btn_shouru.setOnClickListener(l); btn_delete.setOnClickListener(l); return view; } View.OnClickListener l=new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_zhichu: AlertDialog.Builder builder=new AlertDialog.Builder(getView().getContext()); LayoutInflater inflater=LayoutInflater.from(getView().getContext()); View viewDialog=inflater.inflate(R.layout.new_cost_data,null); final EditText content=(EditText)viewDialog.findViewById(R.id.et_cost_content); final DatePicker data=(DatePicker)viewDialog.findViewById(R.id.dp_cost_data); final EditText money=(EditText)viewDialog.findViewById(R.id.et_cost_money); final RadioButton rb1=(RadioButton)viewDialog.findViewById(R.id.rb1); final RadioButton rb2=(RadioButton)viewDialog.findViewById(R.id.rb2); final RadioButton rb3=(RadioButton)viewDialog.findViewById(R.id.rb3); final RadioButton rb4=(RadioButton)viewDialog.findViewById(R.id.rb4); builder.setView(viewDialog); builder.setTitle("New Cost"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Account bean=new Account(); bean.author=username; bean.content=content.getText().toString(); bean.money=Integer.parseInt(money.getText().toString()); bean.date=data.getYear()+"-"+(data.getMonth()+1)+"-"+data.getDayOfMonth(); if(rb1.isChecked()){ bean.title=rb1.getText().toString(); }else if(rb2.isChecked()){ bean.title=rb2.getText().toString(); }else if(rb3.isChecked()){ bean.title=rb3.getText().toString(); }else if(rb4.isChecked()){ bean.title=rb4.getText().toString(); } accountOperator.insert(bean); list.add(bean); accountAdapter.notifyDataSetChanged(); } }); builder.setNegativeButton("Cancel",null); builder.create().show(); break; case R.id.btn_shouru: AlertDialog.Builder builder2=new AlertDialog.Builder(getView().getContext()); LayoutInflater inflater2=LayoutInflater.from(getView().getContext()); View viewDialog2=inflater2.inflate(R.layout.new_income_data,null); final EditText content2=(EditText)viewDialog2.findViewById(R.id.et_income_content); final DatePicker data2=(DatePicker)viewDialog2.findViewById(R.id.dp_income_data); final EditText money2=(EditText)viewDialog2.findViewById(R.id.et_income_money); final RadioButton frb1=(RadioButton)viewDialog2.findViewById(R.id.rb1); final RadioButton frb2=(RadioButton)viewDialog2.findViewById(R.id.rb2); final RadioButton frb3=(RadioButton)viewDialog2.findViewById(R.id.rb3); builder2.setView(viewDialog2); builder2.setTitle("New InCome"); builder2.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Account bean=new Account(); bean.author=username; bean.content=content2.getText().toString(); bean.money=Integer.parseInt(money2.getText().toString()); bean.date=data2.getYear()+"-"+(data2.getMonth()+1)+"-"+data2.getDayOfMonth(); if(frb1.isChecked()){ bean.title=frb1.getText().toString(); }else if(frb2.isChecked()){ bean.title=frb2.getText().toString(); }else if(frb3.isChecked()){ bean.title=frb3.getText().toString(); } accountOperator.insert(bean); list.add(bean); accountAdapter.notifyDataSetChanged(); } }); builder2.setNegativeButton("Cancel",null); builder2.create().show(); break; case R.id.btn_delete: accountOperator.deleteall(username); list.clear(); accountAdapter.notifyDataSetChanged(); break; } } }; private void inin(String author) { Cursor cursor= (Cursor) accountOperator.findall(author); if(cursor!=null){ while(cursor.moveToNext()){ Account bean=new Account(); bean.content=cursor.getString(cursor.getColumnIndex("content")); bean.money=cursor.getInt(cursor.getColumnIndex("money")); bean.date=cursor.getString(cursor.getColumnIndex("date")); bean.title=cursor.getString(cursor.getColumnIndex("title")); list.add(bean); } cursor.close(); } } }