Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
200 views
Welcome To Ask or Share your Answers For Others

1 Answer

To address your memory issue, try reusing some of your views rather than creating them each time... the 2nd argument to getView() is a view that can be converted. You should check that it is non-null (it can be null) and that it's the right type (only needed if your list contains a heterogeneous set of view types; from this code sample I don't think this applies to you). Then you can clear out the curent contents of that view and set the image/text in those rather than inflating a new view. This should save a significant amount of time/memory as your list gets large.

To address your layout issues: rather than totally creating the view via code, define the LinearLayout for a gallery item in its own layout xml file. You can then use the view inflator to inflate that. You should be able to use the visual editor to make sure the layout is as you want it that way.

Here's a sample of what I mean (with both suggestions):

public View getView(final int position, final View convertView,
            ViewGroup parent) {
       LinearLayout viewToUse = (LinearLayout) convertView;
        if (viewToUse == null) {
                   LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                   viewToUse.inflate(R.layout.yourlayoutname);      
                }
        ImageView imgView = (ImageView)viewToUse.findViewById(R.id.imageviewid);
        TextView txtView = (TextView)viewToUse.findViewById(R.id.textviewid);
        txtView.setText(scoreIndes[position]);
        imgView.setImageResource(mImageIds[position]);

 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...