在android中删除数据库中的项目

时间:2011-04-18 22:03:00

标签: android database

我有点问题。我正在尝试从列表中删除存储在数据库中的项目。当我选择“删除列表”时,所有其他项目都将被删除,但我选择的项目不会被删除。我不明白我的错误在哪里。请帮忙。

这是我的代码:

public class IncercListe extends Activity {

    TextView txt;
    String value;
    Button btn;
    private ListView lv1;
    private AlertDialog alertDialog;
    private ArrayAdapter<String> m_adapter;
    Bundle bundle ;
    String item;
    private Cursor cursor;
    private ListDbAdapter db;

    private static final int MENU_CREATE = Menu.FIRST;
    private static final int MENU_RENAME=Menu.FIRST+1;
    private static final int MENU_EDIT = Menu.FIRST + 2;
    private static final int MENU_SEND=Menu.FIRST+3;
    private static final int MENU_DELETE=Menu.FIRST+4;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list1);

        lv1=(ListView)findViewById(R.id.list);

        db=new ListDbAdapter(this);
        db.open();
        fillData();
        registerForContextMenu(lv1);

        lv1.setOnItemClickListener(new OnItemClickListener()
         {

           public void onItemClick(AdapterView<?> parent,View v,int position, long id)
           {
               Intent j= new Intent(IncercListe.this, List.class);
               startActivity(j);
           }
         });

     public void onCreateContextMenu(ContextMenu menu, View v,  ContextMenuInfo menuInfo) {  
             menu.setHeaderTitle("Choose ");  
             menu.add(0, MENU_RENAME, 0, "Rename List");  
             menu.add(0, MENU_EDIT, 0,  "Edit List");
             menu.add(0, MENU_SEND, 0,  "Send List");
             menu.add(0, MENU_DELETE, 0, "Delete List");
        }    
    @Override
    public boolean onContextItemSelected(MenuItem mitem) {     
        switch (mitem.getItemId()) {  
            case MENU_RENAME:  
                rename();
                return true;  

            case MENU_EDIT: 
                   Intent j= new Intent(IncercListe.this, List.class);
                   startActivity(j);                
                return true; 

            case MENU_SEND:
                return true;

            case MENU_DELETE:
                AdapterContextMenuInfo info=(AdapterContextMenuInfo)mitem.getMenuInfo();
                db.delete(info.id);
                fillData();
                    return true;
            default:    
                return super.onContextItemSelected(mitem);  
        }
    }   
    public boolean onCreateOptionsMenu(Menu menu) {    
        menu.add(0, MENU_CREATE, 0, "New List");//.setIcon(R.drawable.quit); 
        return true;
    }
    /* Handles item selections */
   public boolean onOptionsItemSelected (MenuItem item) {    
        switch (item.getItemId()) {    
            case MENU_CREATE:        
                createlist();
                return true;  
        }
        return false;
    }

    public void createlist() {    
        alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle("New List");
        alertDialog.setMessage("Enter name of new shopping list :");

        final EditText edt=new EditText(this);
        alertDialog.setView(edt);
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int which) {
                      item =edt.getText().toString();
                      if (!item.equals("")) db.create(item);
                      fillData();
                       return;
             }
         });
        alertDialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                   return;
            }
        }); 
        alertDialog.show();

    }
    public void rename() {      
    }
    public void sendlist(){
    }    
    protected void onActivityResult(int requestCode, int resultCode, Intent intent){
        super.onActivityResult(requestCode, resultCode, intent);
        fillData();
    }
    private void fillData(){
        cursor=db.fetchAll();
        startManagingCursor(cursor);

        ListAdapter adapter=new SimpleCursorAdapter(this,R.layout.textview,cursor,
                new String[] {ListDbAdapter.KEY_TITLE},new int[] {R.id.txt1});
        lv1.setAdapter(adapter);
    }

}

1 个答案:

答案 0 :(得分:0)

我不完全确定,但您可能希望调用db.delete(info.position)而不是db.delete(info.id)。