我可以在我的应用程序中单击按钮时保存和删除 sharePreferences 文件中的项目。当我按下按钮时,所选项目应显示在下一个 Favorite_Activity 的图库中。如何从 SharedPreferences 文件动态添加和删除图库中的项目-Favorite_Activity?谢谢求助。
有我的画廊代码
int[] imgID = { R.drawable.health, R.drawable.health_1, R.drawable.health_2,
R.drawable.health_3, R.drawable.health_4, R.drawable.health_5,
R.drawable.health_6, R.drawable.health_7, R.drawable.health_8,
R.drawable.health_9,R.drawable.health_10,R.drawable.health_11 };
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favorite);
imageView = (ImageView) findViewById(R.id.ImageFavoriteView);
imageView.setImageResource(imgID[0]);
gallery = (Gallery) findViewById(R.id.FavoriteGallery);
// creating AddImgadapter
gallery.setAdapter(new AddImgAdapter(this));
// getting gallery item click listener
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> perent, View view,
int position, long id) {
imageView.setImageResource(imgID[position]);
Log.d(MY_LOG, "img Id"+position);
}
});
}
// new our adapter
public class AddImgAdapter extends BaseAdapter {
int GalItemBg;
Context count;
public AddImgAdapter(Context c) {
count = c;
// taking Gallery attributes and resource id theme
TypedArray typeArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typeArray.getResourceId(
R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typeArray.recycle();
}
// amount of element
@Override
public int getCount() {
return imgID.length;
}
// position of element
@Override
public Object getItem(int position) {
return position;
}
// id of element
@Override
public long getItemId(int position) {
return position;
}
//setting view image resourses params position and backgroud resourses
@Override
public View getView(int position, View converView, ViewGroup parent) {
ImageView newImageView = new ImageView(count);
newImageView.setImageResource(imgID[position]);
newImageView.setLayoutParams(new Gallery.LayoutParams(130, 100));
newImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
newImageView.setBackgroundResource(GalItemBg);
return newImageView;
}
}