Reputation: 398
I'm trying to do some kind of GridView using this official example and I don't know how to add some text below the images. I tried this code for the adapter, but it is not working:
public class ImageAdapterForGridView extends BaseAdapter {
private Context mContext;
public ImageAdapterForGridView(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
TextView textView = null;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(250, 250));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
textView = new TextView(mContext);
textView.setLayoutParams(new GridView.LayoutParams(100, 100));
textView.setPadding(8,8,8,8);
} else {
imageView = (ImageView) convertView;
//textView = (TextView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
textView.setText(mThumbTexts[position]);
return imageView;
}
/* public TextView getTextView(int position, TextView convertView, ViewGroup parent){
TextView textView;
if (convertView == null) { // if it's not recycled, initialize some attributes
textView = new TextView(mContext);
textView.setLayoutParams(new GridView.LayoutParams(100, 100));
textView.setPadding(8,8,8,8);
} else {
textView = convertView;
}
textView.setText(mThumbTexts[position]);
return textView;
}*/
// references to our images
private Integer[] mThumbIds = {
R.drawable.ic_facebook, R.drawable.ic_flickr,
R.drawable.ic_gmail, R.drawable.ic_linkedin,
R.drawable.ic_picasa, R.drawable.ic_myspace,
R.drawable.ic_rss, R.drawable.ic_skype,
R.drawable.ic_tuenti, R.drawable.ic_twitter,
R.drawable.ic_windowslive, R.drawable.ic_vimeo,
R.drawable.ic_yahoo, R.drawable.otro_32,
};
private String[] mThumbTexts = {
"Facebook", "Flickr", "Gmail", "LinkedIn", "Picasa", "Myspace", "RSS", "Skype", "Tuenti",
"Twitter", "WindowsLive", "Vimeo", "Yahoo", "Otros"
};
}
Do you know how could I add some TextView the same way I do add the imageView??
Thank you so much for your help :)
Upvotes: 0
Views: 1179
Reputation: 5220
in your getView
method you are returning your imageView
try adding yout ImageView
and TextView
to a LinearLayout
and return that layout.
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
...
layout.addView(imageView);
layout.addView(textView);
...
return layout;
Upvotes: 1
Reputation: 4123
If you just want an image with a label, you can go for a compound drawable. In Android a textview can have an image associated with it. You can achieve this through the drawableTop/Left/Right/Bottom in XML, or through setCompoundDrawables programmatically. Check my answer to this question on more details in how you can do this: https://stackoverflow.com/a/26959558/1003528 with a pratical example for a gridview.
Upvotes: 0