Java 使 getContentResolver() 在扩展 Fragment 类的类中工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31852653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:41:49  来源:igfitidea点击:

Making getContentResolver() to work in class extending Fragment class

javaandroidandroid-fragments

提问by Syed Danish Ali





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 Fragmentclass 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.

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

How should I do this?

我该怎么做?

Any help will be highly appreciated.

任何帮助将不胜感激。

采纳答案by Ahmed Abidi

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;
}

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

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

 contextOfApplication = getApplicationContext();

In your fragment you can access this by using :

在您的片段中,您可以使用以下方法访问它:

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

回答by Ahmed Abidi

Getting the application context using getter doesn't work. The equivalent of above answer is

使用 getter 获取应用程序上下文不起作用。相当于上面的答案是

getActivity().getApplicationContext().getContentResolver(). 

回答by Nino van Hooff

When using android.support.v4.app.Fragment:

使用时android.support.v4.app.Fragment

getActivity().getContentResolver()

getActivity().getContentResolver()

回答by Mike Fallon

I had similar problem with a fragment, but tried

我有一个片段的类似问题,但尝试过

(getContext().getContentResolver(), filePath);

回答by Samo

if (requestCode == GALLERY) {


        if (resultCode==RESULT_OK){
            if (data!=null) {
                 Uri uri = data.getData();
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                try {
                    BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri), null, options);
                    options.inSampleSize = calculateInSampleSize(options, 100, 100);
                    options.inJustDecodeBounds = false;
                    Bitmap image = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri), null, options);
                    //imageofpic.setImageBitmap(image);

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }else {
                Toast.makeText(getActivity(), "Cancelled",
                        Toast.LENGTH_SHORT).show();
            }
}

回答by shady

Edit like this.

像这样编辑。

  Cursor contctCursorVar = getContext().getContentResolver().query(contctDataVar, null,