gpt4 book ai didi

android - 数据库删除功能不起作用

转载 作者:搜寻专家 更新时间:2023-10-30 20:35:30 25 4
gpt4 key购买 nike

大家好,我在从数据库中删除项目时遇到了一些问题。我在一项 Activity 中有一个学校列表。这是我的屏幕截图[![Schools.java][1]][1]

在 schooldetails.class 的顶部,我有一个星形图标来为学校添加书签。单击书签图标后,图标变为实心图标,相应的学校将保存到我的书签类中。直到一切正常。现在我正在尝试通过重新单击填充的星形图标来删除书签。我希望从书签类中删除带有书签的学校。这就是我在 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;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.user.educationhunt.adapter.BookmarkAdapter;
import com.example.user.educationhunt.database.DatabaseHelper;
import com.example.user.educationhunt.pojos.Bookmarkitem;

import java.util.ArrayList;
import java.util.List;

public class Bookmark extends AppCompatActivity {
private List<Bookmarkitem> ourBookmarkListItems = new ArrayList<Bookmarkitem>();
private ListView listView;
private BookmarkAdapter adapter;
DatabaseHelper dbhelper;
private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bookmark);
dbhelper = new DatabaseHelper(this);

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

toolbar.setPadding(0, getStatusBarHeight(), 0, 0);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Bookmark");

List<Bookmarkitem> bookmarkedSchools = dbhelper.getAllSchoolBookmark();

listView = (ListView) findViewById(R.id.list_bookmarked);
if (bookmarkedSchools.size() != 0) {
adapter = new BookmarkAdapter(this, bookmarkedSchools);
listView.setAdapter(adapter);
} else {
Toast.makeText(this, "You have no bookmark yet.", Toast.LENGTH_SHORT).show();
}

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

startActivity(new Intent(Bookmark.this,SchoolDetails.class));
}
});

}

public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

我既没有得到任何错误也没有得到正确的结果。请有人帮忙

最佳答案

在你的助手类中使用它 :D

public boolean removeBookmarkItem(String id) {
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME_BOOKMARK, ID + "=" + id, null) > 0;
}

关于android - 数据库删除功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41540854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com