今天尝试了家庭小账本的数据库创建并且测试,但是在写完代码运行测试的时候软件闪退,修改了好长时间并没有成功,可能是因为作为初学者有很多地方并不是清晰地了解,修改的地方也不是很准确,做了很多的无用功,但是今天这次毕竟是一个尝试。
先创建了DataBaseHelper的类,进行数据库的创建,添加以及其他的操作。
1 package com.example.familyledger; 2 3 import android.database.Cursor; 4 import android.os.Bundle; 5 6 import com.google.android.material.floatingactionbutton.FloatingActionButton; 7 import com.google.android.material.snackbar.Snackbar; 8 9 import androidx.appcompat.app.AppCompatActivity; 10 import androidx.appcompat.widget.Toolbar; 11 12 import android.view.View; 13 import android.view.Menu; 14 import android.view.MenuItem; 15 import android.widget.ListView; 16 17 import java.util.ArrayList; 18 import java.util.List; 19 20 import androidx.appcompat.app.AppCompatActivity; 21 22 import android.database.sqlite.SQLiteOpenHelper; 23 import android.os.Bundle; 24 import android.content.Context; 25 import android.database.sqlite.SQLiteDatabase; 26 import android.database.sqlite.SQLiteDatabase.CursorFactory; 27 import android.database.sqlite.SQLiteException; 28 import android.util.Log; 29 30 public class MainActivity extends AppCompatActivity { 31 32 private List<CostBean> mCostListBean; 33 private DataBaseHelper mDataBaseHelper; 34 @Override 35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 setContentView(R.layout.activity_main); 38 Toolbar toolbar = findViewById(R.id.toolbar); 39 setSupportActionBar(toolbar); 40 41 42 mDataBaseHelper=new DataBaseHelper(this); 43 mCostListBean=new ArrayList<>(); 44 ListView costList=(ListView)findViewById(R.id.lv_main); 45 initCostdata(); 46 costList.setAdapter(new CostListAdapter(this,mCostListBean)); 47 48 49 FloatingActionButton fab = findViewById(R.id.fab); 50 fab.setOnClickListener(new View.OnClickListener() { 51 @Override 52 public void onClick(View view) { 53 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 54 .setAction("Action", null).show(); 55 } 56 }); 57 } 58 59 private void initCostdata() { 60 for (int i=0;i<6;i++) { 61 CostBean costBean=new CostBean(); 62 costBean.costDate="11-11"; 63 costBean.costMoney="20"; 64 costBean.costTitle=i+"mock"; 65 mDataBaseHelper.insertCost(costBean); 66 } 67 Cursor cursor =mDataBaseHelper.getAllCostData(); 68 if(cursor!=null) 69 { 70 while (cursor.moveToNext()){ 71 CostBean costBean=new CostBean(); 72 costBean.costTitle=cursor.getString(cursor.getColumnIndex("cost_title")); 73 costBean.costDate=cursor.getString(cursor.getColumnIndex("cost_date")); 74 costBean.costMoney=cursor.getString(cursor.getColumnIndex("cost_money")); 75 mCostListBean.add(costBean); 76 } 77 cursor.close(); 78 } 79 } 80 81 @Override 82 public boolean onCreateOptionsMenu(Menu menu) { 83 // Inflate the menu; this adds items to the action bar if it is present. 84 getMenuInflater().inflate(R.menu.menu_main, menu); 85 return true; 86 } 87 88 @Override 89 public boolean onOptionsItemSelected(MenuItem item) { 90 // Handle action bar item clicks here. The action bar will 91 // automatically handle clicks on the Home/Up button, so long 92 // as you specify a parent activity in AndroidManifest.xml. 93 int id = item.getItemId(); 94 95 //noinspection SimplifiableIfStatement 96 if (id == R.id.action_settings) { 97 return true; 98 } 99 100 return super.onOptionsItemSelected(item); 101 } 102 }
然后在MainActivity中添加关于创建数据库操作的代码,这时候数据库中已经存在了几天数据(上述代码),但是添加完之后并不能运行,修改多次也不行,今天只能这样结束了。
1 package com.example.familyledger; 2 3 import android.database.Cursor; 4 import android.os.Bundle; 5 6 import com.google.android.material.floatingactionbutton.FloatingActionButton; 7 import com.google.android.material.snackbar.Snackbar; 8 9 import androidx.appcompat.app.AppCompatActivity; 10 import androidx.appcompat.widget.Toolbar; 11 12 import android.view.View; 13 import android.view.Menu; 14 import android.view.MenuItem; 15 import android.widget.ListView; 16 17 import java.util.ArrayList; 18 import java.util.List; 19 20 import androidx.appcompat.app.AppCompatActivity; 21 22 import android.database.sqlite.SQLiteOpenHelper; 23 import android.os.Bundle; 24 import android.content.Context; 25 import android.database.sqlite.SQLiteDatabase; 26 import android.database.sqlite.SQLiteDatabase.CursorFactory; 27 import android.database.sqlite.SQLiteException; 28 import android.util.Log; 29 30 public class MainActivity extends AppCompatActivity { 31 32 private List<CostBean> mCostListBean; 33 private DataBaseHelper mDataBaseHelper; 34 @Override 35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 setContentView(R.layout.activity_main); 38 Toolbar toolbar = findViewById(R.id.toolbar); 39 setSupportActionBar(toolbar); 40 41 42 mDataBaseHelper=new DataBaseHelper(this); 43 mCostListBean=new ArrayList<>(); 44 ListView costList=(ListView)findViewById(R.id.lv_main); 45 initCostdata(); 46 costList.setAdapter(new CostListAdapter(this,mCostListBean)); 47 48 49 FloatingActionButton fab = findViewById(R.id.fab); 50 fab.setOnClickListener(new View.OnClickListener() { 51 @Override 52 public void onClick(View view) { 53 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 54 .setAction("Action", null).show(); 55 } 56 }); 57 } 58 59 private void initCostdata() { 60 for (int i=0;i<6;i++) { 61 CostBean costBean=new CostBean(); 62 costBean.costDate="11-11"; 63 costBean.costMoney="20"; 64 costBean.costTitle=i+"mock"; 65 mDataBaseHelper.insertCost(costBean); 66 } 67 Cursor cursor =mDataBaseHelper.getAllCostData(); 68 if(cursor!=null) 69 { 70 while (cursor.moveToNext()){ 71 CostBean costBean=new CostBean(); 72 costBean.costTitle=cursor.getString(cursor.getColumnIndex("cost_title")); 73 costBean.costDate=cursor.getString(cursor.getColumnIndex("cost_date")); 74 costBean.costMoney=cursor.getString(cursor.getColumnIndex("cost_money")); 75 mCostListBean.add(costBean); 76 } 77 cursor.close(); 78 } 79 } 80 81 @Override 82 public boolean onCreateOptionsMenu(Menu menu) { 83 // Inflate the menu; this adds items to the action bar if it is present. 84 getMenuInflater().inflate(R.menu.menu_main, menu); 85 return true; 86 } 87 88 @Override 89 public boolean onOptionsItemSelected(MenuItem item) { 90 // Handle action bar item clicks here. The action bar will 91 // automatically handle clicks on the Home/Up button, so long 92 // as you specify a parent activity in AndroidManifest.xml. 93 int id = item.getItemId(); 94 95 //noinspection SimplifiableIfStatement 96 if (id == R.id.action_settings) { 97 return true; 98 } 99 100 return super.onOptionsItemSelected(item); 101 } 102 }