我正在将 editText 和 textView 写入我的应用程序中的数据库。我想显示他们通过特定列添加的内容;我有 DATE_SELECTED。我有一个主要障碍,任何帮助将不胜感激。以下是我在本节的代码:
public class CalendarDBAdapter
{
public static final String KEY_TITLE = "title";
public static final String DATE_SELECTED = "date";
public static final String WEIGHT_INPUT = "weight";
public static final String REPS_INPUT = "reps";
public static final String KEY_BODY = "body";
public static final String KEY_ROWID = "id";
private static final String TAG = "CalendarDBAdapter";
private DatabaseHelper myDBHelper;
private SQLiteDatabase myDatabase;
private static final String DATABASE_NAME = "exerciselog";
private static final String DATABASE_TABLE = "exercises";
private static final int DATABASE_VERSION = 1;
//Database creation sql statement
private static final String DATABASE_CREATE = "create table " + DATABASE_TABLE + "(" + KEY_ROWID + " integer primary key autoincrement, "
+ KEY_TITLE + DATE_SELECTED + WEIGHT_INPUT + REPS_INPUT + " text not null);";
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try
{
db.execSQL(DATABASE_CREATE);
}
catch (SQLException e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
db.execSQL("DROP TSBLE IF EXISTS exercises");
onCreate(db);
}
}
//constructor
public CalendarDBAdapter(Context ctx)
{
this.mCtx = ctx;
}
/*
* Open exercises database. If it cannot be opened, try to create a new instance of the database
* If it cannot be created, trow and exception to signal failure
* @return this (self reference, allowing this to be chained in an
* initialization call)
* @throws SQLException if the database could be neither opened or created
*/
public CalendarDBAdapter open()throws SQLException
{
myDBHelper = new DatabaseHelper(mCtx);
myDatabase = myDBHelper.getWritableDatabase();
return this;
}
public void close()
{
myDBHelper.close();
}
/*
* Create a new exercise using the title provided. If the note is
* successfully created return the new rowId for that exercise, otherwise return
* a -1 to indicate failure.
*
* @param title the title of the exercise
* @return rowId or -1 if failed
*/
public long createExercise(String title, String date, String weight, String reps, String body)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(DATE_SELECTED, date);
initialValues.put(WEIGHT_INPUT, weight);
initialValues.put(REPS_INPUT, reps);
initialValues.put(KEY_BODY, body);
return myDatabase.insert(DATABASE_TABLE, null, initialValues);
}
/*
* Delete exercise with given rowId
* @param rowId id of exercise to delete
* @return true if deleted, false otherwise
*/
public boolean deleteExercise(long rowID)
{
return myDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowID, null) > 0;
}
/**
* Return a Cursor over the list of all exercise inputs in the database
*
* @return Cursor over all exercise inputs
*/
public Cursor fetchAllExercises()
{
return myDatabase.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, DATE_SELECTED, WEIGHT_INPUT, REPS_INPUT, KEY_BODY}, null, null, null, null, null);
}
/**
* Return a Cursor positioned at the exercise that matches the given rowId
*
* @param rowId id of exercise to retrieve
* @return Cursor positioned to matching note, if found
* @throws SQLException if note could not be found/retrieved
*/
public Cursor fetchExerciss(long rowId)throws SQLException
{
Cursor myCursor =
myDatabase.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, DATE_SELECTED, WEIGHT_INPUT, REPS_INPUT, KEY_BODY}, KEY_ROWID + "=" + rowId,
null, null, null, null, null);
if(myCursor != null)
{
myCursor.moveToFirst();
}
return myCursor;
}
/**
* Update the exercises using the details provided. The note to be updated is
* specified using the rowId, and it is altered to use the title values passed in
*
* @param rowId id of exercise to update
* @param title value to set exercise title to
* @param body value to set exercise body to
* @return true if the exercise was successfully updated, false otherwise
*/
public boolean updateExercise(long rowID, String title, String date, String weight, String reps, String body)
{
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
args.put(DATE_SELECTED, date);
args.put(WEIGHT_INPUT, weight);
args.put(REPS_INPUT, reps);
args.put(KEY_BODY, body);
return myDatabase.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowID, null) > 0;
}
public Cursor sortAllRows()
{
return myDatabase.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, DATE_SELECTED, WEIGHT_INPUT, REPS_INPUT, KEY_BODY},
null, null, null, null, DATE_SELECTED + " ASC");
}
}
活动:
package com.mobifit;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.SimpleFormatter;
import com.mobifit.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AddToCalendar extends Activity
{
private Button favBtn;
private Button timerBtn;
private Button editBtn;
private Button selectedDMY;
private Button addCalBtn;
private TextView exercise, exerciseDB, weightText, repsText, notesText;
private String textPassed, datePassed;
private EditText weightInput, repsInput, notesInput;
private Long myRowID;
private CalendarDBAdapter myDBHelper;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
myDBHelper = new CalendarDBAdapter(this);
myDBHelper.open();
setContentView(R.layout.add_to_calendar);
//set view from xml
favBtn = (Button)this.findViewById(R.id.favoriteBtn);
timerBtn = (Button)this.findViewById(R.id.timerBtn);
editBtn = (Button)this.findViewById(R.id.editBtn);
addCalBtn = (Button)this.findViewById(R.id.addToCalBtn);
weightInput = (EditText)this.findViewById(R.id.weightInput);
repsInput = (EditText)this.findViewById(R.id.repsInput);
notesInput = (EditText)this.findViewById(R.id.notesInput);
exerciseDB = (TextView)this.findViewById(R.id.exerciseDB);
weightText = (TextView)this.findViewById(R.id.weight);
repsText = (TextView)this.findViewById(R.id.repsText);
notesText = (TextView)this.findViewById(R.id.notesText);
//set date
selectedDMY = (Button)this.findViewById(R.id.selectedDayMonthYear);
datePassed = GlobalVars.getDate();
if(datePassed == null)
{
Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
Integer.toString(day);
Integer.toString(month);
Integer.toString(year);
selectedDMY.setText("Today" + month + "-" + day + "-" + year + "\n(click to change)");
}
else
{
selectedDMY.setText(datePassed + "\n(click to change)");
}
exercise = (TextView)this.findViewById(R.id.exercise);
//enable button
favBtn.setEnabled(true);
timerBtn.setEnabled(true);
editBtn.setEnabled(true);
selectedDMY.setEnabled(true);
//set text for exercise passed
textPassed = GlobalVars.getVar();
exercise.setText(textPassed);
//rowID for database
myRowID = (savedInstanceState == null) ? null: (Long)savedInstanceState.getSerializable(CalendarDBAdapter.KEY_ROWID);
if(myRowID == null)
{
Bundle extras = getIntent().getExtras();
myRowID = extras != null ? extras.getLong(CalendarDBAdapter.KEY_ROWID): null;
}
//populateFields();
addCalBtn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//previous code
/*setResult(RESULT_OK);
finish();*/
}
});
}
public void dateClick(View v)
{
startActivity(new Intent(this, MobiFitCalendar.class));
}
//add the data to the calendar for display
public void addToCalBtnClick()
{
}
private void populateFields()
{
if(myRowID != null)
{
Cursor exerciseInput = myDBHelper.fetchExerciss(myRowID);
startManagingCursor(exerciseInput);
weightInput.setText(exerciseInput.getString(exerciseInput.getColumnIndexOrThrow(CalendarDBAdapter.WEIGHT_INPUT)));
repsInput.setText(exerciseInput.getString(exerciseInput.getColumnIndexOrThrow(CalendarDBAdapter.REPS_INPUT)));
notesInput.setText(exerciseInput.getString(exerciseInput.getColumnIndexOrThrow(CalendarDBAdapter.KEY_BODY)));
}
}
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
saveState();
outState.putSerializable(CalendarDBAdapter.KEY_ROWID, myRowID);
}
@Override
protected void onPause()
{
super.onPause();
saveState();
}
@Override
protected void onResume()
{
super.onResume();
populateFields();
}
private void saveState()
{
String title = exercise.toString();
String weight = weightInput.getText().toString();
String reps = weightInput.getText().toString();
String notes = notesInput.getText().toString();
String dateSelected = selectedDMY.toString();
if(myRowID == null)
{
long id = myDBHelper.createExercise(title, dateSelected, weight, reps, notes);
if(id > 0)
{
myRowID = id;
}
}
else
{
myDBHelper.updateExercise(myRowID, title, dateSelected, weight, reps, notes);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add_to_calendar, menu);
return true;
}
}