I have a Gallery with ImageViews and I want to make a text appear under each of them. My code doesn't work and I don't know why. I am not so advanced in Android programming so an edited Code response will be truly helpful. I also have a memory error when I fast scroll from the first image to the last one.
P.S: I have tried many stackoverflow similar topics but none of them has worked for me.
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.text,
R.drawable.numbers,
R.drawable.random,
R.drawable.programming,
R.drawable.left_hand,
R.drawable.right_hand,
R.drawable.maths,
R.drawable.capitals,
R.drawable.emoticons,
R.drawable.spaces,
R.drawable.fast,
R.drawable.ultimate,
};
private String[] scoreIndex={"1.56","2.56","2.56","2.56","2.56","2.56","2.56","2.56","2.56","2.56","2.56","2.56"};
private int j=0;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View arg1, ViewGroup arg2) {
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new Gallery.LayoutParams(250, 200));
ImageView i = null;
// Riutilizziamo l'eventuale convertView.
if (arg1 == null) {
i = new ImageView(mContext);
} else {
i = (ImageView) arg1;
}
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(250, 200));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
TextView tv=new TextView(mContext);
tv.setTag(scoreIndex[position]);
tv.setText(scoreIndex[position]);
tv.setTextColor(0x000000);
tv.setTextSize(50);
tv.setPadding(0, 0, 0, 40);
tv.setGravity(Gravity.CENTER);
layout.addView(i);
layout.addView(tv);
return layout;
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…