1

我想从 sqlite 数据库中删除数据。我已经将记录或数据从 sqlitedab 显示到列表视图。现在,当用户单击列表视图中显示的特定记录时,我正试图删除记录。当用户单击特定记录时,我使用上下文菜单进行更新和删除选项。我的问题是我没有在该上下文菜单中成功应用删除选项并且无法删除该特定记录!

我的代码如下:

 public class update_page extends Activity 
{

    DatabaseHandler db = new DatabaseHandler(this);
    public String tableName = db.TABLE_CONTACTS;
    public String databaseName = db.DATABASE_NAME;
    public String taskID = db.KEY_ID;

    public String task_name = db.KEY_TASK;
    public String dt = db.KEY_DATETIME;
    private ArrayList<String> results = new ArrayList<String>();
    private ArrayList<String> results2 = new ArrayList<String>();

    Intent op_intent;
    SQLiteDatabase database;
    ListView myview;
    Cursor c;
    String o_name,o_no1;
    //String o_id1 = null;
     HttpClient client;
    Integer op_id,status;
    String url;
    Context ctx;
    Intent myintent;

    final MainActivity act = new MainActivity();
    protected Object adapter;

     @Override
    public void onCreate(Bundle savedInstanceState)
        {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.update_list);
            final ProgressDialog myPd_ring=ProgressDialog.show(update_page.this, "Please wait", "Login please wait..", true);
            myPd_ring.setCancelable(true);
            myPd_ring.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            new Thread() {

                public void run() {

                try{

                sleep(1000);

                } catch (Exception e) {

                Log.e("tag", e.getMessage());

                }

                // dismiss the progress dialog

                myPd_ring.dismiss();

                }

                }.start();


            myview = (ListView)this.findViewById(R.id.list);
            ctx=this;





            database = act.getInstance().openOrCreateDatabase("contactsManager",
            SQLiteDatabase.CREATE_IF_NECESSARY, null);       
            Toast.makeText(getApplicationContext(), "Database is open", 1500).show();


       //  database=this.openOrCreateDatabase("contactsManager", SQLiteDatabase.CREATE_IF_NECESSARY,null);

            final String[] columnsone={"_id","task_name","date_time"};
            String[] columnstwo={"task_name","date_time"};
            int to[] = {R.id.lbl_task,R.id.lbl_datetime}; 
            c = database.query("contacts", columnsone, null, null, null, null, null);
            status=c.getCount();

            Toast.makeText(getApplicationContext(), "totle task :" +status, 1500).show();



             if(c!=null)
               {

                Toast.makeText(getApplicationContext(), "in courser", 1500).show();
                SimpleCursorAdapter adapter=new 
                        SimpleCursorAdapter(update_page.this,R.layout.update_listview, c, columnstwo, to);
                Toast.makeText(getApplicationContext(), "records are in list", 1500).show();

                myview.setAdapter(adapter);


                        myview.setOnItemLongClickListener(new OnItemLongClickListener() 
                        {

                            public boolean onItemLongClick(AdapterView<?> arg0,
                                    View record, int arg2, long arg3)
                            {
                                update_page.this.registerForContextMenu(record);

                                return false;
                            }

                        });
                }
             }
            //@Override 
            public boolean onContextItemSelected(MenuItem item)
            {
                AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo)item.getMenuInfo();
                int choice = item.getItemId();
                switch(choice)
                {
                case 1:
                    Toast.makeText(getApplicationContext(), "Update Task", 2000).show();

                    Intent op_intent = new Intent(update_page.this,MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    update_page.this.startActivity(op_intent);

                    break;

                case 2:



                    database = act.getInstance().openOrCreateDatabase("contactsManager",
                    SQLiteDatabase.CREATE_IF_NECESSARY, null);       
                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctx);
                    // set title
                    alertDialogBuilder.setTitle("are you sure to delete this record  !!");
                    // set dialog message
                    alertDialogBuilder
                            .setMessage("Click cancle to exit!")
                            .setCancelable(false)
                            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                                 public void onClick(DialogInterface dialog, int whichButton) {
                                                 DatabaseHandler db=new DatabaseHandler(getApplicationContext());

                                                 db.deleteContact(arg3+"");
                                                 list.remove(position);
                                                 update_page.this.adapter.notifyDataSetChanged();
                                                }
                                              });

                        /*  .setNegativeButton("Cancle",
                                    new DialogInterface.OnClickListener() 
                                    {
                                        public void onClick(DialogInterface dialog,
                                                int id) {
                                            // if this button is clicked, just close
                                            // the dialog box and do nothing
                                            dialog.cancel();
                                            myintent = new Intent(update_page.this,MainActivity.class);
                                            startActivity(myintent);
                                        }
                                    });
                            */

                    // create alert dialog
                    AlertDialog alertDialog = alertDialogBuilder.create();

                    // show it
                    alertDialog.show();





                    break;




                }

                return super.onContextItemSelected(item);
            }


            @Override
            public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenuInfo menuInfo)
            {
                menu.add(0,1,0,"Update Task");
                menu.add(0,2,0,"Delete");

                super.onCreateContextMenu(menu, v, menuInfo);
            }


    }
4

0 回答 0