具体代码如下:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); show = (ListView)findViewById(R.id.show); db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/music.db3", null); //显示列表 showlist(); // 添加长按点击弹出选择菜单 show.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.setHeaderTitle("选择操作"); menu.add(0, 0, 0, "更新该条"); menu.add(0, 1, 0, "删除该条"); } }); } //给菜单项添加事件 @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); //info.id得到listview中选择的条目绑定的id String id = String.valueOf(info.id); switch (item.getItemId()) { case 0: updateDialog(id); //更新事件的方法 return true; case 1: //System.out.println("删除"+info.id); deleteData(db,id); //删除事件的方法 showlist(); return true; default: return super.onContextItemSelected(item); } }
//显示列表项 private void showlist(){ Cursor cursor = null; try{ cursor = db.rawQuery("select * from musiclist", null); inflateList(cursor); }catch(SQLiteException se){ db.execSQL("create table musiclist(_id integer primary key autoincrement," +"music varchar(30) ," +"singer varchar(30))"); //查询 cursor = db.rawQuery("select * from musiclist", null); inflateList(cursor); }finally{ //cursor.close(); } }
Android学习----------长按列表项弹出菜单,给菜单项添加事件,获取上下文,布布扣,bubuko.com
Android学习----------长按列表项弹出菜单,给菜单项添加事件,获取上下文
原文:http://blog.csdn.net/frightingforambition/article/details/26168551