使 getContentResolver() 在扩展 Fragment 类的类中工作 [英] Making getContentResolver() to work in class extending Fragment class

查看:71
本文介绍了使 getContentResolver() 在扩展 Fragment 类的类中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想到的是使用两个片段,第一个显示联系人列表,第二个显示正在上方片段中选择的联系人的详细信息.
由于上下文问题,我用来扩展 Fragment 类的类不允许我使用 getContentResolver() 方法.现在我试图在扩展 Fragment 类的同一个类中获取联系人,并使用它在列表视图中显示及其详细信息.在经历了一些较旧的解决方案后,我找到了创建函数并将 Context 作为参数传递给它的方法,但问题是我不必从任何其他扩展 Activity 的类中调用它.我想从扩展 Fragment 的同一个类中做到这一点.

What I have in mind is to use two fragments, first one to show the contacts list and second one to show the details of contact which is being selected in the upper fragment.
The class that I'm using to extend the Fragment class does not allow me to use getContentResolver() method due to the context issue. Now I'm trying to fetch the contact in the same class which is extending Fragment class and use it to show in the list view and its details. After going through some older solution I found the way to create a function and pass Context to it as a parameter but the problem is I don't have to call it from any other class which is extending Activity. I want to do it from that same class extending Fragment.

我该怎么做?

任何帮助将不胜感激.

推荐答案

尝试将此代码添加到以前的活动中:

Try to add this code to a previous activity:

// a static variable to get a reference of our application context
public static Context contextOfApplication;
public static Context getContextOfApplication()
{
return contextOfApplication;
}

并在同一个活动中,在您的 onCreate 方法中添加以下行:

and in the same activity, in your onCreate method add this line:

 contextOfApplication = getApplicationContext();

在您的片段中,您可以使用:

In your fragment you can access this by using :

Context applicationContext = YourActivity.getContextOfApplication();
applicationContext.getContentResolver();

这篇关于使 getContentResolver() 在扩展 Fragment 类的类中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆