这是java文件
package com.src;
import java.text.NumberFormat;
import java.util.Calendar;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class MyyjActivity extends Activity {
public static final String DB_NAME = "test.db";
/**
* 定义数据库名称
*/
private TextView middle, message, totalMoney, traffic, communication,
catering, entertainment;
/**
* 定义TextView控件以及定义其变量
*/
private ImageButton leftButton;
/**
* 界面里的左按钮
*/
private ImageButton rightButton;
/**
* 界面里的右按钮
*/
private MyOpenHelper OpenHelper;
/**
* 打开数据库的辅助类
*/
private SQLiteDatabase db = null;
int month;
int year;
int day;
int temp;
//第二次强制转换的赋值变量
String d;
//保存年月日的拼接字符串变量
String largeCost, trueCost, money, type, description, time;
//lareCost:从设置界面传过来的最高消费
//trueCost:
SharedPreferences dataBase;
//为跨页面取值做准备
Editor edit;
double cost = 0,//接收数据库查询的金额数据并进行强制转换的变量
totalCost = 0, //总金额
trafficMoney = 0, //公交的总消费金额
cateringMoney = 0,//餐饮的总消费金额
communicationMoney = 0,//通讯的总消费金额
entertainmentMoney = 0;//娱乐的总消费金额
Toast toastShow;//显示消息
NumberFormat nf;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.myyj);
middle = (TextView) findViewById(R.id.time);
totalMoney = (TextView) findViewById(R.id.zxf_txt);
traffic = (TextView) findViewById(R.id.gj_txt);
catering = (TextView) findViewById(R.id.cy_txt);
communication = (TextView) findViewById(R.id.tx_txt);
entertainment = (TextView) findViewById(R.id.yl_txt);
message = (TextView) findViewById(R.id.mes);
leftButton = (ImageButton) findViewById(R.id.left_btn);
rightButton = (ImageButton) findViewById(R.id.right_btn);
setUiClick();//调用监听方法
fillUi();//调用显示日期方法
showData(d);//调用查询数据并显示的方法
}
/**
* 设置控件显示当前日期
*/
public void fillUi() {
Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1;
day = calendar.get(Calendar.DAY_OF_MONTH);
if (month < 10) {
d = year + "-" + "0" + month;
} else {
d = year + "-" + month;
}
middle.setText(d);
}
/**
* 设置左右imageButton图片按钮的监听事件
*/
public void setUiClick() {
final Calendar calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH) + 1;
leftButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
month--;
if (month == 0) {
year = year - 1;
month = 13;
} else {
if (month < 10) {
d = year + "-" + "0" + month;
} else {
d = year + "-" + month;
}
}
middle.setText(d);
trafficMoney = 0;
cateringMoney = 0;
communicationMoney = 0;
totalCost = 0;
entertainmentMoney = 0;
cost = 0;
showData(d);
}
});
rightButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
month++;
if (month == 13) {
year = year + 1;
month = 1;
d = year + "-" + "0" + month;
} else {
if (month < 10) {
d = year + "-" + "0" + month;
} else {
d = year + "-" + month;
}
}
middle.setText(d);
totalCost = 0;
trafficMoney = 0;
communicationMoney = 0;
cateringMoney = 0;
entertainmentMoney = 0;
cost = 0;
showData(d);
}
});
}
/**
*
* @param da:传入的时间变量
* 根据时间查询数据
*/
private void showData(String da) {
OpenHelper = new MyOpenHelper(MyyjActivity.this, DB_NAME, null, 1);
db = OpenHelper.getReadableDatabase();
Cursor cursor = null;
try {
cursor = db.query("count", null, "sj like" + "'" + "%" + da + "%"
+ "'", null, null, null, null);
if (cursor.moveToFirst()) {
do {
money = cursor.getString(cursor.getColumnIndex("je"));
description = cursor.getString(cursor.getColumnIndex("ms"));
type = cursor.getString(cursor.getColumnIndex("lx"));
time = cursor.getString(cursor.getColumnIndex("sj"));
fillDatas();
} while (cursor.moveToNext());
}
numberFormat();//调用格式化数字的方法
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
dataBase = getSharedPreferences("spName", 0);
edit = dataBase.edit();
largeCost = dataBase.getString("ZGXF", null);
long largeCost1 = Integer.parseInt(largeCost);
if (totalCost > largeCost1) {
message.setText("这个月我超支了!");
} else {
message.setText("这个月我没超支!");
}
}
/**
*
* 将查询的数据显示在textView上
*/
private void fillDatas() {
if (type.equals("公交")) {
cost = Double.parseDouble(money);
temp=(int)cost;
trafficMoney += temp;
} else {
traffic.setText("" + 0.00);
}
if (type.equals("餐饮")) {
cost = Double.parseDouble(money);
temp=(int)cost;
cateringMoney += temp;
} else {
catering.setText("" + 0.00);
}
if (type.equals("通讯")) {
cost = Double.parseDouble(money);
temp=(int)cost;
communicationMoney += temp;
} else {
communication.setText("" + 0.00);
}
if (type.equals("娱乐")) {
cost = Double.parseDouble(money);
temp=(int)cost;
entertainmentMoney += temp;
} else {
entertainment.setText("" + 0.00);
}
}
/**
* 对查询的数据进行格式化
*/
private void numberFormat() {
nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
nf.format(trafficMoney);
nf.format(communicationMoney);
nf.format(cateringMoney);
nf.format(entertainmentMoney);
nf.format(totalCost);
traffic.setText("" + nf.format(trafficMoney));
communication.setText("" + nf.format(communicationMoney));
catering.setText("" + nf.format(cateringMoney));
entertainment.setText("" + nf.format(entertainmentMoney));
totalCost = trafficMoney + cateringMoney + entertainmentMoney
+ communicationMoney;
totalMoney.setText("" + nf.format(totalCost));
}
}