这两天写了个小东东, 关于测土的《我不专业啊,数据别人提供的》
主要有看俺代码吧,
//这个是主activity
package cn.com.itisme.ctsf;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.itisme.ctsf.dao.SqliteOp;
import cn.com.itisme.ctsf.entity.XiShu;
import cn.com.itisme.ctsf.entity.YangFenXuQiu;
public class MainActivity extends ActionBarActivity {
private final int OVER_WATER = 0;
private final int DOT_WATER = 1;
private Spinner spinnerCrop;
private Spinner spinnerArea;
private RadioGroup rbGroup;
private long purpose;
private double cantainN;
private double cantainP;
private double cantainK;
private EditText etPurpose;
private EditText etIncN;
private EditText etIncP;
private EditText etIncK;
private Button btnRecomm;
private TextView tvN;
private TextView tvP;
private TextView tvK;
private double usageN = 1.0;
private double usageK = 1.0;
private double usageP = 1.0;
DecimalFormat df=new DecimalFormat("######.##");
private List<XiShu> xiShuLists;
private List<YangFenXuQiu> lists;
private List<String> crops = new ArrayList<String>();
private List<String> areas = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
}
private void getDataFile() {
lists = new ArrayList<YangFenXuQiu>();
xiShuLists = new ArrayList<XiShu>();
SqliteOp sqliteOp = new SqliteOp();
SQLiteDatabase cstfDB = sqliteOp.openDatabase(getApplicationContext());
if (cstfDB == null) {
return;
}
Cursor cursor = cstfDB.rawQuery("SELECT * from yangfenxuqiu ", null);
if (cursor == null) {
return;
}
while (cursor.moveToNext()) {
YangFenXuQiu yfxq = new YangFenXuQiu();
yfxq.setId(cursor.getInt(cursor.getColumnIndex("id")));
yfxq.setCropname(cursor.getString(cursor.getColumnIndex("cropname")));
yfxq.setCroptype(cursor.getString(cursor.getColumnIndex("croptype")));
yfxq.setGrain(cursor.getString(cursor.getColumnIndex("grain")));
yfxq.setN(cursor.getDouble(cursor.getColumnIndex("n")));
yfxq.setP2o5(cursor.getDouble(cursor.getColumnIndex("p2o5")));
yfxq.setK2o(cursor.getDouble(cursor.getColumnIndex("k2o")));
lists.add(yfxq);
}
Cursor xishuC = cstfDB.rawQuery("SELECT * from xishu", null);
if (xishuC == null) {
return;
}
while (xishuC.moveToNext()) {
XiShu xishu = new XiShu();
xishu.setId(xishuC.getInt(xishuC.getColumnIndex("id")));
xishu.setQuyu(xishuC.getString(xishuC.getColumnIndex("quyu")));
xishu.setNx(xishuC.getDouble(xishuC.getColumnIndex("nx")));
xishu.setNz(xishuC.getDouble(xishuC.getColumnIndex("nz")));
xishu.setPx(xishuC.getDouble(xishuC.getColumnIndex("px")));
xishu.setPz(xishuC.getDouble(xishuC.getColumnIndex("pz")));
xiShuLists.add(xishu);
}
cursor.close();
xishuC.close();
cstfDB.close();
}
private void initview() {
getDataFile();
if (lists.size() != 0) {
for (YangFenXuQiu ya : lists) {
crops.add(ya.getCropname());
}
} else {
crops.add("无数据");
}
if (xiShuLists.size() != 0) {
for (XiShu xishu : xiShuLists) {
areas.add(xishu.getQuyu());
}
} else {
areas.add("无数据");
}
spinnerCrop = (Spinner) findViewById(R.id.spinnerCrop);
spinnerArea = (Spinner) findViewById(R.id.spinnerArea);
ArrayAdapter<String> dataCropsAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, crops);
dataCropsAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> dataAreaAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, areas);
dataAreaAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerCrop.setAdapter(dataCropsAdapter);
spinnerArea.setAdapter(dataAreaAdapter);
rbGroup = (RadioGroup) findViewById(R.id.radioMethod);
rbGroup.setSelected(true);
etIncK = (EditText) findViewById(R.id.etK);
etIncP = (EditText) findViewById(R.id.etP);
etIncN = (EditText) findViewById(R.id.etN);
etPurpose = (EditText) findViewById(R.id.etPurpose);
tvK = (TextView) findViewById(R.id.tvKDisplay);
tvN = (TextView) findViewById(R.id.tvNDisplay);
tvP = (TextView) findViewById(R.id.tvPDisplay);
btnRecomm = (Button) findViewById(R.id.button);
btnRecomm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(checkDataEmpty()){
Toast.makeText(MainActivity.this, "请输入完数据后获得推荐", Toast.LENGTH_SHORT).show();
}else{
setUsage();
setTvK();
setTvN();
setTvP();
}
}
});
}
private long getItem(Spinner s){
return s.getSelectedItemId();
}
private int getWaterMethod() {
switch (rbGroup.getCheckedRadioButtonId()) {
case R.id.rbDot:
return DOT_WATER;
case R.id.rbOver:
return OVER_WATER;
}
return DOT_WATER;
}
public Long getPurpose() {
String str = etPurpose.getText().toString().trim();
purpose = Long.parseLong(str);
return purpose;
}
private double getN() {
String str = etIncN.getText().toString().trim();
cantainN = Double.parseDouble(str);
return cantainN;
}
private double getK() {
String str = etIncK.getText().toString().trim();
cantainK = Double.parseDouble(str);
return cantainK;
}
private double getP() {
String str = etIncP.getText().toString().trim();
cantainP = Double.parseDouble(str);
return cantainP;
}
private boolean checkDataEmpty() {
if (etIncK.getText().toString().trim() == null
|| etIncK.getText().toString().trim().equals("")
|| etIncN.getText().toString().trim() == null
|| etIncN.getText().toString().trim().equals("")
|| etIncP.getText().toString().trim() == null
|| etIncP.getText().toString().trim().equals("")
|| etPurpose.getText().toString().trim() == null
|| etPurpose.getText().toString().trim().equals("")) {
return true;
}
return false;
}
private void setTvN() {
double results;
YangFenXuQiu yaFenXuQiu = getYangFenXuQiu();
results = (yaFenXuQiu.getN() * getPurpose() / 100 - 3.662*getN() * 0.15 * Math.pow(getN(), -0.3843))/usageN ;
tvN.setText(df.format(results));
}
private YangFenXuQiu getYangFenXuQiu() {
YangFenXuQiu yaFenXuQiu;
yaFenXuQiu = lists.get((int)getItem(spinnerCrop));
return yaFenXuQiu;
}
private void setTvK() {
double results;
YangFenXuQiu yaFenXuQiu = getYangFenXuQiu();
results = (yaFenXuQiu.getN() * getPurpose() / 100 - getN() * 0.15 )/usageK ;
tvK.setText(df.format(results));
}
private void setTvP() {
double results;
YangFenXuQiu yaFenXuQiu = getYangFenXuQiu();
results = (yaFenXuQiu.getP2o5() * getPurpose() / 100 - 8.473 * getP() * 0.15 * Math.pow(getN(), -0.6745))/usageP ;
tvP.setText(df.format(results));
}
private void setUsage(){
if(getWaterMethod() == DOT_WATER){
usageK = 0.55;
usageN = 0.5;
usageP = 0.2;
}else{
usageK = 0.5;
usageN = 0.35;
usageP = 0.1;
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
@Override
protected void onDestroy() {
finish();
super.onDestroy();
}
}
//然后是获得数据库的, 这个是用的别人的
package cn.com.itisme.ctsf.dao;
/**
* Created by play on 2015/4/20.
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
/**
* This is a Assets Database Manager
* Use it, you can use a assets database file in you application
* It will copy the database file to "/data/data/[your application package name]/database" when you first time you use it
* Then you can get a SQLiteDatabase object by the assets database file
* @author RobinTang
* @time 2012-09-20
*
*
* How to use:
* 1. Initialize AssetsDatabaseManager
* 2. Get AssetsDatabaseManager
* 3. Get a SQLiteDatabase object through database file
* 4. Use this database object
*
* Using example:
* AssetsDatabaseManager.initManager(getApplication()); // this method is only need call one time
* AssetsDatabaseManager mg = AssetsDatabaseManager.getManager(); // get a AssetsDatabaseManager object
* SQLiteDatabase db1 = mg.getDatabase("db1.db"); // get SQLiteDatabase object, db1.db is a file in assets folder
* db1.??? // every operate by you want
* Of cause, you can use AssetsDatabaseManager.getManager().getDatabase("xx") to get a database when you need use a database
*/
public class AssetsDatabaseManager {
private static String tag = "AssetsDatabase"; // for LogCat
private static String databasepath = "/data/data/%s/databases"; // %s is packageName
// A mapping from assets database file to SQLiteDatabase object
private Map<String, SQLiteDatabase> databases = new HashMap<String, SQLiteDatabase>();
// Context of application
private Context context = null;
// Singleton Pattern
private static AssetsDatabaseManager mInstance = null;
/**
* Initialize AssetsDatabaseManager
* @param context, context of application
*/
public static void initManager(Context context){
if(mInstance == null){
mInstance = new AssetsDatabaseManager(context);
}
}
/**
* Get a AssetsDatabaseManager object
* @return, if success return a AssetsDatabaseManager object, else return null
*/
public static AssetsDatabaseManager getManager(){
return mInstance;
}
private AssetsDatabaseManager(Context context){
this.context = context;
}
/**
* Get a assets database, if this database is opened this method is only return a copy of the opened database
* @param dbfile, the assets file which will be opened for a database
* @return, if success it return a SQLiteDatabase object else return null
*/
public SQLiteDatabase getDatabase(String dbfile) {
if(databases.get(dbfile) != null){
Log.i(tag, String.format("Return a database copy of %s", dbfile));
return (SQLiteDatabase) databases.get(dbfile);
}
if(context==null)
return null;
Log.i(tag, String.format("Create database %s", dbfile));
String spath = getDatabaseFilepath();
String sfile = getDatabaseFile(dbfile);
File file = new File(sfile);
SharedPreferences dbs = context.getSharedPreferences(AssetsDatabaseManager.class.toString(), 0);
boolean flag = dbs.getBoolean(dbfile, false); // Get Database file flag, if true means this database file was copied and valid
if(!flag || !file.exists()){
file = new File(spath);
if(!file.exists() && !file.mkdirs()){
Log.i(tag, "Create \""+spath+"\" fail!");
return null;
}
if(!copyAssetsToFilesystem(dbfile, sfile)){
Log.i(tag, String.format("Copy %s to %s fail!", dbfile, sfile));
return null;
}
dbs.edit().putBoolean(dbfile, true).commit();
}
SQLiteDatabase db = SQLiteDatabase.openDatabase(sfile, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
if(db != null){
databases.put(dbfile, db);
}
return db;
}
private String getDatabaseFilepath(){
return String.format(databasepath, context.getApplicationInfo().packageName);
}
private String getDatabaseFile(String dbfile){
return getDatabaseFilepath()+"/"+dbfile;
}
private boolean copyAssetsToFilesystem(String assetsSrc, String des){
Log.i(tag, "Copy "+assetsSrc+" to "+des);
InputStream istream = null;
OutputStream ostream = null;
try{
AssetManager am = context.getAssets();
istream = am.open(assetsSrc);
ostream = new FileOutputStream(des);
byte[] buffer = new byte[1024];
int length;
while ((length = istream.read(buffer))>0){
ostream.write(buffer, 0, length);
}
istream.close();
ostream.close();
}
catch(Exception e){
e.printStackTrace();
try{
if(istream!=null)
istream.close();
if(ostream!=null)
ostream.close();
}
catch(Exception ee){
ee.printStackTrace();
}
return false;
}
return true;
}
/**
* Close assets database
* @param dbfile, the assets file which will be closed soon
* @return, the status of this operating
*/
public boolean closeDatabase(String dbfile){
if(databases.get(dbfile) != null){
SQLiteDatabase db = (SQLiteDatabase) databases.get(dbfile);
db.close();
databases.remove(dbfile);
return true;
}
return false;
}
/**
* Close all assets database
*/
static public void closeAllDatabase(){
Log.i(tag, "closeAllDatabase");
if(mInstance != null){
for(int i=0; i<mInstance.databases.size(); ++i){
if(mInstance.databases.get(i)!=null){
mInstance.databases.get(i).close();
}
}
mInstance.databases.clear();
}
}
}
// 布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="测土配方施肥专家系统"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25sp" />
<RelativeLayout
android:id="@+id/inforSetting"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp">
<TextView
android:id="@+id/tvInfor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="种植信息设置"
android:textSize="20sp" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvInfor"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="种植作物" />
<Spinner
android:id="@+id/spinnerCrop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="种植区域" />
<Spinner
android:id="@+id/spinnerArea"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/linearLayout"
android:layout_marginTop="5dp"
android:text="灌溉方式" />
<RadioGroup
android:id="@+id/radioMethod"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView2"
android:layout_marginLeft="50dp">
<RadioButton
android:id="@+id/rbDot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="滴灌" />
<RadioButton
android:id="@+id/rbOver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="淹灌" />
</RadioGroup>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/etPurpose"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/radioMethod"
android:text="目标产量" />
<EditText
android:id="@+id/etPurpose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radioMethod"
android:layout_toRightOf="@id/textView3"
android:ems="10"
android:inputType="number" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/etPurpose"
android:layout_below="@+id/radioMethod"
android:layout_toEndOf="@+id/etPurpose"
android:layout_toRightOf="@+id/etPurpose"
android:text="千克/亩" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/basicInfor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/inforSetting"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/tvBasic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="土壤肥力基础"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tvBasic"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="含氮量" />
<EditText
android:id="@+id/etN"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="含磷量" />
<EditText
android:id="@+id/etP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="含钾量" />
<EditText
android:id="@+id/etK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/basicInfor"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="获得推荐"
android:textSize="20sp" />
<RelativeLayout
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/tvResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="推荐施肥量"
android:textSize="20sp" />
<TextView
android:id="@+id/tvNlabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvResult"
android:layout_marginTop="20dp"
android:text="施氮量"
android:textSize="18sp" />
<TextView
android:id="@+id/tvNDisplay"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvNlabel"
android:layout_below="@+id/tvResult"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/tvNlabel" />
<TextView
android:id="@+id/tvPlabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvNlabel"
android:layout_below="@+id/tvResult"
android:layout_marginLeft="40dp"
android:layout_marginStart="20dp"
android:layout_toRightOf="@+id/tvNDisplay"
android:text="施磷量"
android:textSize="18sp" />
<TextView
android:id="@+id/tvPDisplay"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvNlabel"
android:layout_below="@+id/tvResult"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/tvPlabel" />
<TextView
android:id="@+id/tvKlebel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvNlabel"
android:layout_marginTop="10dp"
android:text="施钾量"
android:textSize="18sp" />
<TextView
android:id="@+id/tvKDisplay"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/tvKlebel"
android:layout_below="@+id/tvNDisplay"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/tvKlebel" />
</RelativeLayout>
</RelativeLayout>
上面还有 几个类没有列, 可以从已有的推出来,我就不写了哦