Android数据库信息显示在listview上

 

Key Points:

1.使用SimpleCursorAdapter将Android数据库信息显示在listview上

adapter = new SimpleCursorAdapter(this, R.layout.user_list_cell, null, new String[]{"name","sex"}, new int[]{R.id.tvName, R.id.tvSex});

2.长按listview弹出对话框删除信息。顺带学习了AlertDialog。

getListView().setOnItemLongClickListener(ListViewItemLongClicklistener);

    private OnItemLongClickListener ListViewItemLongClicklistener = new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
            final int position, long id) {
            
            new AlertDialog.Builder(HelloWorldActivity.this).setTitle("Title").setMessage("Are you sure to CANCEL THIS ITEM?").setNegativeButton("Cancel", null).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Cursor c = adapter.getCursor();
                    c.moveToPosition(position);
                    
                    int itemId = c.getInt(c.getColumnIndex("_id"));
                    dbWrite.delete("user", "_id=?", new String[]{itemId+""});
                    
//                    ContentValues cv = new ContentValues();
//                    cv.put("name", "ZP-BETTER");
//                    dbWrite.update("user", cv, "_id=?", new String[]{itemId+""});
                    refreshListView();
                }
            }).show();
            
            return true;
        }
    };

3.学习了Android数据库中的 ”增删改查“。

 

posted @ 2016-04-15 21:16  JoneZP  阅读(2870)  评论(0编辑  收藏  举报