何时使用Exception(When to use Exception)
我有一个表单或页面以及分配给使用此页面的用户的一些权限(保存,修改,删除等),假设此用户仅具有“保存”和“修改”权限。
当用户点击他没有特权的删除按钮时,我想向他显示一条消息,告诉他他没有这个特权,我的代码怎么样?
选项1
If(loggedUser.Privileges.Contains(PrivilegeTypes.Delete) == false) { MessageBox.Show("You dont have delete privilege!"); // Log that user tried to delete without permission etc return; }
选项2
try { If(loggedUser.Privileges.Contains(PrivilegeTypes.Delete) == false) { // Throw custom exception throw new UngrantedPrivilegeException(PrivilegeTypes.Delete, "Invalid privilege exception"); } } catch (UngrantedPrivilegeException ex) { if( ex.PrivilegeType == PrivilegeTypes.Delete) { MessageBox.Show("You dont have delete privilege!"); // Log that user tried to delete without permission etc } }
I have a Form or Page and some privileges (Save, Modify, Delete etc.) assigned to a user that is using this page, let's assume this user only has Save and Modify privileges.
When user clicks Delete button, which he is not privileged to, I want to display a message to him to inform him that he doesnt have this privilege, how should my code look like?
Option 1
If(loggedUser.Privileges.Contains(PrivilegeTypes.Delete) == false) { MessageBox.Show("You dont have delete privilege!"); // Log that user tried to delete without permission etc return; }
Option 2
try { If(loggedUser.Privileges.Contains(PrivilegeTypes.Delete) == false) { // Throw custom exception throw new UngrantedPrivilegeException(PrivilegeTypes.Delete, "Invalid privilege exception"); } } catch (UngrantedPrivilegeException ex) { if( ex.PrivilegeType == PrivilegeTypes.Delete) { MessageBox.Show("You dont have delete privilege!"); // Log that user tried to delete without permission etc } }
原文:https://stackoverflow.com/questions/20858284
最满意答案
是的,使用
ListView
或RecyclerView
主要原因是它们为您提供了View
重用。 它不会一次加载所有子视图:它会要求您在需要时将数据绑定到View
。Yes, the main reason of using
ListView
orRecyclerView
is that they provide you withView
re-use. It won't load all the child-Views in one go: it will ask you to bind your data to aView
whenever needed.
相关问答
更多-
我假设“创建视图时发生异常”,表示您在编辑器中看到异常。 这不是一个错误,而是缺乏与IDE的集成。 没有布局管理器,回收站视图不起作用。 由于设置布局管理器的代码未在IDE中执行,因此您会看到此错误。 将来会有更好的整合。 I assume by "the exception happens at creating the view" you mean you are seeing the exception in your editor. This is not a bug, rather a lack ...
-
列表视图到Recycleview(Listview to Recycleview)[2023-01-20]
我修改了你的完整适配器类如下: public class GetAllAllarmiListViewAdapter extends RecyclerView.Adapter{ private JSONArray dataArray; private Activity activity; private static LayoutInflater inflater = null; // Provide a suitab ... -
在getView()回收您的视图 使用ViewHolder模式 如果您有大量数据用于填充列表,请使用延迟加载 如果数据来自database ,则使用Cursor作为基础数据,而不是使用Cursor构建的对象列表,而不是通过创建其他对象来节省内存。 请参阅http://www.google.com/events/io/2010/sessions/world-of-listview-android.html 请参阅http://android.amberfog.com/?p=296 recycle your v ...
-
试试这个链接: https://github.com/cundong/HeaderAndFooterRecyclerView 。 您可以添加header并使用StaggeredGridLayoutManager 。 Try this link: https://github.com/cundong/HeaderAndFooterRecyclerView. You can add a header and use StaggeredGridLayoutManager.
-
最后,我在自定义视图类中创建了一个函数,其中ListView和行数作为参数。 在视图中我将setOnScrollListener设置为ListView,并在滚动到自定义视图的全局滚动变量时指定ListView的位置。 喜欢这个 public void setListView(final TwoWayView listView, int totalListRows) { totalColumns = totalListRows; for (int i = 0; i < totalColumns ...
-
试试这个 int scrollX = recycleView.computeHorizontalScrollOffset(); Try with this int scrollX = recycleView.computeHorizontalScrollOffset();
-
是的,使用ListView或RecyclerView主要原因是它们为您提供了View重用。 它不会一次加载所有子视图:它会要求您在需要时将数据绑定到View 。 Yes, the main reason of using ListView or RecyclerView is that they provide you with View re-use. It won't load all the child-Views in one go: it will ask you to bind your dat ...
-
如上所述,无论何时需要更新listview或recyclerview中的项目行,都应该使用notifiyDataSetchange。 所以,你走了 首先,您必须在项模型类中创建setter和getter方法以更新列表视图行。 如下 public class Item { private boolean mIsSelected; public void setIsSelected(boolean isSelected) { mIsSelected = isSelected; } ...
-
如果你的recyclerView是match_parent,你可以使用smth之类的 DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics(); float dpHeight = displayMetrics.heightPixels / displayMetrics.density; 所以你可以holder.mView.setheight(dpHeight/6); 另一种方式是 @Override pu ...
-
我想你每次调用getTimeLineData()时都会创建一个新的项目列表: timeLineItems = new ArrayList<>(); 首先检查您是否已有列表: if (timeLineItems == null) { timeLineItems = new ArrayList<>(); } I think you create a new items list each time you call getTimeLineData(): timeLineItems = new Arr ...