所以我已经对这个主题进行了研究,并且有很多方法可以对数组中的字符串进行收藏。有一个收藏按钮,用户单击该按钮以收藏将在数组中的特定显示字符串。我在这个类中提出了一个加载数组和保存数组的方法。我在 loadArray(favorites, this); 和 saveArray(favorites, "favorites", this); 它似乎没有将 loadArray 或 saveArray 识别为一种方法。非常感谢!
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
}
}