我需要添加一种方法来编辑,删除或启动一个活动,该活动根据列表视图中的选择显示数据库记录

时间:2012-12-28 18:22:42

标签: android contextmenu

我正在编写一个显示属性列表的应用程序。目前,当您单击地址时,它会启动编辑活动,当您长按地址时,它会显示一个上下文菜单,其中包含删除选项。

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Intent i = new Intent(this, ProjectEditActivity.class);
    i.putExtra(ProjectDbAdapter.ROW_ID, id);
    startActivityForResult(i, ACTIVITY_EDIT);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.menu_project_delete:
            AdapterContextMenuInfo info =
                    (AdapterContextMenuInfo) item.getMenuInfo();
            mDbHelper.deleteProject(info.id);
            fillData();
            return true;
    }
    return super.onContextItemSelected(item);
}

我想修改它,以便点击地址显示要查看的记录,上下文菜单提供删除和编辑记录的选项。

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    Intent i = new Intent(this, ProjectShowActivity.class);
    i.putExtra(ProjectDbAdapter.ROW_ID, id);
    startActivityForResult(i, ACTIVITY_EDIT);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.menu_project_delete:
            AdapterContextMenuInfo deleteInfo =
                    (AdapterContextMenuInfo) item.getMenuInfo();
            mDbHelper.deleteProject(deleteInfo.id);
            fillData();
            return true;
        case R.id.menu_project_edit:
            AdapterContextMenuInfo editInfo =
                    (AdapterContextMenuInfo) item.getMenuInfo();
            Intent i = new Intent(this, ProjectEditActivity.class);
            i.putExtra(ProjectDbAdapter.ROW_ID, id)
            startActivityForResult(i, ACTIVITY_EDIT);
            fillData();
            return true;
    }
    return super.onContextItemSelected(item);
}

不幸的是,我不知道在调用i.putExtra时如何确定id的值。任何建议将不胜感激

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;


public class ProjectDbAdapter extends DbAdapter {

    private static final String DATABASE_TABLE = "Project";
    public static final String ROW_ID = "_id";
    public static final String STREET = "Street";
    public static final String CITY = "City";
    public static final String STATE = "State";
    public  static final String ZIP = "Zip";
    public static final String OTHER = "Other";

    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     *
     * @param ctx the Context within which to work
     */
    public ProjectDbAdapter(Context ctx) {
        super(ctx);
    }

    /**
     * Create a new project. If the project is successfully created return the
     * new rowId for the project, otherwise return -1 to indicate failure
     *
     * @param street
     * @param city
     * @param state
     * @param zip
     * @param other
     * @return rowId or -1 if failed
     */
    public long createProject(String street, String city, String state, String zip,         String other) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(STREET, street);
        initialValues.put(CITY, city);
        initialValues.put(STATE, state);
        initialValues.put(ZIP, zip);
        initialValues.put(OTHER, other);
        return this.mDb.insert(DATABASE_TABLE, null, initialValues);
    }

    /**
         * Delete the project with the given rowId
     *
     * @param rowId
     * @return true if deleted or false if not
     * @param rowId
     * @return
     */
    public boolean deleteProject(long rowId) {
        return this.mDb.delete(DATABASE_TABLE, ROW_ID + "=" + rowId, null) > 0;
    }

    /**
                     * Return a Cursor over the list of all projects in the database
     *
     * @return Cursor over all projects
     */
    public Cursor getAllProjects() {
        return this.mDb.query(DATABASE_TABLE,
                new String[]{ROW_ID, STREET, CITY, STATE, ZIP, OTHER},
                null, null, null, null, null);
    }

    /**
     * Return a Cursor positioned at project that matches the given rowId
     *
     * @param rowId
             * @return Cursor positioned to matching project, if found
     * @throws SQLException if project could not be found/retrieved
     */
    public Cursor getProject(long rowId) throws SQLException {
        Cursor mCursor = this.mDb.query(DATABASE_TABLE, new String[]{ROW_ID, STREET,     CITY, STATE, ZIP, OTHER},
                ROW_ID + "=" + rowId, null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    /**
     * Update the project
     *
     * @param rowId
     * @param street
     * @param city
     * @param state
     * @param zip
     * @param other
     * @return true if the record was updated or false if not
         */
    public boolean updateProject(long rowId, String street, String city, String state,     String zip, String other) {
        ContentValues args = new ContentValues();
        args.put(STREET, street);
        args.put(CITY, city);
        args.put(STATE, state);
        args.put(ZIP, zip);
        args.put(OTHER, other);
        return this.mDb.update(DATABASE_TABLE, args, ROW_ID + "=" + rowId, null) > 0;
    }
}



    import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;

/**
 *
 * @author TJDroid
 */
public class DbAdapter {

    protected static final String DBADAPTER_TAG = "DbAdapter";
    protected static final String DATABASE_NAME = "TJBuilder";
    protected static final int DATABASE_VERSION = 1; 

    protected static final String CREATE_TABLE_PROJECT = 
            "create table Project (_id integer primary key autoincrement, "
            + "Street TEXT, "
            + "City TEXT, "
            + "State TEXT, "
            + "Zip TEXT, "
            + "Other TEXT);";

    protected final Context mCtx;
    protected DatabaseHelper mDbHelper;
    protected SQLiteDatabase mDb;

             protected static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
            public void onCreate(SQLiteDatabase db) {

            db.execSQL("DROP TABLE IF EXISTS Project");
            db.execSQL(CREATE_TABLE_PROJECT);

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(DBADAPTER_TAG, "Upgrading database from version " + oldVersion + " to         "
                    + newVersion + ", which will destroy all old data");
                db.execSQL("DROP TABLE IF EXISTS Project");
            onCreate(db);
        }
    }

    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     * 
     * @param ctx the Context within which to work
     */
    public DbAdapter(Context ctx) {
        this.mCtx = ctx;
    }

    public DbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
        }    
}

1 个答案:

答案 0 :(得分:0)

  

不幸的是,我不知道在调用i.putExtra时如何确定id的值。任何建议将不胜感激

您可以使用getItemId()

Intent i = new Intent(this, ProjectEditActivity.class);
i.putExtra(ProjectDbAdapter.ROW_ID, item.getItemId());
startActivityForResult(i, ACTIVITY_EDIT);