长按子项目,我弹出菜单,把此项目删掉,
问题是: 我无法获知这个项目(item)在listview中的位置。我的代码如下,应该怎么改?
class SomeActivity{ ListView mBooksView ;
.../**
* 长按某个书签的响应菜单创建
*/
void longClickMenuStrategyProcess() {
if (mBooksView == null) {
Log.e("Error", "book s view not created.");
return;
}
mBooksView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { @Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
BooksAdapter.ViewHolder viewHolder = (ViewHolder) v.getTag();
Log.i("Info", String.valueOf(viewHolder.mTitle));
menu.setHeaderTitle(R.string.menu_book_title);
menu.add(0, BooksActivity.DELETE_THIS_BOOKMARK, 0, R.string.menu_book_deleteThis);
menu.add(0, BooksActivity.DELETE_ALL_BOOKMARKS, 0, R.string.menu_book_deleteAll);
menu.add(0, BooksActivity.CANCEL_THEN_RETURN, 0, R.string.menu_book_cancel);
}
});
} /**
* 长按菜单响应函数 ,用弹出菜单的方式确认删除,删除全部,或取消返回
*/
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case BooksActivity.DELETE_THIS_BOOKMARK:
// 高人帮忙告诉,这里是哪个 item 被选中了。
int id = ??? //item.getItemId();//这里,这个id
if (mBooksMgr != null && mBooksMgr.delBookMarkRecord(id)) {// do not notify when there is no deletion happened.
mAdapter.notifyDataSetChanged();
}
break;
case BooksActivity.DELETE_ALL_BOOKMARKS:
if (mBooksMgr != null) {
mBooksMgr.clearBookMarkRecord();
mAdapter.notifyDataSetChanged();
}
break;
default:
// choosing cancel_and_return will reach here.
break;
} return super.onContextItemSelected(item);
}
}
// Adapter 代码附上:public class BooksAdapter extends BaseAdapter { static public class ViewHolder {
TextView mTitle;
TextView mTime;
TextView mPageNum;
} private List<BookRecord> mItems;
private LayoutInflater mInflater; public BooksAdapter(Context context, List<BookRecord> items) {
mItems = items;
mInflater = LayoutInflater.from(context);
} @Override
public int getCount() {
return mItems.size();
} @Override
public Object getItem(int position) {
if (mItems.isEmpty()) {
return null;
}
else {
if (position >= 0 && position < mItems.size()) {
return mItems.get(position);
}
else {
return null;
}
}
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_book_listitem, null);
holder = new ViewHolder();
holder.mTitle = (TextView)convertView.findViewById(R.id.book_title);
holder.mTime = (TextView)convertView.findViewById(R.id.book_description_time);
holder.mPageNum = (TextView)convertView.findViewById(R.id.book_description_pagenum); convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
BookRecord record = mItems.get(position);
if (record != null) {
holder.mTitle.setText(record.mBookTitle);
holder.mTime.setText(record.mRecordTime);
holder.mPageNum.setText(getPageNumShowStr(record));
} return convertView;
} public void remove(int position) {
mItems.remove(position);
notifyDataSetChanged();
} private String getPageNumShowStr(final BookRecord record) {
if (record == null || record.mRecordTime == null) {
Log.e("book record data error", "record time or dest page is null");
return "";
}
return " 第" + Long.toString(record.mDestPage) + "页";
}}
问题是: 我无法获知这个项目(item)在listview中的位置。我的代码如下,应该怎么改?
class SomeActivity{ ListView mBooksView ;
.../**
* 长按某个书签的响应菜单创建
*/
void longClickMenuStrategyProcess() {
if (mBooksView == null) {
Log.e("Error", "book s view not created.");
return;
}
mBooksView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { @Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
BooksAdapter.ViewHolder viewHolder = (ViewHolder) v.getTag();
Log.i("Info", String.valueOf(viewHolder.mTitle));
menu.setHeaderTitle(R.string.menu_book_title);
menu.add(0, BooksActivity.DELETE_THIS_BOOKMARK, 0, R.string.menu_book_deleteThis);
menu.add(0, BooksActivity.DELETE_ALL_BOOKMARKS, 0, R.string.menu_book_deleteAll);
menu.add(0, BooksActivity.CANCEL_THEN_RETURN, 0, R.string.menu_book_cancel);
}
});
} /**
* 长按菜单响应函数 ,用弹出菜单的方式确认删除,删除全部,或取消返回
*/
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case BooksActivity.DELETE_THIS_BOOKMARK:
// 高人帮忙告诉,这里是哪个 item 被选中了。
int id = ??? //item.getItemId();//这里,这个id
if (mBooksMgr != null && mBooksMgr.delBookMarkRecord(id)) {// do not notify when there is no deletion happened.
mAdapter.notifyDataSetChanged();
}
break;
case BooksActivity.DELETE_ALL_BOOKMARKS:
if (mBooksMgr != null) {
mBooksMgr.clearBookMarkRecord();
mAdapter.notifyDataSetChanged();
}
break;
default:
// choosing cancel_and_return will reach here.
break;
} return super.onContextItemSelected(item);
}
}
// Adapter 代码附上:public class BooksAdapter extends BaseAdapter { static public class ViewHolder {
TextView mTitle;
TextView mTime;
TextView mPageNum;
} private List<BookRecord> mItems;
private LayoutInflater mInflater; public BooksAdapter(Context context, List<BookRecord> items) {
mItems = items;
mInflater = LayoutInflater.from(context);
} @Override
public int getCount() {
return mItems.size();
} @Override
public Object getItem(int position) {
if (mItems.isEmpty()) {
return null;
}
else {
if (position >= 0 && position < mItems.size()) {
return mItems.get(position);
}
else {
return null;
}
}
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_book_listitem, null);
holder = new ViewHolder();
holder.mTitle = (TextView)convertView.findViewById(R.id.book_title);
holder.mTime = (TextView)convertView.findViewById(R.id.book_description_time);
holder.mPageNum = (TextView)convertView.findViewById(R.id.book_description_pagenum); convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
BookRecord record = mItems.get(position);
if (record != null) {
holder.mTitle.setText(record.mBookTitle);
holder.mTime.setText(record.mRecordTime);
holder.mPageNum.setText(getPageNumShowStr(record));
} return convertView;
} public void remove(int position) {
mItems.remove(position);
notifyDataSetChanged();
} private String getPageNumShowStr(final BookRecord record) {
if (record == null || record.mRecordTime == null) {
Log.e("book record data error", "record time or dest page is null");
return "";
}
return " 第" + Long.toString(record.mDestPage) + "页";
}}
例如:
// Responds long click menu item
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
setTitle("点击了长按菜单里面的第"+item.getItemId()+"个项目");
return super.onContextItemSelected(item);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
setTitle("点击了菜单里面的第"+item.getGroupId()+"个项目");
return super.onContextItemSelected(item);
}
我自己解决了.关键代码: class SomeActivity implements Activity{ ListView mBooksView ; @Override
public void onCreate(Bundle savedInstanceState) {
...
registerForContextMenu(mBooksView);
} @Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle(R.string.menu_book_title);
menu.add(0, BooksActivity.DELETE_THIS_BOOKMARK, 0, R.string.menu_book_deleteThis);
menu.add(0, BooksActivity.DELETE_ALL_BOOKMARKS, 0, R.string.menu_book_deleteAll);
menu.add(0, BooksActivity.CANCEL_THEN_RETURN, 0, R.string.menu_book_cancel);
} /**
* 长按菜单响应函数 ,用弹出菜单的方式确认删除,删除全部,或取消返回
*/
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) {
case BooksActivity.DELETE_THIS_BOOKMARK:
/* 关键是这一句。能获得Adapter里的项目(item)位置。位置是 info.position */
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
if (info == null) {
return super.onContextItemSelected(item);
}
if (mBooksMgr != null && mBooksMgr.delBookMarkRecord(info.position)) {// do not notify when there is no deletion happened.
mAdapter.notifyDataSetChanged();
}
break;
case BooksActivity.DELETE_ALL_BOOKMARKS:
if (mBooksMgr != null) {
mBooksMgr.clearBookMarkRecord();
mAdapter.notifyDataSetChanged();
}
break;
default:
// choosing cancel_and_return will reach here.
break;
} return super.onContextItemSelected(item);
}
用setOnItemLongClickListener之后,可以知道项目position,可是怎么弹出菜单?
AlertDialog 的样式和菜单相同吗?
info.position 这个是精髓