Reputation: 229
I am trying to update the record, whenever I am updating a record new id is generated with the updated values. The update should happen to the same id. What can be the reason?
Create table :
public void createTable(SQLiteDatabase db){
String CREATE_EVENTS_TABLE = "CREATE TABLE " + TABLE_EVENTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_TITLE + " TEXT,"
+ KEY_FROM_DATE + " DATE,"
+ KEY_TO_DATE + " DATE,"
+ KEY_DAY_OF_WEEK + " TEXT,"
+ KEY_LOCATION + " TEXT,"
+ KEY_NOTIFICATION_TIME + " DATE" + ")";
db.execSQL(CREATE_EVENTS_TABLE);
}
update function :
public int updateEvent(EventData event) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE,event.getTitle());
values.put(KEY_FROM_DATE,event.getFromDate());
values.put(KEY_TO_DATE,event.getToDate());
values.put(KEY_DAY_OF_WEEK,event.getDayOfWeek());
values.put(KEY_LOCATION,event.getLocation());
values.put(KEY_NOTIFICATION_TIME,event.getNotificationTime());
// updating row
return db.update(TABLE, values, KEY_ID + " = ?",
new String[] { String.valueOf(event.getId()) });
}
Calling update function :
db = new EventTableHelper(getApplicationContext());
eventData = new EventData();
db.updateEvent(eventData);
Thank you.
EDIT :
My constructor in EventData is this:
public EventData(String title,String fromDate,String toDate,String dayOfWeek,String location,String notificationTime){
this.title = title;
this.fromDate = fromDate;
this.toDate = toDate;
this.dayOfWeek = dayOfWeek;
this.location = location;
this.notificationTime = notificationTime;
}
and I am adding and updating value using eventData object:
db.addEvent(new EventData(eventTitle, startTime, endTime, dayOfWeek, location,notificationTime));
db.updateEvent(eventData);
The id is getting increased, I am not passing any value to id.
EventData class
public class EventData {
public int id;
public String title;
public String fromDate;
public String toDate;
public String location;
public String dayOfWeek;
public String notificationTime;
public EventData(){}
public EventData(String title,String fromDate,String toDate, String location){
this.title = title;
this.fromDate = fromDate;
this.toDate = toDate;
this.location = location;
}
public EventData(int id,String title,String fromDate,String toDate,String dayOfWeek,String location,String notificationTime){
this.title = title;
this.fromDate = fromDate;
this.toDate = toDate;
this.dayOfWeek = dayOfWeek;
this.location = location;
this.notificationTime = notificationTime;
}
/* public EventData(String title,String fromDate,String toDate,String dayOfWeek,String location,String notificationTime){
this.id = id;
this.title = title;
this.fromDate = fromDate;
this.toDate = toDate;
this.dayOfWeek = dayOfWeek;
this.location = location;
this.notificationTime = notificationTime;
}*/
public void setId(int id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setFromDate(String fromDate) {
this.fromDate = fromDate;
}
public void setToDate(String toDate) {
this.toDate = toDate;
}
public void setLocation(String location) {
this.location = location;
}
public void setDayOfWeek(String dayofWeek) {
this.dayOfWeek = dayofWeek;
}
public void setNotificationTime(String notificationTime) {
this.notificationTime = notificationTime;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public String getFromDate() {
return fromDate;
}
public String getToDate() {
return toDate;
}
public String getLocation() {
return location;
}
public String getDayOfWeek() {
return dayOfWeek;
}
public String getNotificationTime() {
return notificationTime;
}
}
I am creating events with child view so I want to update the event on which i will click. For that i have used setTag method to pass id of view.
This is my day fragment
dayplanView = (ViewGroup) view.findViewById(R.id.hoursRelativeLayout);
int id = i.getIntExtra("id",0);
mDb = new EventTableHelper(getActivity());
events = mDb.getAllEvents("Mon");
int tag = 0;
for (EventData eventData : events) {
String datefrom = eventData.getFromDate();
if (datefrom != null) {
String[] times = datefrom.substring(11, 16).split(":");
minutesFrom = Integer.parseInt(times[0]) * 60 + Integer.parseInt(times[1]);
}
String dateTo = eventData.getToDate();
String title = eventData.getTitle();
String location = eventData.getLocation();
if (dateTo != null) {
String[] times1 = dateTo.substring(11, 16).split(":");
minutesTo = Integer.parseInt(times1[0]) * 60 + Integer.parseInt(times1[1]);
}
createEvent(inflater, dayplanView, minutesFrom, minutesTo, title, location, tag);
tag++;
}
return view;
}
private void createEvent(LayoutInflater inflater, ViewGroup dayplanView, int fromMinutes, int toMinutes, String title,String location,int tag) {
final View eventView = inflater.inflate(R.layout.event_view, dayplanView, false);
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) eventView.getLayoutParams();
RelativeLayout container = (RelativeLayout) eventView.findViewById(R.id.container);
TextView tvTitle = (TextView) eventView.findViewById(R.id.textViewTitle);
if (tvTitle.getParent() != null)
((ViewGroup) tvTitle.getParent()).removeView(tvTitle);
if(location.equals(""))
{
tvTitle.setText("Event : " + title);
}
else
{
tvTitle.setText("Event : " + title + " (At : " + location +")");
}
int distance = (toMinutes - fromMinutes);
layoutParams.topMargin = dpToPixels(fromMinutes + 9);
layoutParams.height = dpToPixels(distance);
eventView.setLayoutParams(layoutParams);
dayplanView.addView(eventView);
container.addView(tvTitle);
eventView.setTag(tag);
eventView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i = new Intent(getActivity(),AddEventActivity.class);
editMode = true;
i.putExtra("EditMode",editMode);
int tag = 0;
tag =(int)v.getTag();
i.putExtra("tag",tag);
startActivityForResult(i,1);
}
});
}
Upvotes: 0
Views: 93
Reputation: 8562
As we discussed in comments. I think you don't need to be so complex as you are being now,
Just simply do like this,
db.update(TABLE, values, KEY_ID = id, null);
where id means the Integer value OR the value of column KEY_ID
that you want to update. Like the value of id will be 5 if you want to update the row with id of 5.
Upvotes: 2