gpt4 book ai didi

android - 从数据库中删除,不成功

转载 作者:行者123 更新时间:2023-11-29 02:05:52 24 4
gpt4 key购买 nike

我试图从数据库中删除一个条目,但没有成功。这是我的代码来展示我在做什么。我创建了一个 ContextMenu,它会打开一个名为“Delete”的新菜单,但是当我点击这个删除按钮时,它并没有删除所选项目。它只是什么都不做。

主要.java:

import java.util.List;
import java.util.Random;

import android.app.ListActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.view.MenuItem;


public class Announce extends ListActivity{
private CommentsDataSource datasource;
EditText edit;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.announce);

datasource = new CommentsDataSource(this);
datasource.open();

List<Comment> values = datasource.getAllComments();

ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
registerForContextMenu(getListView());
}


public void onClick(View view) {
@SuppressWarnings("unchecked")
ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) getListAdapter();
Comment comment = null;
switch (view.getId()) {
case R.id.add:
edit = (EditText)findViewById(R.id.editTxt);
Editable txt=(Editable)edit.getText();
String insert = txt.toString();
comment = datasource.createComment(insert);
adapter.add(comment);
edit.setText("");
break;
}
adapter.notifyDataSetChanged();
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}


@Override
public boolean onContextItemSelected(MenuItem item){
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
if(item.getTitle() == "Delete") {
datasource.deleteCommentById(info.id);
return true;
}
return super.onContextItemSelected(item);
}

@Override
protected void onResume() {
datasource.open();
super.onResume();
}

@Override
protected void onPause() {
datasource.close();
super.onPause();
}

}

这是我的 SQLHelper,我正在调用它进行删除。

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

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class CommentsDataSource {

// Database fields
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_COMMENT };

public CommentsDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}

public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}

public void close() {
dbHelper.close();
}

public Comment createComment(String comment) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_COMMENT, comment);
long insertId = database.insert(MySQLiteHelper.TABLE_COMMENTS, null,
values);
Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
Comment newComment = cursorToComment(cursor);
cursor.close();
return newComment;
}

public void deleteComment(Comment comment) {
long id = comment.getId();
System.out.println("Comment deleted with id: " + id);
database.delete(MySQLiteHelper.TABLE_COMMENTS, MySQLiteHelper.COLUMN_ID
+ " = " + id, null);
}

public void deleteCommentById(long id) {
System.out.println("Comment deleted with id: " + id);
database.delete(MySQLiteHelper.TABLE_COMMENTS, MySQLiteHelper.COLUMN_ID
+ " = " + id, null);
}

public List<Comment> getAllComments() {
List<Comment> comments = new ArrayList<Comment>();

Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
allColumns, null, null, null, null, null);

cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Comment comment = cursorToComment(cursor);
comments.add(comment);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return comments;
}

private Comment cursorToComment(Cursor cursor) {
Comment comment = new Comment();
comment.setId(cursor.getLong(0));
comment.setComment(cursor.getString(1));
return comment;
}
}

我做错了什么?它只是简单地不删除我选择的项目。

最佳答案

尝试改变

if(item.getTitle() == "Delete") {

if(item.getTitle().equals("Delete")) {

关于android - 从数据库中删除,不成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10094164/

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