justdeko
justdeko

Reputation: 1124

Getting context for SharedPreferences in a method

So I have a pdf plugin in my app, an it uses a public class SimpleDocumentReader. It's just a class, not an activity. I have a method where you can change the page of the document when it has finished loading:

@Override    
public void onLoadFinish(DocumentState.OPEN state) {
    if (state == OPEN.SUCCESS) {
    SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences();
                String page = pref.getString("example_list","");
                goToPage(Integer.parseInt(page));
       }
    }

As you can see, I want to access the defaultsharedpreferences, where a string is stored with which I can change the page. The problem is, in tha brackets of getDeaultSharedPreferences(); I need the context. I have tried several things already:

getApplicationContext() (didn't work),

getActivity() also pretty stupid.

Also tried to make the method receive the context like this:

public void onLoadFinish(DocumentState.OPEN state, Context context) {

// line for getting sharedpreferences Context:
SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(context);
}

But it throws several errors: First, the @Override isn't working because

method does not override method from its superclass

Second error is at the top, the class itself:

public class SimpleDocumentReader implements ReaderListener {}

Throws this error:

Class "SimpleDocumentReader" must either be declared abstract or implement abstract method 'onLoadFinish(OPEN)' in ReaderListener.

What does that mean?

EDIT: Here's how the SimpleDocumentReader class is being initiated:

First, creating a SimpleViewer with a SimpleReaderFactory in my Activity:

SimpleDocumentReader v = SimpleReaderFactory.createSimpleViewer(this, null);
v.openUrl("myurl", "mypassword");

Second, the SimpleReaderFactory:

public class SimpleReaderFactory {

    /**
     * Creates and returns a PDF viewer used for viewing, reading, and
     * navigating PDF document
     * 
     * @param act
     *            The activity of the current application.
     * @param listener
     *            The SimpleDocumentViewerListener instance.
     * 
     * @return SimpleDocumentViewer The default PDF document viewer instance
     */
    public static SimpleDocumentReader createSimpleViewer(Activity act,
            SimpleDocumentReaderListener listener) {

        SimpleDocumentReader viewer = new SimpleDocumentReader(act);
        viewer.setListener(listener);


        return viewer;
    }
}

And, at last, my SimpleDocumentReader:

    public class SimpleDocumentReader implements ReaderListener {
    // a lot of stuff here, I will post the important snippets of code:
    public void onLoadFinish(DocumentState.OPEN state) {
    if (state == OPEN.SUCCESS) {
                mControlView.init(mAct);

                RelativeLayout layout = new RelativeLayout(mAct);
                layout.addView(mReaderView);
                layout.addView(mControlView);

                mAct.setContentView(layout);

                CoordConverter.initCoordConverter(mAct, mReaderView);
                SharedPreferences pref= 
//my attempts at getting the defaultsharedpreferences
PreferenceManager.getDefaultSharedPreferences();
                String hour = pref.getString("example_list","");
                goToPage(Integer.parseInt(hour));
            }
}

Upvotes: 0

Views: 99

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007359

What does that mean?

Just what it says. ReaderListener requires you to have an onLoadFinish() with the signature public void onLoadFinish(DocumentState.OPEN state), apparently.

With regards to your overall problem, pass the SharedPreferences into the constructor of SimpleDocumentReader, which holds onto it in a data member and uses it in onLoadFinish().

Upvotes: 1

Related Questions