自己也是第一次使用,中途遇到很多難題,總算努力還是會有識訓,自己代碼有很多不足的地方希望大神指出來一起學習,加油奧利給,話不多說貼代碼

第一步,onCreate建立資料庫
public class DBHelperop extends SQLiteOpenHelper {
// 資料庫檔案名
public static final String DB_NAME = "two.db";
// 資料庫版本號
public static final int DB_VERSION = 1;
public DBHelperop(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE practic(id INTEGER PRIMARY KEY AUTOINCREMENT," +
" num VARCHAR,newratio VARCHAR,numratio VARCHAR,total VARCHAR,str_time VARCHAR)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
}
第二步,我建了一個物體類接收資料,代碼沒貼完
public class DbInfor {
private int id;
private String num;
private double newratio;
private double numratio;
private double total;
private String str_time;
public DbInfor(){}
public DbInfor(String num, double newratio, double numratio, double total, String str_time) {
this.num = num;
this.newratio = newratio;
this.numratio = numratio;
this.total = total;
this.str_time = str_time;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNum() {
return num;
}
第三步,建了一個管理資料庫的類,我只做了兩個查詢和一個插入
public class DBManager {
private DBHelperop helper;
private SQLiteDatabase db;
public DBManager(Context context){
helper = new DBHelperop(context);
db = helper.getWritableDatabase();
}
public void add(List<DbInfor> persons){
db.beginTransaction();
try{
for (DbInfor p:persons){
db.execSQL("INSERT INTO practic (num,newratio,numratio,total,str_time)VALUES(?,?,?,?,?)",
//select * from role order by role_id desc limit 0,1;
new Object[]{p.getNum(),p.getNewratio(),p.getNumratio(),p.getTotal(),p.getStr_time()});
}
db.setTransactionSuccessful();
}catch(Exception e){
e.printStackTrace();
}finally {
db.endTransaction();
}
}
public List<DbInfor> findAllDbInfor(int a){
List<DbInfor> dbinfoe = new ArrayList<>();
//"select * from practic order by id desc limit "+a+",2;"
Cursor c = db.rawQuery("select * from practic order by id desc limit "+a+",6", null);
while(c.moveToNext()){
DbInfor p = new DbInfor();
p.setId(c.getInt(c.getColumnIndex("id")));
p.setNum(c.getString(c.getColumnIndex("num")));
p.setNewratio(c.getDouble(c.getColumnIndex("newratio")));
p.setNumratio(c.getDouble(c.getColumnIndex("numratio")));
p.setTotal(c.getDouble(c.getColumnIndex("total")));
p.setStr_time(c.getString(c.getColumnIndex("str_time")));
dbinfoe.add(p);
}
c.close();
return dbinfoe;
}
public DbInfor findDbInfor(){
DbInfor p = null;
Cursor c = db.rawQuery("select * from practic order by id desc limit 0,1", null);
while(c.moveToNext()){
p = new DbInfor();
p.setId(c.getInt(c.getColumnIndex("id")));
p.setNum(c.getString(c.getColumnIndex("num")));
p.setNewratio(c.getDouble(c.getColumnIndex("newratio")));
p.setNumratio(c.getDouble(c.getColumnIndex("numratio")));
p.setTotal(c.getDouble(c.getColumnIndex("total")));
p.setStr_time(c.getString(c.getColumnIndex("str_time")));
}
c.close();
return p;
第三步,一個輸入資料的類,帶一個查詢,
public class MainActivity extends Activity{
private DBManager dm;
private EditText shuru;
private TextView shuchu;
private int a = 100;
private double newratio ;
private double numratio ;
private double total;
private Button btConform;
private TextView cahkan;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shuru = findViewById(R.id.et_etnum);
shuchu = findViewById(R.id.la_view2);
cahkan= findViewById(R.id.cahkan);
btConform = findViewById(R.id.bt_conform);
dm = new DBManager(this);
btConform.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
insertData();
}
});
cahkan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,DetialActivity.class));
}
});
}
public void insertData(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm E");
Date date = new Date(System.currentTimeMillis());
String time = sdf.format(date);//獲取系統時間
String shurunum = shuru.getText().toString();
DbInfor dbInform= dm.findDbInfor();//查詢最后一個資料
newratio = Double.valueOf(shurunum)/(double)a;
if (dbInform==null){
numratio = Double.valueOf(shurunum)+0/(double)a;
total =Double.valueOf(shurunum)+0;
}else {
numratio = (Double.valueOf(shurunum)+dbInform.getTotal())/(double)a;
total =Double.valueOf(shurunum)+dbInform.getTotal();
}
List<DbInfor> persons = new ArrayList<>();
DbInfor p1 = new DbInfor(shurunum,newratio,numratio,total,time);//插入獲取的資料
persons.add(p1);
dm.add(persons);
findDb();
}
public void findDb(){
//輸出方法
DbInfor dbInfor= dm.findDbInfor();
if (dbInfor.getNumratio()==0){
shuchu.setText(0+"%");
}
DecimalFormat df=new DecimalFormat(".##");
double d=dbInfor.getNumratio()*100;
String st=df.format(d);
shuchu.setText(st+"%");
}
}
第五步,查詢所有資料并顯示
public class DetialActivity extends AppCompatActivity implements XListView.IXListViewListener{
private TextView title,detail,time;
private ListRooAdapter listAdapter;
private XListView lvContent;
private DBManager dm;
private int a=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detial);
title= findViewById(R.id.news_tvtitle);
detail= findViewById(R.id.news_tvdetail);
title= findViewById(R.id.news_tvtime);
dm = new DBManager(this);
listAdapter= new ListRooAdapter(this);
lvContent =findViewById(R.id.lvcent);
lvContent.setXListViewListener(this);
lvContent.setPullRefreshEnable(true);
lvContent.setPullLoadEnable(true);
lvContent.hideFoot();
lvContent.setAdapter(listAdapter);
love();
}
public void love(){
onComplete();
List<DbInfor> datas = dm.findAllDbInfor(a);
if (datas.size()<0){
Toast.makeText(this, "暫時還沒有記錄呢", Toast.LENGTH_SHORT).show();
}
if(datas.size() < 5){
lvContent.hideFoot();//根據資料顯示長度的,顯示加載按鈕
}else{
lvContent.showFoot();
}
// lvContent.showFoot();
if(a == 0) {
listAdapter.setDatas(datas);
}else{
listAdapter.addDatas(datas);
}
if(datas.size()>0){
a+=6;
}
}
@Override
public void onRefresh() {//下拉重繪
a=0;
love();
}
@Override
public void onl oadMore() {
love();//點擊加載
}
public void onComplete(){
lvContent.stopLoadMore();
lvContent.stopRefresh();
}
}
其他代碼我就不貼了,每個人用資料庫有自己的用法 如需要聯系我
最后效果因為原因沒上傳,有些方法參考了該作者鏈接為
2021年1月6日周三凌晨12:53
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/245698.html
標籤:其他
