EnableListAdapter 中的上下文

发布于 2024-11-04 14:12:06 字数 576 浏览 3 评论 0原文

我创建了一个 ExpandableListAdapter 类,我需要从访问它的活动向它发送上下文。

MyActivity.class:

MenuExpandableListAdapter.useInstanceContext(getApplicationContext());

MyExpandableListAdapter.class:

static Context context;
public static void useInstanceContext(Context applicationContext) {
    context = applicationContext;
}

上面的代码可以工作,但这也可以:

MenuExpandableListAdapter.useInstanceContext(this.getApplicationContext());

有什么区别?这是传递上下文的好方法吗?我仍在尝试完全理解上下文。

I have created an ExpandableListAdapter class and I need to send it the context from the activity that is accessing it.

MyActivity.class:

MenuExpandableListAdapter.useInstanceContext(getApplicationContext());

MyExpandableListAdapter.class:

static Context context;
public static void useInstanceContext(Context applicationContext) {
    context = applicationContext;
}

The above code works, but this also works:

MenuExpandableListAdapter.useInstanceContext(this.getApplicationContext());

What's the difference? Is this a good way to pass context? I'm still trying to fully understand context.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愁杀 2024-11-11 14:12:06

为了访问资源和其他一些东西,上下文是必要的。因此,应用程序和活动上下文都有效。但良好的实践是紧抓最有效的最小事情,即您的案例中的活动。因此,我建议您使用新方法:

MenuExpandableListAdapter.useInstanceContext(this);

此外,在您的示例中,调用之间没有区别。 this 只是对当前对象的引用。

Context is necessary in order to get access to the resources and some other things. So, both - application and activity contexts work. But a good practice is tight to the smallest thing, which works, which is activity in your case. So, I would suggest new way for you:

MenuExpandableListAdapter.useInstanceContext(this);

Also, in your example, there is no difference between the calls. this is just the reference to the current object.

孤独岁月 2024-11-11 14:12:06

this指的是当前正在执行代码的对象,如果该方法是在同一个类中声明的,并且不是static的,那么调用也是一样的:

getApplicationContext()

and

this.getApplicationContext()

(类成员也是如此)

this refers to the object that is currently executing code, if the method is declared in the same class, and is not static, it is the same to call:

getApplicationContext()

and

this.getApplicationContext()

(The same applies to class members)

野侃 2024-11-11 14:12:06
MenuExpandableListAdapter.useInstanceContext(getApplicationContext());

getApplicationContext() 方法将通过使用调用/当前对象隐式调用。

其次,您显式地给出调用/当前对象,因为这是始终引用调用对象的特殊对象。

我建议你使用这个,

MenuExpandableListAdapter.useInstanceContext(this.getApplicationContext());

因为根据我的阅读,这是一个很好的做法。

MenuExpandableListAdapter.useInstanceContext(getApplicationContext());

The getApplicationContext() method will called by using calling/current object implicitly.

in second you are giving calling/current object explicitly because this is special object that is always refer to calling object.

i suggest you to use this

MenuExpandableListAdapter.useInstanceContext(this.getApplicationContext());

because as per my reading it's good practice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文