所以现在我正在尝试做一个理财应用程序。我想让余额在用户增加收入或支出时自动增加或减少。我正在使用sqlite作为数据库。有人能帮我解释一下怎么做吗?我不知道怎么做。
public class MoneyDBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "MONEYPROJECT.DB";
private static final int DATABASE_VERSION = 6;
private static final String TABLE_NAME = "MAINTABLE";
private static final String UID = "_id";
private static final String EXPENSE = "expenses";
private static final String INCOME = "incomes";
private static final String DESC = "description";
private static final String DATE= "date";
private static final String BALANCE= "balance";
private static final String createquery = "CREATE TABLE "+TABLE_NAME+" ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+BALANCE+" INT(9) ,"+EXPENSE+" INT(9),"+INCOME+" INT(9), "+DATE+" TEXT(8), "+DESC+" VARCHAR(100) );";
private static final String DROP_TABLE="DROP TABLE IF EXISTS "+ TABLE_NAME;
public MoneyDBHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(createquery);
}
public long insertDataincome(String income, String date){
SQLiteDatabase db= this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(MoneyDBHelper.INCOME, income);
contentValues.put(MoneyDBHelper.DATE, date);
long id= db.insert(MoneyDBHelper.TABLE_NAME,null, contentValues);
return id;
}
public long insertDataexpense(String expense, String date, String desc){
SQLiteDatabase db= this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(MoneyDBHelper.EXPENSE, expense);
contentValues.put(MoneyDBHelper.DESC, desc);
contentValues.put(MoneyDBHelper.DATE, date);
long id= db.insert(MoneyDBHelper.TABLE_NAME,null, contentValues);
return id;
}
以上是我的数据库设计...我想平衡的价值增加或减少每当用户输入收入或支出...我试图使目前的平衡在这里。
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private EditText mDisplayDate;
private DatePickerDialog.OnDateSetListener mDateSetListener;
Button calendarbt;
Button helpbt;
private Object view;
Context context;
MoneyDBHelper moneyDBHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView balance = (TextView) findViewById(R.id.balance);
final EditText expensebox = (EditText) findViewById(R.id.expenseamount);
final EditText expensedesc = (EditText) findViewById(R.id.expensedesc);
final Button addminus = (Button) findViewById(R.id.add2);
final EditText incomebox = (EditText) findViewById(R.id.incomeamount);
final Button addplus = (Button) findViewById(R.id.add1);
final EditText expensedate = (EditText) findViewById(R.id.expensedate);
moneyDBHelper= new MoneyDBHelper(this);
final SQLiteDatabase[] sqLiteDatabase = {moneyDBHelper.getWritableDatabase()};
mDisplayDate = (EditText) findViewById(R.id.expensedate);
mDisplayDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(
MainActivity.this,
android.R.style.Theme_Black,
mDateSetListener,
year,month,day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
});
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month = month + 1;
Log.d(TAG, "onDateSet: dd/mm/yyyy: "+ month + "/" + dayOfMonth + "/" + year);
String date = month + "/" + dayOfMonth + "/" + year;
mDisplayDate.setText(date);
}
};
Button plusbt = (Button) findViewById(R.id.plus);
plusbt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
incomebox.setVisibility(View.VISIBLE);
addplus.setVisibility(View.VISIBLE);
expensebox.setVisibility(View.GONE);
addminus.setVisibility(View.GONE);
expensedesc.setVisibility(View.GONE);
expensedate.setVisibility(View.VISIBLE);
}
});
Button minusbt= (Button) findViewById(R.id.minus);
minusbt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expensebox.setVisibility(View.VISIBLE);
addminus.setVisibility(View.VISIBLE);
expensedesc.setVisibility(View.VISIBLE);
incomebox.setVisibility(View.GONE);
addplus.setVisibility(View.GONE);
expensedate.setVisibility(View.VISIBLE);
}
});
addplus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(incomebox.length()==0){
incomebox.setError("Please Enter Income");
Toast.makeText(MainActivity.this, "Please fill all field", Toast.LENGTH_LONG).show();
}
else if (expensedate.length()==0){
expensedate.setError("Please Enter Date");
Toast.makeText(MainActivity.this, "Please fill all field", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainActivity.this, "Sucessful", Toast.LENGTH_LONG).show();
String income=incomebox.getText().toString();
String date=expensedate.getText().toString();
long id= moneyDBHelper.insertDataincome(income,date);
}
};
});
addminus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(expensebox.length()==0){
expensebox.setError("Please Enter Expense");
Toast.makeText(MainActivity.this, "Please fill all field", Toast.LENGTH_LONG).show();
}
else if (expensedate.length()==0){
expensedate.setError("Please Enter Date");
Toast.makeText(MainActivity.this, "Please fill all field", Toast.LENGTH_LONG).show();
}
else if (expensedesc.length()==0){
expensedesc.setError("Please Enter Date");
Toast.makeText(MainActivity.this, "Please fill all field", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(MainActivity.this, "Sucessful", Toast.LENGTH_LONG).show();
String expense=expensebox.getText().toString();
String date=expensedate.getText().toString();
String desc=expensedesc.getText().toString();
long id= moneyDBHelper.insertDataexpense(expense,date,desc);
}
};
});
1条答案
按热度按时间hgb9j2n61#
无论何时进行交易,您都必须更新数据库中的余额