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