个人博客
1.学到的东西
package com.example.jiwtingjizhangben;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.ViewModelProvider;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
DatabaseHelper helper = null;
SQLiteDatabase db = null;
String sql = null;
Double ZongZhichu = 0.0;
Double ZongShouru = 0.0;
ArrayList<Info> list = null;
TextView tvTotal = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.listview);
List<Info> list = getData();
listView.setAdapter(new MyAdapter(this, list));
tvTotal = findViewById(R.id.tv_total);
tvTotal.setText("总收入:" + ZongShouru + "\n总支出:" + ZongZhichu);
}
public List<Info> getData() {
list = new ArrayList<>();
helper = new DatabaseHelper(this, "demo.db", null, 1);
db = helper.getReadableDatabase();
sql = "select * from infos";
Cursor cursor = db.rawQuery(sql, null);
System.out.println(cursor);
while (cursor.moveToNext()) {
Info info = new Info();
String number = cursor.getString(0);
String name = cursor.getString(1);
String date = cursor.getString(2);
info.setNumber(number);
info.setName(name);
info.setDate(date);
if (Double.parseDouble(number) < 0) {
ZongZhichu -= Double.parseDouble(number);
} else {
ZongShouru += Double.parseDouble(number);
}
list.add(info);
}
return list;
}
public void shuaxin(View view){
ListView listView = findViewById(R.id.listview);
List<Info> list = getData();
listView.setAdapter(new MyAdapter(this, list));
}
public void shouru(View view){
Intent intent = new Intent(this,shouruActivity.class);
this.startActivity(intent);
}
public void zhichu(View view){
Intent intent = new Intent(this,zhichuActivity.class);
this.startActivity(intent);
}
}
package com.example.jiwtingjizhangben;
import androidx.appcompat.app.AppCompatActivity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
public class shouruActivity extends AppCompatActivity {
DatabaseHelper helper = null;
SQLiteDatabase db = null;
String sql = null;
EditText et_zhichu = null;
EditText et_name = null;
TextView tv_date = null;
Button btnSubmit = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shouru);
timeUpdate();
helper = new DatabaseHelper(this, "demo.db", null, 1);
db = helper.getWritableDatabase();
et_zhichu = (EditText) findViewById(R.id.et_shouru);
et_name = (EditText) findViewById(R.id.et_name);
}
public void timeUpdate(){
tv_date = (TextView)findViewById(R.id.tv_date);
SimpleDateFormat simpleDateFormatDate = new SimpleDateFormat("yyyy年MM月dd日");
Date date = new Date(System.currentTimeMillis());
tv_date.setText(simpleDateFormatDate.format(date));
}
public void submitClick(View view){
timeUpdate();
String number = et_zhichu.getText().toString();
String name = et_name.getText().toString();
String date = tv_date.getText().toString();
sql = "insert into infos values('"+ number +"','"+name+"','"+date+"');";
db.execSQL(sql);
}
}
package com.example.jiwtingjizhangben;
import androidx.appcompat.app.AppCompatActivity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
public class zhichuActivity extends AppCompatActivity {
DatabaseHelper helper = null;
SQLiteDatabase db = null;
String sql = null;
EditText et_zhichu = null;
EditText et_name = null;
TextView tv_date = null;
Button btnSubmit = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zhichu);
timeUpdate();
helper = new DatabaseHelper(this, "demo.db", null, 1);
db = helper.getWritableDatabase();
et_zhichu = (EditText) findViewById(R.id.et_shouru);
et_name = (EditText) findViewById(R.id.et_name);
}
public void timeUpdate(){
tv_date = (TextView)findViewById(R.id.tv_date);
SimpleDateFormat simpleDateFormatDate = new SimpleDateFormat("yyyy年MM月dd日");
Date date = new Date(System.currentTimeMillis());
tv_date.setText(simpleDateFormatDate.format(date));
}
public void submitClick(View view){
timeUpdate();
String number = '-' + et_zhichu.getText().toString();
String name = et_name.getText().toString();
String date = tv_date.getText().toString();
sql = "insert into infos values('"+ number +"','"+name+"','"+date+"');";
db.execSQL(sql);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/tv_total"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
</TextView>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
>
</ListView>
<Button
android:id="@+id/btn_shuaxin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="刷新"
android:onClick="shuaxin"
android:layout_gravity="center"
/>
<Button
android:id="@+id/btn_shouru"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="收入"
android:onClick="shouru"
android:layout_marginTop="20dp"
android:layout_gravity="center"
/>
<Button
android:id="@+id/btn_zhichu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="支出"
android:onClick="zhichu"
android:layout_marginTop="20dp"
android:layout_gravity="center"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".shouruActivity"
android:orientation="vertical">
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:hint="成员姓名"
android:maxLength="12"
android:padding="5dp"
android:textSize="16sp" />
<EditText
android:id="@+id/et_shouru"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:hint="收入金额"
android:maxLength="12"
android:padding="5dp"
android:textSize="16sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxEms="20"
android:text="日期:"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxEms="20"
android:text=""
android:textSize="16sp" />
</LinearLayout>
<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:onClick="submitClick"
android:text="提交" />
</LinearLayout>
2.明日计划
继续完成第二阶段