Flynn
Flynn

Reputation: 6181

Android: Fill Gallery with TextViews

I am trying to make a Gallery that uses TextViews instead of ImageViews, but I cannot seem to make a working adapter. How can I use an adapter to fill the Gallery with TextViews?

Upvotes: 0

Views: 278

Answers (1)

you786
you786

Reputation: 3550

Just extend BaseAdapter like so:

public class GalleryTextAdapter extends BaseAdapter{

  Context context;

  GalleryTextAdapter(Context context){
    this.context = context;        
  }

  public View getView(int position, View convertView, ViewGroup parent){
    LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = li.inflate(R.layout.mylayout, null);

    TextView tv = (TextView) convertView.findViewById(R.id.mylayout_text);
    tv.setText("some text");
    return convertView;
  }

and assign it to the gallery using:

Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new GalleryTextAdapter(this));

Upvotes: 2

Related Questions