Neeha
Neeha

Reputation: 737

gridview with dynamic textview & drawable images

Iam developing an Android App which shows a gridview display of images & with textview on that.Till now i did with gridview display of images from drawable.I got text values from json in arraylist variable.

code i used to display only images:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_front_page);


    GridView grid=(GridView)findViewById(R.id.maincatgrid);

     grid.setAdapter(new ImageAdapter(this));
     grid.setColumnWidth( 170 );
     grid.setVerticalSpacing(20 );
     grid.setStretchMode( GridView.STRETCH_COLUMN_WIDTH );
     grid.setNumColumns( GridView.AUTO_FIT );


}

       public class ImageAdapter extends BaseAdapter {

    private Context mContext;


    public ImageAdapter(Context c){
        mContext = c;
    }

      public Integer[] mThumbIds = {
                R.drawable.image1, 
                R.drawable.image2,
                R.drawable.image3,
                R.drawable.image4, 
                R.drawable.image5,
                R.drawable.image6, 
                R.drawable.image7, 
                R.drawable.image8, 


        };

    public int getCount() {
        // TODO Auto-generated method stub
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mThumbIds[position];
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View MyView = convertView;
        // TODO Auto-generated method stub

        ImageView imageView;
        if (convertView == null) {

            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(170, 150));
            imageView.setAdjustViewBounds(false);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);



        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);

        return imageView;
    }



}
 }

Iam getting textview values from a asynchronous task which contains 15 values.when no image from mThumbIds(i.e., textvalues>images)then it should put my default image R.drawable.no_image.Is there any way to achieve this then plz suggest me.Thanks in advance.

Upvotes: 1

Views: 820

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

https://github.com/nostra13/Android-Universal-Image-Loader.

Can load images localy or from server.

It is based on Lazy List(works on same principle). But it has lot of other configurations. I would prefer to use Universal Image Loader coz it gives you more configuration options. You can display a error image if downlaod failed. Can display images with rounded corners. Can cache on disc or memory. Can compress image.

In your custom adapter constructor

File cacheDir = StorageUtils.getOwnCacheDirectory(activity context, "your folder");//for caching

// Get singletone instance of ImageLoader
imageLoader = ImageLoader.getInstance();
// Create configuration for ImageLoader (all options are optional)
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
  // You can pass your own memory cache implementation
 .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
 .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
 .enableLogging()
 .build();
 // Initialize ImageLoader with created configuration. Do it once.
 imageLoader.init(config);
 options = new DisplayImageOptions.Builder()
 .showStubImage(R.drawable.stub_id)//display stub image untik image is loaded
 .cacheInMemory()
 .cacheOnDisc()
 .displayer(new RoundedBitmapDisplayer(20))
 .build();

In your getView()

  ImageView image=(ImageView)vi.findViewById(R.id.imageview); 
  imageLoader.displayImage(imageurl, image,options);//provide imageurl, imageview and options.

You can configure with other options to suit your needs.

Along with lazy loading/Universal Image Loader you can view holder for smooth scrolling and performance. http://developer.android.com/training/improving-layouts/smooth-scrolling.html.

Upvotes: 1

Related Questions