通过contextmenu列出具有更新和删除选项的视图

时间:2013-10-04 06:55:07

标签: android sqlite listview android-contextmenu

我很困惑,我想在listview之后刷新deleting any item。我只使用intentcall the same activity,但它不起作用。任何其他技巧或想法然后请建议。 my sample code如下:

update_page.java

 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);
                                /*task= ((TextView)record.findViewById(R.id.lbl_task)).getText().toString();
                                date_time1= ((TextView)record.findViewById(R.id.lbl_datetime)).getText().toString();
                                Toast.makeText(getApplicationContext(), "task is"+task, 1500).show();
                                database = act.getInstance().openOrCreateDatabase("contactsManager",
                                        SQLiteDatabase.CREATE_IF_NECESSARY, null);    

                                String query = "SELECT _id FROM contacts WHERE task_name='"+task + "&date_time="+date_time1+"'";


                                c=database.rawQuery(query, null);
                                 c.moveToNext();

                                new_id=c.getInt(c.getColumnIndex("_id"));
                                */

                                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);

                    op_intent.putExtra("task_id", new_id);
                    op_intent.putExtra("task", task);
                    op_intent.putExtra("datetime", date_time1);

                    update_page.this.startActivity(op_intent);*/

                    break;

                case 2:


                    Toast.makeText(getApplicationContext(), "position :" +new_id , 2000).show();

                    String table_name = "contacts";
                    String where = "task_name='"+task+ "&date_time="+date_time1+"'";
                    String[] whereArgs = null;
                    database.delete(table_name, where, whereArgs);  
                     //over

                        c.close();
                        database.close();
                        Toast.makeText(getApplicationContext(), "Task Deleted Successfully", 1500).show();

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

                    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);
            }


    }

2 个答案:

答案 0 :(得分:0)

您可以尝试使用onResume()方法并通知您列表。

@Override
public void onResume() {
    //set your adapter or referesh
    super.onResume();
}

答案 1 :(得分:0)

当您对列表视图中的项目完成删除操作时,您必须只调用适配器的adapter.notifydatasetchanged()