In the constructor you get yhe context and the data you want the adapter to use.
also , you need to take care of getItem() and getCount() methods.
the getView() method calls everytime the View that uses the adapter needs to Draw itself.
you should check if the convert view is null , if it is null you should inflate your layout there an if not you need to make the rootview from the convert View. (the convert view is
the cashing mechanisem that helps the adapter to be more eficient , the adapter recycles View objects instead of creating new views Object , if you don't add this check your Gallery will not run smoothly)
here is an example of a base adapter that display names of leagues that it recived as an
Array:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.LinearLayout;
import android.widget.TextView;
public class GalleryAdapter extends BaseAdapter {
Context context;
private String[] leaguesName;
public GalleryAdapter(Context context,String[] leaguesName )
{
this.context=context;
this.leaguesName=leaguesName;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return leaguesName.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return leaguesName[arg0];
}
@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 root;
counter++;
if(convertView==null)
{
LayoutInflater lif = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
root=lif.inflate(R.layout.gallery_text_view, null);
}
else
root=convertView;
TextView tv =(TextView) root.findViewById(R.id.league_name_tv);
tv.setText(leaguesName[position]);
return root;
}
}