2016-07-06 89 views
1

我正在開發一個應用程序。當我修改我的數據時,將顯示先前的數據,而不是更新的數據。這是ModifyActivity.java文件:SQLite數據庫沒有在應用程序內更新

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 

public class ModifyCountryActivity extends Activity implements OnClickListener { 

private EditText titleText, dateText, timeText; 
private Button updateBtn, deleteBtn; 

public Calendars calendars; 


private DatabaseHelper dbHelper; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setTitle("Modify Record"); 

    setContentView(R.layout.activity_modify_record); 

    dbHelper = new DatabaseHelper(this); 
    calendars = new Calendars(); 

    titleText = (EditText) findViewById(R.id.title_edittext_modify); 
    timeText = (EditText) findViewById(R.id.time_edittext_modify); 
    dateText = (EditText) findViewById(R.id.date_edittext_modify); 

    updateBtn = (Button) findViewById(R.id.btn_update); 
    deleteBtn = (Button) findViewById(R.id.btn_delete); 

    Intent intent = getIntent(); 
    String title = intent.getStringExtra("title"); 
    String time = intent.getStringExtra("time"); 
    String date = intent.getStringExtra("date"); 

    titleText.setText(title); 
    timeText.setText(time); 
    dateText.setText(date); 

    updateBtn.setOnClickListener(this); 
    deleteBtn.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.btn_update: 
      titleText.setText(titleText.getText().toString() + " "); 
      timeText.setText(dateText.getText().toString() + " "); 
      dateText.setText(timeText.getText().toString() + " "); 

      calendars.set_remindertitle(titleText.getText().toString() + " "); 

      calendars.set_reminderdate(dateText.getText().toString() + " "); 
      calendars.set_remindertime(timeText.getText().toString() + " "); 

      dbHelper.update(calendars); 

      this.returnHome(); 
      break; 

     case R.id.btn_delete: 
      dbHelper.delete(calendars); 
      this.returnHome(); 
      break; 
    } 
} 

public void returnHome() { 
    Intent home_intent = new Intent(getApplicationContext(), CountryListActivity.class) 
      .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    startActivity(home_intent); 
    } 
} 

數據庫不更新。它再次顯示以前的數據。這是數據庫類:

public class DatabaseHelper extends SQLiteOpenHelper { 
public static final int DATABASE_VERSION = 1; 
public static final String DATABASE_NAME = "calendar.db"; 
public static final String TABLE_REMINDER = "reminder"; 
public static final String COLUMN_ID = "_id"; 
public static final String COLUMN_REMINDER_TITLE = "_remindertitle"; 
public static final String COLUMN_REMINDER_DESCRIPTION = "_reminderdescription"; 
public static final String COLUMN_REMINDER_DATE = "_reminderdate"; 
public static final String COLUMN_REMINDER_TIME = "_remindertime"; 
public static final String COLUMN_REMINDER_REPEAT = "_reminderrepeat"; 
public static final String COLUMN_REMINDER_SNOOZE = "_remindersnooze"; 
SQLiteDatabase database; 
// Database Information 
Class<? extends DatabaseHelper> context = getClass(); 


DatabaseHelper dbHelper; 


// Creating table query 
public void onCreate(SQLiteDatabase db) { 
    String query = "CREATE TABLE " + TABLE_REMINDER + " (" + 
      COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + 
      COLUMN_REMINDER_DATE + " TEXT, " + COLUMN_REMINDER_TIME + " TEXT, " + COLUMN_REMINDER_TITLE + " TEXT, " 
      + COLUMN_REMINDER_DESCRIPTION + " TEXT, " + COLUMN_REMINDER_REPEAT + " TEXT, " + COLUMN_REMINDER_SNOOZE + " TEXT " + ");"; 
    Log.i("Query", query); 


    db.execSQL(query); 
} 

public DatabaseHelper(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 


@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_REMINDER); 
    onCreate(db); 
} 

public ArrayList<Calendars> databaseToArrayList() { 

    ArrayList<Calendars> arrayList = new ArrayList(); 

    SQLiteDatabase db = getWritableDatabase(); 
    String query = "SELECT * FROM " + TABLE_REMINDER; 
    Cursor c = db.rawQuery(query, null); 
    c.moveToFirst(); 
    while (!c.isAfterLast()) { 
     if (c.getString(c.getColumnIndex("_reminderdate")) != null) { 

      Calendars calendars = new Calendars(); 
      calendars.set_reminderdate(c.getString(c.getColumnIndex(COLUMN_REMINDER_DATE))); 
      calendars.set_remindertime(c.getString(c.getColumnIndex(COLUMN_REMINDER_TIME))); 
      calendars.set_remindertitle(c.getString(c.getColumnIndex(COLUMN_REMINDER_TITLE))); 
      calendars.set_reminderdescription(c.getString(c.getColumnIndex(COLUMN_REMINDER_DESCRIPTION))); 
      calendars.set_reminderrepeat(c.getString(c.getColumnIndex(COLUMN_REMINDER_REPEAT))); 
      calendars.set_remindersnooze(c.getString(c.getColumnIndex(COLUMN_REMINDER_SNOOZE))); 

      arrayList.add(calendars); 
     } 
     c.moveToNext(); 
    } 
    c.close(); 
    db.close(); 
    return arrayList; 

} 

public void remove(String id) { 
    String string = String.valueOf(id); 
    SQLiteDatabase database = getReadableDatabase(); 
    database.execSQL("DELETE FROM " + TABLE_REMINDER + " WHERE _id = '" + string + "'"); 
} 

public void addReminder(Calendars calendars) { 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(COLUMN_REMINDER_DATE, calendars.get_reminderdate()); 
    contentValues.put(COLUMN_REMINDER_TIME, calendars.get_remindertime()); 
    contentValues.put(COLUMN_REMINDER_TITLE, calendars.get_remindertitle()); 
    contentValues.put(COLUMN_REMINDER_DESCRIPTION, calendars.get_reminderdescription()); 
    contentValues.put(COLUMN_REMINDER_REPEAT, calendars.get_reminderrepeat()); 
    contentValues.put(COLUMN_REMINDER_SNOOZE, calendars.get_remindersnooze()); 
    SQLiteDatabase database = getReadableDatabase(); 
    database.insert(TABLE_REMINDER, null, contentValues); 
    Log.i("insData", "the data has been inseted"); 

    database.close(); 

} 


public Cursor fetch() { 
    String[] columns = new String[]{COLUMN_ID, /*COLUMN_REMINDER_DATE, COLUMN_REMINDER_TIME, COLUMN_REMINDER_TITLE,*/ 
      COLUMN_REMINDER_DESCRIPTION, COLUMN_REMINDER_REPEAT, COLUMN_REMINDER_SNOOZE}; 

    SQLiteDatabase database = getReadableDatabase(); 
    Cursor cursor = database.query(TABLE_REMINDER, columns, null, null, null, null, null); 
    if (cursor != null) 

    { 
     cursor.moveToFirst(); 
    } 

    return cursor; 
} 


public int update(Calendars calendars) { 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put(COLUMN_REMINDER_DATE, calendars.get_reminderdate()); 
    contentValues.put(COLUMN_REMINDER_TIME, calendars.get_remindertime()); 
    contentValues.put(COLUMN_REMINDER_TITLE, calendars.get_remindertitle());/* 
    contentValues.put(COLUMN_REMINDER_DESCRIPTION, calendars.get_reminderdescription()); 
    contentValues.put(COLUMN_REMINDER_REPEAT, calendars.get_reminderrepeat()); 
    contentValues.put(COLUMN_REMINDER_SNOOZE, calendars.get_remindersnooze());*/ 
    SQLiteDatabase database = getReadableDatabase(); 
    int i = database.update(TABLE_REMINDER, contentValues, COLUMN_ID + " = " + calendars.get_id(), null); 
    database.close(); 
    return i; 

} 


public void delete(Calendars calendars) { 
    database = getReadableDatabase(); 

    database.delete(TABLE_REMINDER, COLUMN_ID + "=" + calendars.get_id(), null); 
    } 
} 

我相信更新按鈕應該工作正常。我是Android新手,不知道如何解決這個問題。有關如何解決它的任何建議?

回答

0

如果你更新函數返回一個int,然後在您的onClick功能,而不是鍵入:

dbHelper.update(calendars); 

您需要輸入:

int update = dbHelper.update(calendars); 

或者:

if (dbHelper.update(calendars) > 0) { 
    // do something here 
} 

我會推薦後者的選項。看你如何去。