In my app, I set GridView
with an adapter. In first time it loads correctly, but after closing my app and then if I reopen, then the content of gridview
doubles. (i.e) each data in gridview
is again added at end. I don't know the mistake.
MainActivity:
girGridView=(GridView) findViewById(R.id.gridView1_bir);
ImageAdapter adapter;
adapter=new ImageAdapter(MainActivity.this);
girGridView.setAdapter(adapter);
girGridView.setColumnWidth(100);
ImageAdapter.class:
public class ImageAdapter extends BaseAdapter implements ListAdapter {
private Context context;
public ImageAdapter(Context context) {
super();
this.context = context;
GridViewConfig.addImageUrls();
}
public ImageAdapter(Context context1, String ff) {
super();
String g=ff;
this.context = context1;
GridViewConfig.addImageUrls(g);
}
@Override
public int getCount() {
return GridViewConfig.getResim_list().size();
}
@Override
public Object getItem(int position) {
return GridViewConfig.getResim_list().get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if(convertView==null)
{
imageView=new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageDrawable(LoadImageFromURL(GridViewConfig.getResim_list().get(position)));
}else{
imageView=(ImageView)convertView;
}
return imageView;
}
private Drawable LoadImageFromURL(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src");
return d;
}catch (Exception e) {
System.out.println(e);
return null;
}
}
}
I found my solution, i just added created new Arraylist while adding image to gridview. Thats it. (i.e)I created arraylist to store the images in GridViewConfig class. Now i changed to the creation of arraylist just before adding the images.( Adding images to newly created array foe each and every time it loads)Hence i solved my problem. Thank You to all who helped.