我正在尝试更新我的数据库,并且已经传递了内容值,包括ID,我需要检索更新语句的ID。
public int updateEntryById(ContentValues diaryValues) {
ContentValues updateValues = new ContentValues();
String diaryID = updateValues.getAsString(Diary.DiaryItem.COLUMN_NAME_ID);
Log.i(TAG, "ID" + diaryID);
String updateWhere = Diary.DiaryItem.COLUMN_NAME_ID + " = " + diaryID;
Log.i(TAG, "UpdateWhere query values" + updateWhere);
SQLiteDatabase db = diaryDbHelper.getWritableDatabase();
我创建了一个内容值的实例,并将id作为要传递的字符串。
但是logcat给了我id = null
更新条目有更多代码,但具体问题与ID有关。
任何想法?
public class UpdateActivity extends Activity implements LocationListener {
private TextView mID;
private EditText mTitle;
private EditText mDate;
private EditText mTime;
private EditText mEntry;
private TextView mLat;
private TextView mLong;
private ImageButton mSaveButton;
private DAO getDAO;
private int mYear;
private int mMonth;
private int mDay;
private int mHour;
private int mMinute;
static final int DATE_DIALOG_ID = 0;
static final int TIME_DIALOG_ID = 1;
private final String TAG ="UpdateActivity";
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.update_entry); //Sets the view for Diary Entry
/**References to the fields where data is inputted**/
mTitle = (EditText) findViewById(R.id.diaryform_et_title);
mDate= (EditText) findViewById(R.id.diaryform_et_date);
mTime= (EditText) findViewById(R.id.diaryform_et_time);
mEntry= (EditText) findViewById(R.id.diaryform_et_entry);
mLat= (TextView) findViewById(R.id.diaryform_tv_lat);
mLong= (TextView) findViewById(R.id.diaryform_tv_long);
mID = (TextView) findViewById(R.id.diaryform_tv_ID);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
updateDateDisplay();
updateTimeDisplay();
mSaveButton = (ImageButton) findViewById(R.id.diaryform_ib_save); //Reference to the save button is stored in mSaveButton
Intent showTitle = getIntent();
String fieldTitle = showTitle.getStringExtra(Constants.DIARY_TITLE);
mTitle.setText(fieldTitle);
Log.i(TAG,"Field title 2: "+ fieldTitle);
protected void saveDiaryItem() {
ContentValues saveValues = new ContentValues();
saveValues.put(Diary.DiaryItem.COLUMN_NAME_ID, mID.getText().toString());
saveValues.put(Diary.DiaryItem.COLUMN_NAME_DIARY_TITLE, mTitle.getText().toString());
saveValues.put(Diary.DiaryItem.COLUMN_NAME_DIARY_DATE, mDate.getText().toString());
saveValues.put(Diary.DiaryItem.COLUMN_NAME_DIARY_TIME, mTime.getText().toString());
saveValues.put(Diary.DiaryItem.COLUMN_NAME_DIARY_ENTRY, mEntry.getText().toString());
saveValues.put(Diary.DiaryItem.COLUMN_NAME_DIARY_LAT, mLat.getText().toString());
saveValues.put(Diary.DiaryItem.COLUMN_NAME_DIARY_LONG, mLong.getText().toString());
Log.i(TAG, "About to insert data" + saveValues);
DAO diarySave = new DAO(getBaseContext());
diarySave.updateEntryById(saveValues);
Log.i(TAG, "IM BACK!");
}
答案 0 :(得分:0)
public int updateEntryById(ContentValues diaryValues) {
ContentValues updateValues = new ContentValues();
String diaryID = updateValues.getAsString(Diary.DiaryItem.COLUMN_NAME_ID);
您正在尝试从刚创建的空updateValues
对象中获取ID。也许你的意思是diaryValues.getAsString()
。