import android.view.MenuItem; //导入方法依赖的package包/类
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuItem.OnMenuItemClickListener listener = new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
String name = item.getTitle().toString();
mEtLocation.setText(name);
return true;
}
};
//clear menu
if (menu.size() != 0) {
menu.clear();
}
int id_view = v.getId();
switch (id_view) {
case R.id.arrow:
try {
mLocationList = mDbHelper.getLocationList();
} catch (NullObjectException e) {
Log.d(TAG, e.toString());
}
if (mLocationList == null || mLocationList.size() == 0) {
FunctionHelper.makeSToast(mActivity, mResources.getString(R.string.no_data_available));
} else {
int i = 0;
for (Location l : mLocationList) {
//Add menu item with groupId
menu.add(ID_GROUP_LOCATION, l.getLocation_id(), l.getLocation_id(), l.getLocation_name());
menu.getItem(i).setOnMenuItemClickListener(listener);
i++;
}
menu.setHeaderTitle(R.string.choose_a_location);
}
break;
}
}