有人可以帮助我完成我的大学项目吗?
我正在尝试将 BMI 的值存储到 SQLite 中。
我有databaseHelper设置和所有这些,只是不确定如何在将其设置为TextView时存储它。
我想存储并显示结果。
下面的代码可以工作并显示它,我只是不知道如何存储它。
package ie.wit.fitnessmadeeasy;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.NumberPicker;
import android.widget.TextView;
import static ie.wit.fitnessmadeeasy.R.id.imageViewBMI;
public class BmiActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmi);
final Button calc = (Button) findViewById(R.id.calc);
final NumberPicker weight = (NumberPicker) findViewById(R.id.et_weight);
final NumberPicker height = (NumberPicker) findViewById(R.id.et_height);
final TextView result = (TextView) findViewById(R.id.result);
final ImageView image = (ImageView) findViewById(imageViewBMI);
weight.setMinValue(30);
weight.setMaxValue(300);
height.setMinValue(30);
height.setMaxValue(300);
calc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// int weightlbs = weight.getValue();
// int heightcm = height.getValue();
// int weights = weight.getValue();
// int heights = height.getValue();
// String weightStr = String.valueOf(weights);
// String heightStr = String.valueOf(heights);
// double kg = 0.45;
//String kgs = String.valueOf(kg);
// double meter = 0.025;
String heightStr = "" + (height.getValue());
String weightStr = "" + (weight.getValue());
// String heightStr = height.getText().toString();
// String weightStr = weight.getText().toString();
if (heightStr != null && !"".equals(heightStr)
&& weightStr != null && !"".equals(weightStr)) {
double heightValue = Double.parseDouble(heightStr)/100;
double weightValue = Double.parseDouble(weightStr);
double bmi = weightValue/ (heightValue * heightValue) ;
String bmiLabel;
if (bmi <= 15) {
bmiLabel = getString(R.string.very_very_skinny);
image.setImageResource(R.drawable.skinny);
} else if (bmi > 15 && bmi <= 16) {
bmiLabel = getString(R.string.very_skinny);
image.setImageResource(R.drawable.skinny);
} else if (bmi > 16 && bmi <= 18.5) {
bmiLabel = getString(R.string.skinny);
image.setImageResource(R.drawable.skinny);
} else if (bmi > 18.5 && bmi <= 25) {
bmiLabel = getString(R.string.normal);
image.setImageResource(R.drawable.normal);
} else if (bmi > 25 && bmi <= 30) {
bmiLabel = getString(R.string.overweight);
image.setImageResource(R.drawable.fat);
} else if (bmi > 30 && bmi <= 35) {
bmiLabel = getString(R.string.obese_series_i);
image.setImageResource(R.drawable.fat);
} else if (bmi > 35 && bmi <= 40) {
bmiLabel = getString(R.string.obese_series_ii);
image.setImageResource(R.drawable.fat);
} else {
bmiLabel = getString(R.string.obese_series_iii);
image.setImageResource(R.drawable.fat);
}
bmiLabel ="" + bmi;
result.setText(bmiLabel);
}
}
});
}
}
//////////////////////////////////////////这是databasehelper的代码 SQLite数据库数据库;
private static final String TABLE_CREATE = "create table users (id integer primary key autoincrement, " +
" name text not null, username text not null, password text not null, bmi double not null);";
public DataBaseHelper(Context context)
{
super(context , DATABASE_NAME , null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
this.db = db;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(query);
this.onCreate(db);
}
public String searchPassstr(String et_username){
db = this.getReadableDatabase();
String query = "select * from users";
String unstr, passstr;
Cursor cursor = db.rawQuery(query, null);
passstr = "Password not Found!!";
if(cursor.moveToFirst())
{
do{
unstr = cursor.getString(2);
if(unstr.equals(et_username)) {
passstr = cursor.getString(3);
break;
}
}
while(cursor.moveToNext());
}
return passstr;
}
public String getName() throws SQLException {
String name = "";
Cursor cursor = this.getReadableDatabase().query(
TABLE_NAME, new String[] { COLUMN_NAME },
null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
name = cursor.getString(0);
} while (cursor.moveToNext());
}
cursor.close();
return name;
}
public String getUsername() throws SQLException {
String username = "";
Cursor cursor = this.getReadableDatabase().query(
TABLE_NAME, new String[] { COLUMN_USERNAME },
null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
username = cursor.getString(0);
} while (cursor.moveToNext());
}
cursor.close();
return username;
}
public String getResult() throws SQLException {
String result = "";
Cursor cursor = this.getReadableDatabase().query(
TABLE_NAME, new String[] { COLUMN_BMI },
null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
result = cursor.getString(0);
} while (cursor.moveToNext());
}
cursor.close();
return result;
}
public void insertUser(RegRequest reg) {
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "select * from users";
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount();
values.put(COLUMN_ID, count);
values.put(COLUMN_NAME, reg.getEt_name());
values.put(COLUMN_USERNAME, reg.getEt_username());
values.put(COLUMN_PASSWORD, reg.getEt_password());
values.put(COLUMN_BMI, reg.getResult());
// values.put(COLUMN_AGE, reg.getEt_password());
db.insert(TABLE_NAME, null, values);
db.close();
}
最佳答案
使用 AsyncTask
执行数据库事务。
将DataBaseHelper
对象初始化为:
DataBaseHelper dataBaseHelper = new DataBaseHelper(context);
获取SQLiteDatabase
引用为:
SQLiteDatabase sqLiteDatabase = dataBaseHelper.getWritableDatabase();
使用ContentValues
来存储所需的值
ContentValues contentValues = new ContentValues();
contentValues.put("bmi", bmiValue)
contentValues.put("user_id", userID)
然后插入到相应的表中:
sqLiteDatabase.insert("<tableName>", null, contentValues);
关于java - 将 BMI 存储在 SQLite 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42614689/