2024.3.20(周三)进度

完成计网第二章作业,完成Android本地库连接增删改查

代码量:三个.java文件

package com.hui.myapplication.frag_record;

import android.inputmethodservice.KeyboardView;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.hui.myapplication.R;
import com.hui.myapplication.db.AccountBean;
import com.hui.myapplication.db.TypeBean;
import com.hui.myapplication.utils.BeiZhuDialog;
import com.hui.myapplication.utils.KeyBoardUtils;
import com.hui.myapplication.utils.SelectTimeDialog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
记录当前页面当中的支出模块
*/
public abstract class BaseRecordFragment extends Fragment implements View.OnClickListener {


KeyboardView keyboardView;
EditText moneyEt;
ImageView typeIv;
TextView typeTv,beizhuTv,timeTv;
GridView typeGv;
List<TypeBean>typeList;
TypeBaseAdapter adapter;
AccountBean accountBean; //将需要插入到记账本当中的数据保存成对象的形式

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
accountBean=new AccountBean(); //创建对象
accountBean.setTypename("其他");
accountBean.setsImageId(R.mipmap.ic_qita_fs);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_outcome,container,false);
initView(view);
setInitTime();
//给GridView填充数据的方法
loadDataToGv();
setGVListener(); //设置GridView每一项的点击事件
return view;
}
/*获取当前时间,显示在timeTv上*/
private void setInitTime(){
Date date=new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
String time=sdf.format(date);
timeTv.setText(time);
accountBean.setTime(time);
Calendar calendar=Calendar.getInstance();
int year= calendar.get(Calendar.YEAR);
int month=calendar.get(Calendar.MONTH)+1;
int day=calendar.get(Calendar.DAY_OF_MONTH);
accountBean.setYear(year);
accountBean.setMonth(month);
accountBean.setDay(day);
}
/*设置GridView每一项的点击事件*/
private void setGVListener(){
typeGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapter.selectPos=position;
adapter.notifyDataSetInvalidated(); //提示绘制发生变化了
TypeBean typeBean=typeList.get(position);
String typename=typeBean.getTypename();
typeTv.setText(typename);
accountBean.setTypename(typename);
int simageId=typeBean.getSimageId();
typeIv.setImageResource(simageId);
accountBean.setsImageId(simageId);
}
});
}
/*给GridView填充数据的方法*/
public void loadDataToGv(){
typeList=new ArrayList<>();
adapter = new TypeBaseAdapter(getContext(),typeList);
typeGv.setAdapter(adapter);
}
private void initView(View view){
keyboardView=view.findViewById(R.id.frag_record_keyboard);
moneyEt=view.findViewById(R.id.frag_record_et_money);
typeIv=view.findViewById(R.id.frag_record_iv);
typeGv=view.findViewById(R.id.frag_record_gv);
typeTv=view.findViewById(R.id.frag_record_tv_type);
beizhuTv=view.findViewById(R.id.frag_record_tv_beizhu);
timeTv=view.findViewById(R.id.frag_record_tv_time);
beizhuTv.setOnClickListener(this);
timeTv.setOnClickListener(this);
//让自定义软键盘显示出来
KeyBoardUtils boardUtils=new KeyBoardUtils(keyboardView,moneyEt);
boardUtils.showKeyboard();
//设置接口,监听确定按钮按钮被点击了
boardUtils.setOnEnsureListener(new KeyBoardUtils.OnEnsureListener(){
@Override
public void onEnsure() {
//获取输入钱数
String moneyStr = moneyEt.getText().toString();
if (TextUtils.isEmpty(moneyStr)||moneyStr.equals("0")){
getActivity().finish();
return;
}
float money=Float.parseFloat(moneyStr);
accountBean.setMoney(money);
//获取记录的信息,保存在数据库当中
saveAccountToDB();
//返回上一级页面
getActivity().finish();
}
});
}
/*让子类一定要重写这个方法*/
public abstract void saveAccountToDB();

@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.frag_record_tv_time) {
showTimeDialog();
} else if (id == R.id.frag_record_tv_beizhu) {
shouBZDialog();
}
}
/*弹出显示时间的对话框*/
private void showTimeDialog(){
SelectTimeDialog dialog = new SelectTimeDialog(getContext());
dialog.show();
//设定确定按钮被点击了的监听器
dialog.setOnEnsureListener(new SelectTimeDialog.OnEnsureListener() {
@Override
public void onEnsure(String time, int year, int month, int day) {
timeTv.setText(time);
accountBean.setTime(time);
accountBean.setYear(year);
accountBean.setMonth(month);
accountBean.setDay(day);
}
});
}
/*弹出备注对话框*/
public void shouBZDialog(){
final BeiZhuDialog dialog = new BeiZhuDialog(getContext());
dialog.show();
dialog.setDialogSize();
dialog.setOnEnsureListener(new BeiZhuDialog.OnEnsureListener() {
@Override
public void onEnsure() {
String msg = dialog.getEditText();
if (!TextUtils.isEmpty(msg)) {
beizhuTv.setText(msg);
accountBean.setBeizhu(msg);
}
dialog.cancel();
}
});

}
}

package com.hui.myapplication.frag_record;

import androidx.fragment.app.Fragment;

import com.hui.myapplication.R;
import com.hui.myapplication.db.DBManager;
import com.hui.myapplication.db.TypeBean;

import java.util.List;

/**
* A simple {@link Fragment} subclass.
*/
public class IncomeFragment extends BaseRecordFragment {
@Override
public void loadDataToGv() {
super.loadDataToGv();
//获取数据库当中的数据源
List<TypeBean> inlist= DBManager.getTypeList(1);
typeList.addAll(inlist);
adapter.notifyDataSetChanged();
typeTv.setText("其他");
typeIv.setImageResource(R.mipmap.in_qt_fs);
}

@Override
public void saveAccountToDB() {
accountBean.setKind(1);
DBManager.insertItemToAccounttb(accountBean);
}

}

package com.hui.myapplication.frag_record;

import com.hui.myapplication.R;
import com.hui.myapplication.db.DBManager;
import com.hui.myapplication.db.TypeBean;

import java.util.List;

public class OutcomeFragment extends BaseRecordFragment{
//重写
@Override
public void loadDataToGv() {
super.loadDataToGv();
//获取数据库当中的数据源
List<TypeBean>outlist= DBManager.getTypeList(0);
typeList.addAll(outlist);
adapter.notifyDataSetChanged();
typeTv.setText("其他");
typeIv.setImageResource(R.mipmap.ic_qita_fs);
}

@Override
public void saveAccountToDB() {
accountBean.setKind(0);
DBManager.insertItemToAccounttb(accountBean);
}
}
posted @ 2024-03-24 20:45  记得关月亮  阅读(4)  评论(0编辑  收藏  举报