0

So I've done research on this topic and there are many ways of doing a favorite for a string in an array. There is a favorite button that the user clicks on to favorite that particular displayed string which would be in an array. I've come up with a load array and save array method in this class. I'm getting my errors at loadArray(favorites, this); and saveArray(favorites, "favorites", this); It does not seem to recognize loadArray or saveArray as a method. Thanks so much!

public class Base extends Activity implements OnClickListener {

    Button home, search, moreapps, fav;
    TextView display;
    String [] favorites;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(starting.rt.R.layout.relationship);
        home = (Button) findViewById(starting.rt.R.id.Home);
        search = (Button) findViewById(starting.rt.R.id.search);
        moreapps = (Button) findViewById(starting.rt.R.id.moreapps);
        fav = (Button) findViewById(starting.rt.R.id.fav);
        display = (TextView) findViewById(starting.rt.R.id.tvResults);
        fav.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
                display.getText();
                loadArray(favorites, this);
                favorites = Arrays.copyOf(favorites, favorites.length+1);
                favorites[favorites.length]=display.getText().toString();
                saveArray(favorites, "favorites", this);

            }
        }); 
    }

    public String[] loadArray(String arrayName, Context mContext) {  
        SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
        int size = prefs.getInt(arrayName + "_size", 0);  
        String array[] = new String[size];  
        for(int i=0;i<size;i++)  
            array[i] = prefs.getString(arrayName + "_" + i, null);  
        return array;  
    }  

    public boolean saveArray(String[] array, String arrayName, Context mContext) {   
        SharedPreferences prefs = mContext.getSharedPreferences("preferencename", 0);  
        SharedPreferences.Editor editor = prefs.edit();  
        editor.putInt(arrayName +"_size", array.length);  
        for(int i=0;i<array.length;i++)  
            editor.putString(arrayName + "_" + i, array[i]);  
        return editor.commit();  
    } 
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }
}

2 Answers 2

1

That's because this in that scope is the onClickListener(), you could use Base.this to reference the Activity (Context).

I wouldn't recommend using getApplicationContext(), the documentation saids:

This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.

Sign up to request clarification or add additional context in comments.

2 Comments

Worked for SaveArray not load array
You could also remove the Context argument from loadArray() and saveArray() and call getSharedPreferences("preferencename", 0); because they are part of the Activity class which is already a context.
1

You are calling

 loadArray(String[], View.OnClickListener)

instead of

 loadarray(String, Context)

"favorites" is a array of strings, not a string

"this" refers to View.OnclickListener instead of Context

You can get the Context using "getApplicationContext()"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.