Sep 7, 2011

Custom adapter on both GridView and Gallery

It's a example using the same custom adapter on both GridView and Gallery. It can be noticed that adapter used on both GridView and Gallery are basically the same.



Custom adapter on both GridView and Gallery



/res/layout/gridlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>




layout file , main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="50dp"
/>
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:columnWidth="100px"
android:stretchMode="columnWidth"
android:gravity="center"/>
</LinearLayout>




main java code
package com.AndroidGridView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;

public class AndroidGridViewActivity extends Activity {

public class MyAdapter extends BaseAdapter {

final int NumberOfItem = 30;
private Bitmap[] bitmap = new Bitmap[NumberOfItem];

private Context context;
private LayoutInflater layoutInflater;

MyAdapter(Context c){
context = c;
layoutInflater = LayoutInflater.from(context);

//init dummy bitmap,
//using R.drawable.icon for all items
for(int i = 0; i < NumberOfItem; i++){
bitmap[i] = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
}
}

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

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

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

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

View grid;
if(convertView==null){
grid = new View(context);
grid = layoutInflater.inflate(R.layout.gridlayout, null);
}else{
grid = (View)convertView;
}

ImageView imageView = (ImageView)grid.findViewById(R.id.image);
imageView.setImageBitmap(bitmap[position]);

return grid;
}

}

GridView gridView;
Gallery gallery;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView)findViewById(R.id.grid);
gallery = (Gallery)findViewById(R.id.gallery);

MyAdapter adapter = new MyAdapter(this);
gridView.setAdapter(adapter);
gallery.setAdapter(adapter);

}

}





1 comment:

  1. This is nice post. Its really helpful for me me. here I have found another nice one post which helped me lot. that post is...
    http://mindstick.com/Articles/31e530dc-59df-44f6-8ccc-183051bc5399/?Display%20Data%20in%20GridView%20in%20Android

    Thanks!!!

    ReplyDelete

Infolinks In Text Ads