Android中的适配器应该是静态内部类还是非静态内部类
我在 Activity 中有一个 ListView,并且正在为 ListView 设置自定义适配器。
我的适配器类应该是:
private static class MyAdapter extends ArrayAdapter
或者
private class MyAdapter extends ArrayAdapter
我想只要适配器包含在活动引用中,它应该没有什么区别,但想确认这一点。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您小心使用适配器的方式,那么在适配器中保留上下文是很好的。适配器通常与其上下文(一个活动)的生命周期相关联,所以这很好。仅在有意义时才使用 WeakReference。
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.