我在添加+从数据库删除书签时遇到问题。我可以在点击收藏夹图标时添加书签并删除书签。主要问题是,当我添加书签并点击同一图标以取消书签时它没有相同的页面。
SchoolDetails课程
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseUpdatedListener databaseUpdatedListener;
static final String DATABASE_NAME = "BookmarkDatabase";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME_BOOKMARK = "bookmark";
// Contact table columns name
private static final String ID = "Id";
private static final String NAME = "Name";
private static final String ADDRESS = "Address";
private static final String LOGO = "Logo";
private static final String COUNTRY = "country";
private static final String PHONE = "phone";
private static final String EMAIL = "email";
private static final String WEBSITE = "website";
private static final String INSTITUTION_TYPE = "institution_type";
private static final String ESTABLISHED_DATE = "establishment_date";
private static final String ADMISSION_OPEN_FROM = "admission_open_from";
private static final String ADMISSION_OPEN_TO = "admission_open_to";
private static final String LATITUDE = "latitude";
private static final String LONGITUDE = "longitude";
String createTableBookmark = "Create table if not exists `Bookmark` (" + "`name` TEXT," + "`location` TEXT," + "`logo` TEXT);";
String CREATE_SCHOOL_BOOKMARK_TABLE = "CREATE TABLE " + TABLE_NAME_BOOKMARK + "("
+ ID + " INTEGER PRIMARY KEY, "
+ NAME + " TEXT, "
+ LOGO + " TEXT, "
+ ADDRESS + " TEXT, "
+ COUNTRY + " TEXT, "
+ PHONE + " TEXT, "
+ EMAIL + " TEXT, "
+ WEBSITE + " TEXT, "
+ INSTITUTION_TYPE + " TEXT, "
+ ESTABLISHED_DATE + " TEXT, "
+ ADMISSION_OPEN_FROM + " TEXT, "
+ ADMISSION_OPEN_TO + " TEXT, "
+ LATITUDE + " TEXT, "
+ LONGITUDE + " TEXT " + ")";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void insertBookmarkData(Bookmarkitem bookmarkitem) {
SQLiteDatabase db = getWritableDatabase();
ContentValues content = new ContentValues();
content.put("name", bookmarkitem.name);
content.put("location", bookmarkitem.address);
content.put("logo", bookmarkitem.logo);
db.insert("Bookmark", null, content);
}
public ArrayList<Bookmarkitem> getBookmarkist() {
String sql = "select * from Bookmark ";
ArrayList<Bookmarkitem> bookmarklist = new ArrayList<Bookmarkitem>();
Cursor c = getWritableDatabase().rawQuery(sql, null);
while (c.moveToNext()) {
Bookmarkitem info = new Bookmarkitem();
info.name = c.getString(c.getColumnIndex("name"));
info.address = c.getString(c.getColumnIndex("location"));
info.logo = c.getString(c.getColumnIndex("logo"));
bookmarklist.add(info);
}
c.close();
return bookmarklist;
}
public Bookmarkitem getBookmarkData(String bookmarkName) {
String sql = "select * from Bookmark where id='" + bookmarkName + "'";
Cursor c = getWritableDatabase().rawQuery(sql, null);
while (c.moveToNext()) {
Bookmarkitem info = new Bookmarkitem();
info.name = c.getString(c.getColumnIndex("name"));
info.address = c.getString(c.getColumnIndex("location"));
info.logo = c.getString(c.getColumnIndex("logo"));
}
c.close();
Bookmarkitem info = null;
return info;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
// TODO Auto-generated method stub
sqLiteDatabase.execSQL(CREATE_SCHOOL_BOOKMARK_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int arg1, int arg2) {
// TODO Auto-generated method stub
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_BOOKMARK);
onCreate(sqLiteDatabase);
}
public void addSchoolBookmark(Bookmarkitem bookmarkitem, MenuItem menuItem) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ID, bookmarkitem.getBookmarkID());
values.put(NAME, bookmarkitem.getName());
values.put(LOGO, bookmarkitem.getLogo());
values.put(ADDRESS, bookmarkitem.getAddress());
values.put(COUNTRY, bookmarkitem.getCountry());
values.put(PHONE, bookmarkitem.getPhone());
values.put(EMAIL, bookmarkitem.getEmail());
values.put(WEBSITE, bookmarkitem.getWebsite());
values.put(INSTITUTION_TYPE, bookmarkitem.getInstitution_type());
values.put(ESTABLISHED_DATE, bookmarkitem.getEstablishment_date());
values.put(ADMISSION_OPEN_FROM, bookmarkitem.getAdmission_open_from());
values.put(ADMISSION_OPEN_TO, bookmarkitem.getAdmission_open_to());
values.put(LATITUDE, bookmarkitem.getLatitude());
values.put(LONGITUDE, bookmarkitem.getLongitude());
//inserting row
if (db.insert(TABLE_NAME_BOOKMARK, null, values) != -1) {
databaseUpdatedListener.setDatabaseSuccess(bookmarkitem.getName(), menuItem);
} else {
databaseUpdatedListener.setDatabaseError("Failed to Delete");
}
db.close();
}
public List<Bookmarkitem> getAllSchoolBookmark() {
SQLiteDatabase db = this.getWritableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_NAME_BOOKMARK;
List<Bookmarkitem> bookmarkitems = new ArrayList<>();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Bookmarkitem bookmarkitem = new Bookmarkitem();
bookmarkitem.setBookmarkID(Integer.parseInt(cursor.getString(0)));
bookmarkitem.setName(cursor.getString(1));
bookmarkitem.setLogo(cursor.getString(2));
bookmarkitem.setAddress(cursor.getString(3));
bookmarkitem.setCountry(cursor.getString(4));
bookmarkitem.setPhone(cursor.getString(5));
bookmarkitem.setEmail(cursor.getString(6));
bookmarkitem.setWebsite(cursor.getString(7));
bookmarkitem.setInstitution_type(cursor.getString(8));
bookmarkitem.setEstablishment_date(cursor.getString(9));
bookmarkitem.setAdmission_open_from(cursor.getString(10));
bookmarkitem.setAdmission_open_to(cursor.getString(11));
bookmarkitem.setLatitude(cursor.getString(12));
bookmarkitem.setLongitude(cursor.getString(13));
bookmarkitems.add(bookmarkitem);
} while (cursor.moveToNext());
}
return bookmarkitems;
}
public boolean removeBookmarkItem(String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME_BOOKMARK, ID + "=" + id, null) > 0;
}
public Bookmarkitem getBoomarkDetailByID(String id) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = null;
String empName = "";
try {
Bookmarkitem bookmarkitem = new Bookmarkitem();
cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME_BOOKMARK + " WHERE " + ID + "=?", new String[]{id + ""});
if (cursor.getCount() > 0) {
cursor.moveToFirst();
bookmarkitem.setBookmarkID(Integer.parseInt(cursor.getString(0)));
bookmarkitem.setName(cursor.getString(1));
bookmarkitem.setLogo(cursor.getString(2));
bookmarkitem.setAddress(cursor.getString(3));
bookmarkitem.setCountry(cursor.getString(4));
bookmarkitem.setPhone(cursor.getString(5));
bookmarkitem.setEmail(cursor.getString(6));
bookmarkitem.setWebsite(cursor.getString(7));
bookmarkitem.setInstitution_type(cursor.getString(8));
bookmarkitem.setEstablishment_date(cursor.getString(9));
bookmarkitem.setAdmission_open_from(cursor.getString(10));
bookmarkitem.setAdmission_open_to(cursor.getString(11));
bookmarkitem.setLatitude(cursor.getString(12));
bookmarkitem.setLongitude(cursor.getString(13));
}
return bookmarkitem;
} finally {
cursor.close();
}
}
}
DatabaseHelper类
public interface DatabaseUpdatedListener {
void setDatabaseSuccess(String schoolName, MenuItem item);
void setDatabaseError(String failureMessage);
}
我的界面
public class Bookmarkitem implements Serializable{
public int bookmarkID; //school or college or university id
public String name;
public String address;
public String logo;
public String country;
public String phone;
public String email;
public String website;
public String institution_type;
public String establishment_date;
public String admission_open_from;
public String admission_open_to;
public String latitude;
public String longitude;
public int getBookmarkID() {
return bookmarkID;
}
public void setBookmarkID(int bookmarkID) {
this.bookmarkID = bookmarkID;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getLogo() {
return logo;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(String address) {
this.address = address;
}
public void setLogo(String logo) {
this.logo = logo;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getWebsite() {
return website;
}
public void setWebsite(String website) {
this.website = website;
}
public String getInstitution_type() {
return institution_type;
}
public void setInstitution_type(String institution_type) {
this.institution_type = institution_type;
}
public String getEstablishment_date() {
return establishment_date;
}
public void setEstablishment_date(String establishment_date) {
this.establishment_date = establishment_date;
}
public String getAdmission_open_from() {
return admission_open_from;
}
public void setAdmission_open_from(String admission_open_from) {
this.admission_open_from = admission_open_from;
}
public String getAdmission_open_to() {
return admission_open_to;
}
public void setAdmission_open_to(String admission_open_to) {
this.admission_open_to = admission_open_to;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
}
Bookmarkitem class
E/SQLiteDatabase: Error inserting admission_open_from=27-12-2016 longitude=85.2865 website=www.soanitech.com phone=11144441 email=merameah@gmail.com establishment_date=27-12-2016 Id=111 institution_type=Public Address=Koteshwor country=Nepal admission_open_to=27-12-2016 Logo=http://www.myeducationhunt.com/pjQXrVjBPggfOilRxpze.jpeg latitude=27.6794 Name=Sir Ramesh Bista
android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: bookmark.Id (code 1555)
at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1474)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
at com.example.user.educationhunt.database.DatabaseHelper.addSchoolBookmark(DatabaseHelper.java:143)
at com.example.user.educationhunt.SchoolDetails.onOptionsItemSelected(SchoolDetails.java:102)
当我第一次点击添加/删除时没关系,第二次点击以下错误
Intent i = new Intent(Intent.ACTION_GET_CONTENT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECT_PICTURE);
case SELECT_PICTURE: {
if (resultCode == RESULT_OK && null != data) {
mHandler.post(new Runnable() {
@Override
public void run() {
Uri selectedImageUri = data.getData();
InputStream imageStream;
Bitmap selectedImage;
try {
cropCapturedImage(getImageUrlWithAuthority(getApplicationContext(),selectedImageUri));
} catch (ActivityNotFoundException aNFE) {
//display an error message if user device doesn't support
showToast(getString(R.string.error_crop_not_supported));
try {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(getImageUrlWithAuthority(getApplicationContext(),selectedImageUri), filePathColumn, null, null, null);
cursor.moveToFirst();
imageStream = getContentResolver().openInputStream(getImageUrlWithAuthority(getApplicationContext(),selectedImageUri));
selectedImage = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
//SharePreference to store image
PrefManager.putString(Constant.IMAGE_DATA, encodedImage);
cursor.close();
//set gallery image
setChatBackground();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
});
}
如何解决这个问题?
答案 0 :(得分:0)
看来你的bookmark.id并不是唯一的。确保您没有添加相同的ID两次或使用其他内容作为唯一ID。