Should adapters in Android be static inner classes or non-static inner classes

冷暖自知 提交于 2019-12-10 02:02:19

问题


I have a ListView in an Activity and I am setting a custom adapter to the ListView.

Should my adapter class be:

private static class MyAdapter extends ArrayAdapter 

or

private class MyAdapter extends ArrayAdapter

I guess it should make no difference as long as the adapter is enclosed within the activity reference but wanted to confirm that.


回答1:


Holding onto the context is fine from within an adapter if you are careful about how you use the adapter. Adapters are usually tied to the lifecycle of their Context (an Activity) so it's fine. Use a WeakReference only if it makes sense.



来源:https://stackoverflow.com/questions/7924834/should-adapters-in-android-be-static-inner-classes-or-non-static-inner-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!