Andoid+sqlite开发的生命密码原型

前端之家收集整理的这篇文章主要介绍了Andoid+sqlite开发的生命密码原型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<!--
Project name:Mima
Created by Practicer2015 on 2014-09.
Copyright 2014 Practicer2015. All rights reserved.

产品核心功能
1,选择出生日期,计算生命密码
-->

MainActivity 代码

package zn.com.mama;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;

import java.util.List;

import android.content.Intent;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.Toast;
import android.view.View.OnClickListener;
import android.app.Activity;

public class MainActivity extends Activity {


private DataBaseUtil util = null;
private DatePicker dp;//日期控件
private Calendar c;//JAVA中的日历类
String tag = "M" ;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

dp = (DatePicker)findViewById(R.id.datePicker1);

//dp.updateDate(2012,8,9);

c = Calendar.getInstance();

dp.init(c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH),new OnDateChangedListener() {
/*设置日期发生更改时的事件监听*/
public void onDateChanged(DatePicker view,int year,int monthOfYear,
int dayOfMonth) {
c.set(year,monthOfYear,dayOfMonth);
}
});


Button buttonSet = (Button)findViewById(R.id.button1);

OnClicLisers cl=new OnClicLisers();

buttonSet.setOnClickListener(cl);


Button buttonlist = (Button)findViewById(R.id.button2);

OnClicLisersview clview =new OnClicLisersview();

buttonlist.setOnClickListener(clview);
}



class OnClicLisers implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
List<String> data = new ArrayList<String>();
copyDataBaseToPhone();
Log.v(tag,"copyDataBaseToPhone");
int y=dp.getYear();
Log.v(tag,Integer.toString(y));
int m=dp.getMonth()+1;
Log.v(tag,Integer.toString(m));
int d=dp.getDayOfMonth();
Log.v(tag,Integer.toString(d));

String sss = Integer.toString(y)+Integer.toString(m)+Integer.toString(d);
Log.v(tag,sss);

data = compute(sss);

if (null != data)
{

Toast.makeText(getApplicationContext(),sss,Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent();

myIntent.putStringArrayListExtra("val",(ArrayList<String>)data);

myIntent.setClass(MainActivity.this,ShowActivity.class);

startActivity(myIntent);
Log.v(tag,"staredtActivity");
data = null;

}
else
{
Log.v(tag,"data is null");

}
}
}


class OnClicLisersview implements OnClickListener{
@Override
public void onClick(View v) {

List<String> data = new ArrayList<String>();
copyDataBaseToPhone();
Log.v(tag,Integer.toString(d));
String sss = Integer.toString(y)+Integer.toString(m)+Integer.toString(d);
Log.v(tag,Toast.LENGTH_SHORT).show();

Intent myIntent = new Intent();

myIntent.putStringArrayListExtra("val",MyListView.class);

startActivity(myIntent);

Log.v(tag,"staredtActivity");
data = null;
}
else
{
Log.v(tag,"data is null");

}


}
}
/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main,menu);
return super.onCreateOptionsMenu(menu);
}*/

/* @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button,so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}*/


/*@Override
public boolean onKeyDown(int keyCode,KeyEvent event) {
// TODO Auto-generated method stub

if(keyCode == KeyEvent.KEYCODE_BACK&&event.getRepeatCount() == 0 ) { //监控/拦截/屏蔽返回键

Toast.makeText(getApplicationContext(),"再按一次返回键,可以返回",Toast.LENGTH_SHORT).show();

return true;
} else if(keyCode == KeyEvent.KEYCODE_MENU) {
//监控/拦截菜单


} else if(keyCode == KeyEvent.KEYCODE_HOME) {
//由于Home键为系统键,此处不能捕获,需要重写onAttachedToWindow()


}
return super.onKeyDown(keyCode,event);

}*/


/* (non-Javadoc)
* @see android.app.Activity#onAttachedToWindow()
*/
/*@Override
public void onAttachedToWindow() {
// TODO Auto-generated method stub
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}*/

@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();

}

private void copyDataBaseToPhone() {


DataBaseUtil util = new DataBaseUtil(this);
if (null != util)
{
boolean dbExist = util.checkDataBase();

if (dbExist) {
Log.v(tag,"The database is exist.");
} else {// 不存在就把raw里的数据库写入手机
try {
util.copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}


}


}


/*计算生命数字*/
public List<String> compute(String str) {

int int_life_number = 0;

int int_gift_number1 = 0;

int int_gift_number2 = 0;

int int_perfect_number = 0;

List<String> data = new ArrayList<String>();

int int_number = 0;
String fst = "";
String str1 = "";

System.out.print(str);
for(int num=0 ;num<str.length();num++)
{

if (num <= str.length()-1)
{

fst = str.substring(num,num+1);
int_number = int_number + Integer.parseInt(fst);
}

}

System.out.println(int_number);

str1 = Integer.toString(int_number);

int_gift_number1 = Integer.parseInt(str1.substring(0,1));

data.add("gift_number1" + ":" +int_gift_number1);

Log.v(tag,"gift_number1" + ":" +Integer.toString(int_gift_number1));

int_gift_number2 = Integer.parseInt(str1.substring(1,2));

data.add("gift_number1" + ":" +int_gift_number2);

Log.v(tag,"gift_number2" + ":" +Integer.toString(int_gift_number2));

int_life_number = int_gift_number1 + int_gift_number2;


data.add("life_number" + ":" +int_life_number);

Log.v(tag,"life_number" + ":" +Integer.toString(int_life_number));

int_perfect_number = Integer.parseInt(str1+Integer.toString(int_life_number));

data.add("perfect_number" + ":" +int_perfect_number);

Log.v(tag,"perfect_number" + ":" +Integer.toString(int_perfect_number));

return data;

}


}

sqlite数据库代码

package zn.com.mama;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;

public class ShowActivity extends Activity {

public static int int_life_number = 0;

public static int int_gift_number1 = 0;

public static int int_gift_number2 = 0;

public static int perfect_number = 0;

protected int iBackcount = 0;
private List<String> infoList = new ArrayList<String>();
EditText eMima;
EditText eMima1;
EditText eMima2;
EditText eMima3;

protected sqliteDatabase db ;
private String tag1 = "S" ;

@Override
protected void onCreate(Bundle savedInstanceState) {

Cursor c= null;
String sPara = "";
String sResult = "";
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
Log.v(tag1,"show");
eMima = (EditText)findViewById(R.id.editText1);
eMima1 = (EditText)findViewById(R.id.editText2);
eMima2 = (EditText)findViewById(R.id.editText3);
eMima3 = (EditText)findViewById(R.id.editText4);

infoList = (List<String>)getIntent().getStringArrayListExtra("val");
Log.v(tag1,"getlist");
if (null != infoList)
{
int j = 1;
db = OpenandCreateDb();
if (null == db )
{

Log.v(tag1,"db is null");
}
else
{

if (sResult.length()>0) { sResult = "";}

for (String str : infoList) {


int i = str.indexOf(":");

sPara = str.substring(i+1,str.length());

switch (j){

case 1:
Log.v(tag1,"int_gift_number1:"+sPara);

int_gift_number1 = Integer.parseInt(sPara);

c = db.rawQuery("SELECT value FROM gift_code WHERE int_gift_number = ?",new String[]{sPara});

if(c != null && c.getCount()>0)
{
c.moveToNext();
String uname = c.getString( c.getColumnIndex("value") );
uname = this.getResources().getString(R.string.sgiftcode1)+sPara+uname;
eMima.setText(uname);
}
c.close();
break;
case 2:
Log.v(tag1,"int_gift_number2:"+sPara);
int_gift_number2 = Integer.parseInt(sPara);
c = db.rawQuery("SELECT value FROM gift_code WHERE int_gift_number = ?",new String[]{sPara});
if(c != null && c.getCount()>0)
{
c.moveToNext();
//进行操作
String uname = c.getString( c.getColumnIndex("value") );
uname = this.getResources().getString(R.string.sgiftcode2)+sPara+uname;
eMima1.setText(uname);

}
c.close();
break;
case 3:
Log.v(tag1,"lifecode:"+sPara);

int_life_number = Integer.parseInt(sPara);
c = db.rawQuery("SELECT value FROM life_code WHERE int_life_number = ?",new String[]{sPara});
if(c != null && c.getCount()>0)
{
c.moveToNext();
//进行操作
String uname = c.getString( c.getColumnIndex("value") );
uname = this.getResources().getString(R.string.slifecode)+sPara+uname;
eMima2.setText(uname);

}
c.close();
break;

case 4:
Log.v(tag1,"perferct number"+sPara);

perfect_number = Integer.parseInt(sPara);

c = db.rawQuery("SELECT value FROM perfect_code WHERE int_perfect_number = ?",new String[]{sPara});

if(c != null && c.getCount()>0)
{
c.moveToNext();

//进行操作
String uname = c.getString( c.getColumnIndex("value") );
uname = this.getResources().getString(R.string.sperfectcode)+sPara+uname;
eMima3.setText(uname);


}

c.close();
break;
default:
break;
}

j++;
}//for
}

db.close();

}//infolist
else
{

eMima.setText("");

Log.v(tag1,"Infolist is null");

}
}
/* (non-Javadoc)
* @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
*/
/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.share,menu);
return super.onCreateOptionsMenu(menu);
}/*

/* (non-Javadoc)
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
*/
/*@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
int id = item.getItemId();
if (id == R.id.action_share) {

Toast.makeText(getApplicationContext(),"分享功能在下一版本提供",Toast.LENGTH_SHORT).show();

return true;
}
return super.onOptionsItemSelected(item);
}*/

@Override
public boolean onKeyDown(int keyCode,KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK&&event.getRepeatCount() == 0 ) { //监控/拦截/屏蔽返回键

// Toast.makeText(getApplicationContext(),Toast.LENGTH_SHORT).show();

}else
{
}

/*else if(keyCode == KeyEvent.KEYCODE_MENU) {
//监控/拦截菜单
} else if(keyCode == KeyEvent.KEYCODE_HOME) {
//由于Home键为系统键,此处不能捕获,需要重写onAttachedToWindow()
}*/

return super.onKeyDown(keyCode,event);

}

/* (non-Javadoc)
* @see android.app.Activity#onAttachedToWindow()
*/
/*@Override
public void onAttachedToWindow() {
// TODO Auto-generated method stub
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}*/

@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();

}

private sqliteDatabase OpenandCreateDb(){ Log.v(tag1,"createdb"); Context context=this; db = sqliteDatabase.openOrCreateDatabase(context.getFilesDir().getAbsolutePath().replace("files","databases") + File.separator+"test.db",null); Log.v(tag1,context.getFilesDir().getAbsolutePath()+"test.db"); Log.v(tag1,"createddb"); return db; } }

原文链接:https://www.f2er.com/sqlite/199738.html

猜你在找的Sqlite相关文章