Android 我需要添加一种方法来编辑、删除或启动一个活动,该活动根据列表视图中的选择显示数据库记录
我正在编写一个显示属性列表的应用程序。当前,当您单击某个地址时,它将启动编辑活动,当您长按该地址时,它将显示一个上下文菜单,其中包含“删除”选项Android 我需要添加一种方法来编辑、删除或启动一个活动,该活动根据列表视图中的选择显示数据库记录,android,contextmenu,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, ProjectEditA
@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();
}
}
不幸的是,我不知道如何在调用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();
}
}
您可以使用:
我试过了,但失败了。item.getItemId()不只是返回从菜单中选择的项目的位置吗?我需要的是数据库分配给列表中显示的记录的索引号。
ProjectDbAdapter
是ListView的自定义适配器吗?它是CursorAdapter
的子类吗?它是listview的自定义适配器。当我在VB中编写代码时,我可以用记录索引嵌入一个隐藏字段,但我认为我不能在listview中这样做。我开始认为我需要创建一个包含listindex和相应记录索引的数组,并解析它以调用intent,除非有更简单的方法。顺便说一句,感谢您的帮助如果ProjectDbAdapter
是CursorAdapter
的子类,那么应该为您提供id。发布你的ProjectDbAdapter
代码。必须删掉很多内容才能适合。下面是相关代码公共类ProjectDbAdapter扩展DbAdapter{private static final String DATABASE_TABLE=“Project”;…公共ProjectDbAdapter(Context ctx){super(ctx);}公共游标getProject(long rowId)抛出SQLException{Cursor mCursor=this.mDb.query(DATABASE_TABLE,new String[]{ROW_ID,STREET,CITY,STATE,ZIP,OTHER},ROW_ID+“=”+rowId,null,null,null);if(mCursor!=null){mCursor.moveToFirst();}返回mCursor;}