我正在开展一个包含不同学校和学院名单的项目。点击每个列表项时,它会导航到显示其详细信息的下一个活动。我在appbar中添加了一个小书签图标,点击书签图标后,特定的学校/学院将被添加到书签列表中,书签列表将会为了这个我有一个单独的书签类,我正在使用sqlite数据库。现在我想删除书签项目并从数据库中删除该项目。当我重新点击书签图标,然后我希望图标更改,相应的书签项目将被删除。这是我的Schooldetails类,书签图标位于:
package com.example.user.educationhunt;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.example.user.educationhunt.database.DatabaseHelper;
import com.example.user.educationhunt.fragment.About;
import com.example.user.educationhunt.fragment.Admission;
import com.example.user.educationhunt.fragment.FeeStructure;
import com.example.user.educationhunt.listner.DatabaseUpdatedListener;
import com.example.user.educationhunt.pojos.Bookmarkitem;
import com.example.user.educationhunt.pojos.OurSchool;
import java.util.ArrayList;
import java.util.List;
public class SchoolDetails extends AppCompatActivity implements DatabaseUpdatedListener {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
Boolean isStarFilled = false;
DatabaseHelper db;
OurSchool ourSchool;
private Menu menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school_details);
ourSchool = (OurSchool) getIntent().getSerializableExtra("school");
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(ourSchool.getSchoolName());
db = new DatabaseHelper(this);
db.databaseUpdatedListener = this;
Toast.makeText(this, ourSchool.getSchoolName(), Toast.LENGTH_SHORT).show();
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.fav_school, menu);
this.menu = menu;
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.star_School:
Bookmarkitem bookmarkitem = new Bookmarkitem();
bookmarkitem.setBookmarkID(ourSchool.getSchoolId());
bookmarkitem.setName(ourSchool.getSchoolName());
bookmarkitem.setLogo(ourSchool.getSchoolLogo());
bookmarkitem.setAddress(ourSchool.getSchoolAddress());
db.addSchoolBookmark(bookmarkitem, item);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
List<Bookmarkitem> bookmarkitems = db.getAllSchoolBookmark();
if (bookmarkitems.size() != 0) {
for (Bookmarkitem bookmarkitem : bookmarkitems) {
if (bookmarkitem.getBookmarkID() == ourSchool.getSchoolId()) {
isStarFilled = true;
break;
}
else isStarFilled=false;
}
if (isStarFilled) {
menu.getItem(0).setIcon(getResources().getDrawable(R.mipmap.starfilled));
}else if (isStarFilled.booleanValue()==true){
delete();
menu.getItem(0).setIcon(getResources().getDrawable(R.mipmap.star));
}
}
return true;
}
public void delete(){
List<Bookmarkitem> bookmarkitems = db.getAllSchoolBookmark();
db.removeBookmarkItem(ourSchool.getSchoolId()); }
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new About(), "ABOUT US");
adapter.addFragment(new Admission(), "ADMISSION");
adapter.addFragment(new FeeStructure(), "FEE");
viewPager.setAdapter(adapter);
}
@Override
public void setDatabaseSuccess(String schoolName, MenuItem item) {
Toast.makeText(this, schoolName + "successfully added as bookmark", Toast.LENGTH_SHORT).show();
item.setIcon(R.mipmap.starfilled);
}
@Override
public void setDatabaseError(String failureMessage) {
Toast.makeText(this, failureMessage, Toast.LENGTH_SHORT).show();
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
这是我的数据库类
package com.example.user.educationhunt.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.MenuItem;
import com.example.user.educationhunt.listner.DatabaseUpdatedListener;
import com.example.user.educationhunt.pojos.Bookmarkitem;
import java.util.ArrayList;
import java.util.List;
/**
* Created by user on 11/22/2016.
*/
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 LOGO = "Logo";
private static final String LOCATION = "Location";
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, "
+ LOCATION + " 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(LOCATION, bookmarkitem.getAddress());
//inserting row
if (db.insert(TABLE_NAME_BOOKMARK, null, values) != -1) {
databaseUpdatedListener.setDatabaseSuccess(bookmarkitem.getName(), menuItem);
} else {
databaseUpdatedListener.setDatabaseError("Failed to insert");
}
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));
bookmarkitems.add(bookmarkitem);
} while (cursor.moveToNext());
}
return bookmarkitems;
}
public void removeBookmarkItem(int sID) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_NAME_BOOKMARK + " WHERE " + ID + "= '" + sID + "'");
db.close();
}
}
这是数据库监听器
package com.example.user.educationhunt.listner;
import android.view.MenuItem;
/**
* Created by Xerric on 11/30/2016.
*/
public interface DatabaseUpdatedListener {
void setDatabaseSuccess(String schoolName, MenuItem item);
void setDatabaseError(String failureMessage);
}
我尝试了我能做的事情。我既没有得到任何错误也没有得到理想的结果。请指导。