I'm pretty much stuck with my android app... I am trying to make a gridview with images
and use OnItemClickListener() to set an image as background. 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.
////////////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. There's plenty of StackOverflow examples like this on how to use.