2017-03-05 7 views
1

誰でも私の大学のプロジェクトで私を助けることができますか?
私はBMIの値をSQLiteに格納しようとしています。SQLiteにBMIを保存する

私は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); 


      } 


     } 
    }); 

} 
} 

//////////////////////////////////////////このデータベースヘルパーのコードです SQLiteDatabase db;

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(); 
} 
+0

bmiをdatabaseHelperに渡す必要があります。これは、これまでに設定したとおりです。これはそうでなければ本当の質問 –

+0

私は以前の値を渡している彼らはeditTextのされていると私はAndroidスタジオとSqliteに新しいです。このコードでは、どのようなコードでbmiを渡すのですか? – IrishProgrammer

+0

sqliteとdatabaseHelpersの使い方はどこから学んだのですか? –

答えて

0

データベーストランザクションを行うにはAsyncTaskを使用してください。

初期DataBaseHelperオブジェクトとして:

DataBaseHelper dataBaseHelper = new DataBaseHelper(context); 

としてSQLiteDatabase参照を取得:

:必要な値

ContentValues contentValues = new ContentValues(); 
contentValues.put("bmi", bmiValue) 
contentValues.put("user_id", userID) 

を格納する

SQLiteDatabase sqLiteDatabase = dataBaseHelper.getWritableDatabase(); 

使用ContentValuesそしてように、適切なテーブルに挿入

sqLiteDatabase.insert("<tableName>", null, contentValues); 
関連する問題