我已经完成了关于从数据库访问数据的所有帖子,虽然我无法解决我的问题所以我在这个网站上发布我的问题。我想访问先前存储的SQL表。我的代码可以很好地存储数据但是当我点击保存按钮但是当我按下加载按钮时它不起作用意味着它插入空值而不是存储值。
这是我的代码..
public class SetValue extends Fragment {
@Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
populateData();
}
Button save, Load;
EditText firstName, name, patientID, minH, maxH, minI, maxI, minB, maxB;
private String nameofP,FName,pID;
private String HRM,HRX,IBIM,IBIX,BRM,BRX;
private String nameET,firstNameET,pIDET;
private String minHET,maxHET,minIET,maxIET,minBET,maxBET;
Patient patient=new Patient();
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
populateData();
}
@SuppressWarnings("deprecation")
DatabaseHandler populateData() {
// TODO Auto-generated method stub
DatabaseHandler mDatabase= new DatabaseHandler(getActivity());
SQLiteDatabase sql=mDatabase.getReadableDatabase();
Cursor cursor = sql.query(mDatabase.NAME_TABLE, null, null, null, null, null, null);
getActivity().startManagingCursor(cursor);
while (cursor.moveToNext()) {
String cFirstname = cursor.getString(cursor.getColumnIndex( mDatabase.KEY_NAME));
String cName = cursor.getString(cursor.getColumnIndex( mDatabase.KEY_HR));
String cPID = cursor.getString(cursor.getColumnIndex( mDatabase.KEY_IBI));
String cHeartRateMin = cursor.getString(cursor.getColumnIndex( mDatabase.KEY_HMIN));
String cHeartRateMax = cursor.getString(cursor.getColumnIndex( mDatabase.KEY_HMAX));
String cIBIMin = cursor.getString(cursor.getColumnIndex( mDatabase.KEY_IMIN));
String cIBIMax = cursor.getString(cursor.getColumnIndex( mDatabase.KEY_IMAX));
String cBreathingRateMin = cursor.getString(cursor.getColumnIndex( mDatabase.KEY_BMIN));
String cBreathingRateMax = cursor.getString(cursor.getColumnIndex( mDatabase.KEY_BMAX));
Patient currentPatient= new Patient();
currentPatient.setName(cName);
currentPatient.setFirstName(cFirstname);
currentPatient.setId(cPID);
currentPatient.setNumHeartRateMin(cHeartRateMin);
currentPatient.setNumHeartRateMax(cHeartRateMax);
currentPatient.setNumInterBeatIntervalMin(cIBIMin);
currentPatient.setNumInterBeatIntervalMax(cIBIMax);
currentPatient.setNumBreathingrateMin(cBreathingRateMin);
currentPatient.setNumBreathingrateMax(cBreathingRateMax);
}
sql.close();
return mDatabase;
}
DatabaseHandler dbhandler = new DatabaseHandler(getActivity());
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
final View myFragmentView = inflater.inflate(
R.layout.set_value_fragment, container, false);
save = (Button) myFragmentView.findViewById(R.id.Save);
save.setOnClickListener(onClickSave(myFragmentView));
Load = (Button) myFragmentView.findViewById(R.id.btn4);
Load.setOnClickListener(onClickLoad(myFragmentView));
firstName=(EditText)myFragmentView.findViewById(R.id.FN);
name=(EditText)myFragmentView.findViewById(R.id.name);
patientID=(EditText)myFragmentView.findViewById(R.id.PID);
minH=(EditText)myFragmentView.findViewById(R.id.Hmin);
maxH=(EditText)myFragmentView.findViewById(R.id.Hmax);
minI=(EditText)myFragmentView.findViewById(R.id.Imin);
maxI=(EditText)myFragmentView.findViewById(R.id.Imax);
minB=(EditText)myFragmentView.findViewById(R.id.Bmin);
maxB=(EditText)myFragmentView.findViewById(R.id.Bmax);
return myFragmentView;
}
private OnClickListener onClickLoad(View myFragmentView) {
// TODO Auto-generated method stub
return new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
firstName.setText(nameofP);
name.setText(FName);
patientID.setText(pID);
minH.setText(String.valueOf(HRM));
maxH.setText(String.valueOf(HRX));
minI.setText(String.valueOf(IBIM));
maxI.setText(String.valueOf(IBIX));
minB.setText(String.valueOf(BRM));
maxB.setText(String.valueOf(BRX));
updateDetails(patient);
}
private void updateDetails(Patient patient) {
// TODO Auto-generated method stub
DatabaseHandler androidDbHelper = new DatabaseHandler(getActivity());
SQLiteDatabase sql=androidDbHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(androidDbHelper.KEY_NAME, nameET);
contentValues.put(androidDbHelper.KEY_HR, firstNameET);
contentValues.put(androidDbHelper.KEY_IBI, pIDET);
contentValues.put(androidDbHelper.KEY_HMIN, minHET);
contentValues.put(androidDbHelper.KEY_HMAX, maxHET);
contentValues.put(androidDbHelper.KEY_IMIN, minIET);
contentValues.put(androidDbHelper.KEY_IMAX, maxIET);
contentValues.put(androidDbHelper.KEY_BMIN, minBET);
contentValues.put(androidDbHelper.KEY_BMAX, maxBET);
String[] clauseArgument= new String[1];
clauseArgument[0]=patient.getName();
System.out.println("whereClauseArgument[0] is :" + clauseArgument[0]);
sql.update(androidDbHelper.NAME_TABLE, contentValues, androidDbHelper.KEY_NAME+"=?", clauseArgument);
sql.close();
}
};
}
private OnClickListener onClickSave(final View myFragmentView) {
return new OnClickListener() {
/**
*
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
//set the values that we extracted
nameET=firstName.getText().toString();
firstNameET=name.getText().toString();
pIDET=patientID.getText().toString();
minHET=minH.getText().toString();
maxHET= maxH.getText().toString();
minIET= minI.getText().toString();
maxIET=maxI.getText().toString();
minBET=minB.getText().toString();
maxBET=maxB.getText().toString();
patient.setName(nameofP);
patient.setFirstName(FName);
patient.setId(pID);
patient.setNumHeartRateMin(HRM);
patient.setNumHeartRateMax(HRX);
patient.setNumInterBeatIntervalMin(IBIM);
patient.setNumInterBeatIntervalMax(IBIX);
patient.setNumBreathingrateMin(BRM);
patient.setNumBreathingrateMax(BRX);
insertData(patient);
}
public void insertData(Patient patient) {
DatabaseHandler androidDbHelper = new DatabaseHandler(getActivity());
SQLiteDatabase sql=androidDbHelper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(androidDbHelper.KEY_NAME, patient.getName());
contentValues.put(androidDbHelper.KEY_HR, patient.getFirstName());
contentValues.put(androidDbHelper.KEY_IBI, patient.getId());
contentValues.put(androidDbHelper.KEY_HMIN, patient.getNumHeartRateMin());
contentValues.put(androidDbHelper.KEY_HMAX, patient.getNumHeartRateMax());
contentValues.put(androidDbHelper.KEY_IMIN, patient.getNumInterBeatIntervalMin());
contentValues.put(androidDbHelper.KEY_IMAX, patient.getNumInterBeatIntervalMax());
contentValues.put(androidDbHelper.KEY_BMIN, patient.getNumBreathingrateMin());
contentValues.put(androidDbHelper.KEY_BMAX, patient.getNumBreathingrateMax());
long columnID=sql.insert(androidDbHelper.NAME_TABLE, null, contentValues);
sql.close();
Toast.makeText(getActivity().getBaseContext(), "Values inserted column ID is :" + columnID, Toast.LENGTH_SHORT).show();
}
};
}
}
这是我的DatadaseHandler.java
public class DatabaseHandler extends SQLiteOpenHelper {
// Database fields
public SQLiteDatabase db;
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
private static final int DATABASE_VERSION = 3;
// Database Name
private static final String DATABASE_NAME = "dataManager";
public static final String NAME_TABLE = "nametable";
// Contacts table name
public static final String KEY_NAME = "name";
public static final String KEY_HR = "firstName";
public static final String KEY_IBI = "patientID";
public static final String KEY_HMIN = "MinimumofHeartRate";
public static final String KEY_HMAX = "MaximumofHeartRate";
public static final String KEY_IMIN = "MinimumofIBI";
public static final String KEY_IMAX = "MaximumofIBI";
public static final String KEY_BMIN = "MinimumofBreathingRate";
public static final String KEY_BMAX = "MaximumofBreathingRate";
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ", ";
// Creating Tables
static final String DATABASE_CREATE_FIRST = "CREATE TABLE IF NOT EXISTS "
+ NAME_TABLE + "(" + KEY_NAME + TEXT_TYPE + COMMA_SEP + KEY_HR
+ TEXT_TYPE + COMMA_SEP + KEY_IBI + TEXT_TYPE + COMMA_SEP
+ KEY_HMIN + TEXT_TYPE + COMMA_SEP + KEY_HMAX + TEXT_TYPE
+ COMMA_SEP + KEY_IMIN + TEXT_TYPE + COMMA_SEP + KEY_IMAX
+ TEXT_TYPE + COMMA_SEP + KEY_BMIN + TEXT_TYPE + COMMA_SEP
+ KEY_BMAX + TEXT_TYPE + ")";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE_FIRST);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_CREATE_FIRST);
onCreate(db);
}
这是我的getter和setter变量:: patient.java
public class Patient {
private String id;
private String firstName;
private String name;
private String numHeartRateMin, numHeartRateMax, numInterBeatIntervalMin, numInterBeatIntervalMax,
numBreathingrateMin, numBreathingrateMax;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumHeartRateMin() {
return numHeartRateMin;
}
public void setNumHeartRateMin(String numHeartRateMin) {
this.numHeartRateMin = numHeartRateMin;
}
public String getNumHeartRateMax() {
return numHeartRateMax;
}
public void setNumHeartRateMax(String numHeartRateMax) {
this.numHeartRateMax = numHeartRateMax;
}
public String getNumInterBeatIntervalMin() {
return numInterBeatIntervalMin;
}
public void setNumInterBeatIntervalMin(String numInterBeatIntervalMin) {
this.numInterBeatIntervalMin = numInterBeatIntervalMin;
}
public String getNumInterBeatIntervalMax() {
return numInterBeatIntervalMax;
}
public void setNumInterBeatIntervalMax(String numInterBeatIntervalMax) {
this.numInterBeatIntervalMax = numInterBeatIntervalMax;
}
public String getNumBreathingrateMin() {
return numBreathingrateMin;
}
public void setNumBreathingrateMin(String numBreathingrateMin) {
this.numBreathingrateMin = numBreathingrateMin;
}
public String getNumBreathingrateMax() {
return numBreathingrateMax;
}
public void setNumBreathingrateMax(String numBreathingrateMax) {
this.numBreathingrateMax = numBreathingrateMax;
}
}
我知道,我放了很长的代码,但相信我这些是描述我的问题所必需的......
答案 0 :(得分:0)
您正在Pacient中插入空字符串并保存在数据库中。
首先:
创建Global Pacient:
Patient currentPatient= new Patient();
之后,在onClickLoad:
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
firstName.setText(currentPatient.getName());
name.setText(currentPatient.getFirstName());
patientID.setText(currentPatient.getId());
minH.setText(currentPatient.getNumHeartRateMin());
maxH.setText(currentPatient.getNumHeartRateMax());
minI.setText(currentPatient.getNumInterBeatIntervalMin());
maxI.setText(currentPatient.getNumInterBeatIntervalMax());
minB.setText(currentPatient.getNumBreathingrateMin());
maxB.setText(currentPatient.getNumBreathingrateMax());
updateDetails(patient);
}
这会修复保存操作。
nameET=firstName.getText().toString();
firstNameET=name.getText().toString();
pIDET=patientID.getText().toString();
minHET=minH.getText().toString();
maxHET= maxH.getText().toString();
minIET= minI.getText().toString();
maxIET=maxI.getText().toString();
minBET=minB.getText().toString();
maxBET=maxB.getText().toString();
patient.setName(nameET);
patient.setFirstName(firstNameET);
patient.setId(pIDET);
patient.setNumHeartRateMin(minHET);
patient.setNumHeartRateMax(maxHET);
patient.setNumInterBeatIntervalMin(minIET);
patient.setNumInterBeatIntervalMax(maxIET);
patient.setNumBreathingrateMin(minBET);
patient.setNumBreathingrateMax(maxBET);
insertData(patient);