Android ActionMode传递选中项目的_id

时间:2014-07-15 08:27:56

标签: android sqlite android-actionmode onitemlongclicklistener

我正在尝试从我的数据库中删除记录。我有这个Contact ListView列出了数据库中的记录。在itemLongClick上打开操作栏中的菜单,我可以在其中选择编辑或删除。当选择删除时,我打开一个对话框,询问他们是否确定要删除记录。如果是,那么我需要将所选项目的_id传递给DatabaseHandler,其中方法将删除RowID等于传递的_id的记录。

    public class ActivityListContacts extends Activity {

    // DatabaseHandler db = new DatabaseHandler(ActivityAddNewContact.this);
    DatabaseHandler db;
    final Context context = this;
    private ActionMode mActionMode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_contacts);
        populateContactList();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.contactlistmenu, menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Take appropriate action for each action item click
        switch (item.getItemId()) {
        case R.id.addcontact:
            Intent i = new Intent(this, ActivityAddNewContact.class);
            startActivity(i);            

            return true;

        default:
            return super.onOptionsItemSelected(item);
        }
    }


    private void populateContactList() {
        DatabaseHandler db = new DatabaseHandler(this);
        TextView tvFullName = (TextView) findViewById(R.id.tvContactFullNameC);
        // TextView tvCompany = (TextView)
        // findViewById(R.id.tvContactListCompany);
        Cursor cursor = db.fetchContacts();

        // Allow activity to manage lifetime of the cursor.
        // DEPRECATED! Runs on the UI thread, OK for small/short queries.
        startManagingCursor(cursor);

        // Setup mapping from cursor to view fields:
        String[] fromFieldNames = new String[] { db.KEY_NAMECONCAT, db.KEY_COMPANY };
        int[] toViewIDs = new int[] { R.id.tvContactFullNameC, R.id.tvContactListCompany };

        SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this, // Context
                R.layout.item_contactlist, // Row layout template
                cursor, // cursor (set of DB records to map)
                fromFieldNames, // DB Column names
                toViewIDs // View IDs to put information in
        );

        // Set the adapter for the list view
        ListView contactList = (ListView) findViewById(R.id.lvContacts);
        contactList.setAdapter(myCursorAdapter);
        db.close();

        contactList
                .setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                    public boolean onItemLongClick(AdapterView parent,
                            View view, int position, long id) {

                        System.out.println("Long click");
                        startActionMode(modeCallBack);
                        view.setSelected(true);
                        Log.w("CONTACT", "Long click item  " + id);
                        startActionMode.setTag(id);
                        return true;
                    }
                });
    }

    private ActionMode.Callback modeCallBack = new ActionMode.Callback() {

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            mode.setTitle("Options");
            mode.getMenuInflater().inflate(R.menu.contactlistdialog, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            int contactid = item.getItemId();

            Log.w("CONTACT", "Contact selected is ID# " + contactid);
            switch (contactid) {
            case R.id.delete: {
                // Delete the Contact Record
                // First ask if you are sure you want to delete, If yes then
                // delete, if No then dismiss
                Log.w("CONTACT", "Switch Contact ID  " + contactid);
                AlertDialog.Builder alert = new AlertDialog.Builder(ActivityListContacts.this);

                // set title
                alert.setTitle(R.string.contact_alert_title);
                // set dialog message
                alert.setMessage(R.string.contact_alert_message);
                alert.setCancelable(false);
                alert.setPositiveButton(R.string.contact_alert_yes,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int contactid) {
                                // If YES then delete the contact from database
                                Log.w("CONTACT", "Contact passed to DB is ID#  " + contactid);
                                db.deleteContact(contactid);

                                dialog.cancel();
                            }
                        }).setNegativeButton(R.string.contact_alert_no,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int contactid) {
                                // if this button is clicked, just close
                                // the dialog box and do nothing
                                dialog.cancel();
                            }
                        });

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

                // show it
                alertDialog.show();

                mode.finish();
                break;
            }
            case R.id.edit: {
                // Edit the Contact Record

                break;
            }
            default:
                return false;
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }
    };

    /**
     * private void registerListClickCallback() { ListView contactList =
     * (ListView) findViewById(R.id.lvContacts);
     * contactList.setOnItemClickListener(new AdapterView.OnItemClickListener()
     * {
     * 
     * @Override public void onItemClick(AdapterView<?> parent, View view, int
     *           position, long id) {
     * 
     *           updateItemForId(id); } }); } private void updateItemForId(long
     *           idInDB) { Cursor cursor = db.getRow(idInDB); if
     *           (cursor.moveToFirst()) { long idDB =
     *           cursor.getLong(db.COL_ROWID); String name =
     *           cursor.getString(db.COL_NAME); int studentNum =
     *           cursor.getInt(db.COL_STUDENTNUM); String favColour =
     *           cursor.getString(db.COL_FAVCOLOUR);
     * 
     *           favColour += "!"; db.updateRow(idInDB, name, studentNum,
     *           favColour); } cursor.close(); populateContactList(); }
     * 
     * 
     * 
     * 
     * 
     * 
     * 
     *           }
     **/

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.finish();
    }

}

以上是目前的全班。 OnItemLongClick位于populateContactList()中。在那里,我从适配器获得“长ID”。其中的日志消息显示该项目的正确RowID。现在我们知道id是正确的项目,我需要将该id一直传递给DialogBox Positive按钮,该按钮执行数据库处理程序中的删除记录。

0 个答案:

没有答案