0

プロパティのリストを表示するアプリを作成しています。現在、アドレスをクリックすると編集アクティビティが開始され、アドレスを長押しすると、オプションとして削除を含むコンテキスト メニューが表示されます。

@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();
        }    
}
4

1 に答える 1

0

残念ながら、i.putExtra の呼び出しで id の値を決定する方法がわかりません。任意の提案をいただければ幸いです

使用できますgetItemId()

Intent i = new Intent(this, ProjectEditActivity.class);
i.putExtra(ProjectDbAdapter.ROW_ID, item.getItemId());
startActivityForResult(i, ACTIVITY_EDIT);
于 2012-12-28T18:29:57.587 に答える