I am developing an app where I have a list of bitmaps and I want to place those bitmaps in a gridView. I have the folleowing code.
My Activity for gridView
public class TestBitmap extends Activity {
private Bitmap bitmap;
private ImageView image;
public static List<Bitmap> splittedBitmaps;
#Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.splitted_grid);
splittedBitmaps = getIntent().getParcelableArrayListExtra("split");
GridView gv = (GridView)findViewById(R.layout.splitted_grid);
gv.setAdapter(new SplittedImageAdapter(this));
}
}
and this is my Adapter class
public class SplittedImageAdapter extends BaseAdapter{
private Context mContext;
public SplittedImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return TestBitmap.splittedBitmaps.size();
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(30,30));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(1, 1, 1, 1);
//imageView.setImageResource(TestBitmap.splittedImages.get(arg0));
imageView.setImageBitmap(TestBitmap.splittedBitmaps.get(arg0));
return imageView;
}
}
I am getting a NullPointerException in the last line of the onCreate method of my Activity class.
Please help me to trace the bug.
In last line of onCreate gb is null. That means (GridView)findViewById(R.layout.splitted_grid); is returning null, which means it is not finding the GridView.
I believe the argument of findViewById is incorrect. R.layout.splitted_grid is a name of layout file. Instead you should use the ID of a view, eg. R.id.your_view_id_here if you have ID specified in your layout XML like that:
<GridView
android:id="#+id/your_view_id_here"
(...)
/>
Related
Is there any way to remove selected item from gridview.
I want to delete the selected item from my gridview.
I did not find any thing . my code is
public class ImageAdapter extends BaseAdapter{
Context context;
public ImageAdapter(Context context)
{
this.context = context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 5, 0, 0);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
public Integer[] mThumbIds = {
R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,
R.drawable.sample_3,R.drawable.sample_1,R.drawable.sample_2,
R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_1
};
}
//////////////////
public class ImageActivity extends Activity {
ImageAdapter iAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
iAdapter = new ImageAdapter(this);
final GridView gView = (GridView)findViewById(R.id.grid_view);
gView.setAdapter(iAdapter);
gView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//gView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
// gView.setItemChecked(position, true);
Toast.makeText(ImageActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
iAdapter.notifyDataSetChanged();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_image, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.menu_delete)
{
Toast.makeText(this, "Delete",Toast.LENGTH_SHORT ).show();
}
return super.onOptionsItemSelected(item);
}
}
can anyone have idea .
thank
you are using a table :
public Integer[] mThumbIds = {
R.drawable.sample_1,R.drawable.sample_2,R.drawable.sample_3,
R.drawable.sample_3,R.drawable.sample_1,R.drawable.sample_2,
R.drawable.sample_2,R.drawable.sample_3,R.drawable.sample_1}
Tables are not modifiable.
Replace it by a List on which you will be able to make add or remove operations. Simply call notifyDataSetChanged when a change is made to let the adapter know that its list has been modified.
As Teovald and pskink suggested, you cannot have a fixed list of images and then implement the remove functionality that you are looking for. If you want to add remove, change your design and make sure your data set is also removable. What you have tried so far seems to be using some really basic code which is good to show basic GridView feature.
Just try this. Create a image data set class which returns the actual images. Store the images in a List that can be modified. Add setters/getters to this data set and also add a method to delete a particular item. Change your image adapter to use the image from this new data set. Whenever an image is deleted, call the notifyDataSetChanged on the adpater.
Good luck
I'm new to android domain..
I'm working with small app..
What i need is ??
I have videos urls and image urls in a array list,which retrive from database as json object and stored in separate array. I want this array list of images should show in listview with text.
How to implement this?? please help me..
I have went through google but still i didn't clear example.. Please any one help me..
Thanks a lot in advance...
public class act extends Activity {
/** Called when the activity is first created. */
static String uri1="http://i3.ytimg.com/vi/bQaWsVQSLdY/default.jpg";
static String uri2="http://i4.ytimg.com/vi/cJQCniWQdno/mqdefault.jpg";
static String uri3="http://i1.ytimg.com/vi/D8dA4pE5hEY/mqdefault.jpg";
public static String[] urls={uri1,uri2,uri3};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView grd=(GridView)findViewById(R.id.gridView1);
grd.setAdapter(new ImageAdapter(this));
grd.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent,View v,int pos,long id)
{
Toast.makeText(getBaseContext(),"pic"+(pos+1)+"select ",Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter
{
private Context context;
private int itemBackground;
ImageAdapter(Context c)
{
context=c;
TypedArray a=obtainStyledAttributes(R.styleable.Gallery1);
itemBackground=a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground,0);
a.recycle();
}
public int getCount()
{
return urls.length;
}
public Object getItem(int pos)
{
return pos;
}
public long getItemId(int pos)
{
return pos;
}
public View getView(int pos,View cv,ViewGroup vg)
{
ImageView imageview=new ImageView(context);
imageview.setImageResource(urls[pos]);
imageview.setScaleType(ImageView.ScaleType.FIT_XY);
imageview.setLayoutParams(new Gallery.LayoutParams(150,120));
imageview.setBackgroundResource(itemBackground);
return imageview;
}
}
}
I try like this..i can't able to get the image...
you can't set it directly as you are trying to do, you will first need to download the image,
store the bitmap, and only then apply the image to you ImageView.
check this question:
How to set image button resource from web url in Android?
this solution is good as well:
How to load an ImageView by URL in Android?
If the requirement is of showing the imageview and textview in list row then try the concept of universal loader.
Link:
https://github.com/nostra13/Android-Universal-Image-Loader
I'm using a listview in my android application in which the listitems(in my case it is bitmap images) are loaded dynamically. Actually i'm creating the bitmap images and then it is loaded one by one into the list. what i want is to show all the list items with some default image and update them correspondingly when the bitmap image is created. My code is given below,
public class BitmapDemoActivity extends Activity {
HorizontalListView listview;
Vector<Bitmap> thumbImg;
BitmapCreator creator;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listviewdemo);
creator=new BitmapCreator();
thumbImg= new Vector<Bitmap>(97);
listview = (HorizontalListView)findViewById(R.id.listview);
listview.setAdapter(new BitmapAdapter());
new AsyncBitmapCreate().execute();
}
private class AsyncBitmapCreate extends AsyncTask<Void, Bitmap, Void>{
//Bitmap[] temp=new Bitmap[44];
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
for(int i=0;i<97;i++){
publishProgress(creator.generateBitmap(i+1));
}
return null;
}
#Override
protected void onProgressUpdate(Bitmap... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
new BitmapAdapter().add(values[0]);
new BitmapAdapter().notifyDataSetChanged();
}
}
class BitmapAdapter extends BaseAdapter{
public void add(Bitmap bitmap)
{
Log.w("My adapter","add");
thumbImg.add(bitmap);
}
#Override
public int getCount() {
return thumbImg.capacity();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
View retval = inflater.inflate(R.layout.listitem, null);
ImageView img = (ImageView) retval.findViewById(R.id.tImage);
img.setImageBitmap(thumbImg.get(position));
return retval;
}
};
}
Here i'm using a vector in which after creating each bitmap, it is inserted into that vector. I'm using an asynctask to create the bitmap. After each bitmap is created i'm calling notifydatasetchanged() method to update the listview. But now in the output whenever each bitmap image is created it is adding one item in the listview with that image. But my requirement is to show all the 97 items in my list with some default image and whenever bitmap is created update the corresponding listitem.
can anyone help me?? Thanks in advance....
The simplest would be to include the default image as the src for the ImageView with id tImage in your layout listitem.xml. And in your getView method, replace the default image if the Bitmap for that position is available.
ImageView img = (ImageView) retval.findViewById(R.id.tImage);
Bitmap bmp = null;
if(position < thumbImg.size()){
thumbImg.get(position);
}
if(null != bmp){
img.setImageBitmap(bmp);
}
i have a big doubt about memory leak and screen rotation. I know that activity is destroy when that happens and new one is started. With my code, with is the best way to show the gridview? (this activity is part of a tab activity so in the main one i get the pictures from web services and put them on the object, using AsyncTask)
i would like to show the pictures at the same time they are saved on the object. Like simulating ajax...is that possible? at the moment, they are all show on the start or resume.
Also, im facing memory leak here?
last question, how can i show photos in the grid
my code:
public class LoteFotosActivity extends Activity {
Sistema sis=Sistema.getInstance();
Lote lote;
GridView gridview;
ImageAdapter iA;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lote_foto_view);
Bundle extras = getIntent().getExtras();
lote=sis.getLoteDetalle(extras.getInt("LoteID"));
gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(iA=new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(LoteFotosActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return lote.FOTOS.size();
}
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(200,200));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageBitmap(lote.FOTOS.get(position));
return imageView;
}
}
//test looking
public void onResume() {
super.onResume();
Toast.makeText(this,"ON RESUME",
Toast.LENGTH_SHORT).show();
// gridview.setAdapter(new ImageAdapter(this));
// foto.setImageDrawable(lote.FOTOS.get(0));
}
public void onPause() {
super.onPause();
Toast.makeText(this,"onPause",
Toast.LENGTH_SHORT).show();
iA=null;
gridview.setAdapter(iA=new ImageAdapter(this));
// foto.setImageDrawable(lote.FOTOS.get(0));
}
thx in advance !
I think you should add this to your activity
android:configChanges="orientation"
to avoid the destroying problem when rotating the screen... with this you dont need to save state to restore, there is no need because the activity is not destroyed and recreated everytime the screen rotation changes
For API 13+ the screenSize must also be handled:
android:configChanges="orientation|screenSize"
i want to add a images to grid view and then to linear layout.i tried below code
protected LinearLayout asLayout(final String message,final String path,boolean back){
LoaderImageView liv=new LoaderImageView(this,path);
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(100,120));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
(imageView).setPadding(10, 10, 10, 10);
liv.setBackgroundColor(0xFF000000);
linearWrapper = new GridView(mContext);
linearWrapper.addView(asLayout(fmsg,fpath,true));
linearWrapper.addView(asLayout(smsg,spath,false));
linerLayout.addView(linearWrapper);
the gridview was added but the images in grid view are not added .so please tell me the solution to how to add the gridview images to linearlayout.
Thanks in advance
Best Regards
You need an adapter for the grid view. Look at this to learn how to add adapters.
Edit 1: Sample code:
public class TestGrid extends Activity {
int[] myImages;
#Override public void OnCreate(Bundle icicle) {
super.onCreate(icicle);
GridView mGridView = new GridView(this);
/** Set up your data array with resource id's from your app. */
setAdapter(new TestAdapter());
setContentView(mGridView);
}
private class BenchAdapter extends BaseAdapter {
#Override public int getCount() { return (mContent != null) ? myImages.length : -1; }
#Override public Object getItem(int pos) { return pos; }
#Override public long getItemId(int pos) { return pos; }
#Override public View getView(int pos, View view, ViewGroup parent) {
if (myImages == null) return null;
GridView.LayoutParams lp = null;
if (getWidth() < getHeight()) lp = new GridView.LayoutParams(getWidth()/3, getHeight()/2);
else lp = new GridView.LayoutParams(getWidth()/2, getHeight()/3);
ImageView iv = new ImageView(TestGrid.this);
iv.setBackgroundResource(myImages[pos]);
iv.setLayoutParams(lp);
return iv;
}
}
Now what this will do is set a gridview as the activities content view. The Adapter will fill the content of the gridview. Without the adapter, the gridview would have no idea of what it should display.