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数据库中的 ”增删改查“。

 

转载于:https://www.cnblogs.com/ZP-Better/p/5396964.html

你可能感兴趣的:(数据库,移动开发,ui)