2012-04-10 111 views
2

我想從數據庫中刪除一個條目,但不成功。這是我的代碼來展示我在做什麼。我創建了一個ContextMenu,它打開了一個名爲「刪除」的新菜單,但是當我單擊此刪除時,它不會刪除所選項目。它只是沒有做任何事情。從數據庫中刪除,不成功

main.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; 
    } 
} 

我在做什麼錯?它只是不刪除我選擇的項目。

+0

如果我的回答沒有幫助,您是否嘗試設置斷點並調試此代碼?如果是這樣,它有多遠?它是否在Annouce活動中的onContextItemSelected上發生?它是否達到deleteCommentById方法? – 2012-04-10 18:26:12

回答

0

我想你應該把這個

adapter.notifyDataSetChanged(); 
這部分代碼後

..

datasource.deleteCommentById(info.id); 
+0

可能是的,但是當我輸入它顯示爲錯誤,我嘗試:info.no​​tifyDataSetChanged();它顯示爲固定添加到信息,但這不是幫助。 – HyperX 2012-04-11 15:47:22

1

嘗試改變

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

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

試過什麼都沒有發生。看起來,它甚至不認識到它被點擊了.. – HyperX 2012-04-10 19:07:17

+0

你是說onContextItemSelected方法永遠不會被調用嗎? – 2012-04-10 19:19:18

+0

不知道它是否被調用,因爲我不是很熟悉Eclipse中的這個調試器,但是我嘗試了一下chained methoid,但沒有任何反應。 – HyperX 2012-04-11 15:41:24