[英]Android gridview onItemClickListener to set activity background image
I'm pretty much stuck with my android app... I am trying to make a gridview with images 我几乎坚持我的Android应用程序......我正在尝试使用图像进行gridview
and use OnItemClickListener() to set an image as background. 并使用OnItemClickListener()将图像设置为背景。 THE PROBLEM I HAVE IS... When i set the image as background it disappears when the activity is closed. 我遇到的问题......当我将图像设置为背景时,它会在活动关闭时消失。
I want the user to set the background image by selecting from the gridview without disappearing when the user exits the activity. 我希望用户通过从gridview中选择来设置背景图像,而不会在用户退出活动时消失。
////////////My Code/////////////////// ////////////我的代码///////////////////
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(120, 120));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = { R.drawable.bg, R.drawable.bg_010,
R.drawable.bg_003, R.drawable.bg_007, R.drawable.bg_001,
R.drawable.bg_006, R.drawable.bg_002, R.drawable.bg_011,
R.drawable.bg_004, R.drawable.bg_008, R.drawable.bg_005,
R.drawable.bg_009
};
}
//////Activity...........///////// //////活动.........../////////
gridview = (GridView) findViewById(R.id.grid_skin_view);
gridview.setAdapter(new ImageAdapter(this));
ly = (RelativeLayout) findViewById(R.id.rev_ly);
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
ly.setBackgroundResource(R.drawable.bg_003);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor edit = settings.edit();
int bgDrawableUri = position;
edit.putInt("bg_drawable_uri", bgDrawableUri);
edit.commit();
}
});`
}
}
Have a look at SharedPreferences for storing data. 查看SharedPreferences以存储数据。 There's plenty of StackOverflow examples like this on how to use. 有大量的例子StackOverflow上像这样如何使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.