Android图库 - 使用的TextView和ImageView的不起作用? [英] Android Gallery - Using TextView and ImageView doesn't work?

查看:140
本文介绍了Android图库 - 使用的TextView和ImageView的不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ImageViews画廊和我要让下他们每个人的文字出现。我的code不工作,我不知道为什么。我不是那么先进的Andr​​oid中的编程如此的编辑code反应将是真正有帮助的。我也有一个内存错误,当我从第一图像快速滚动到最后一个。

P.S:我已经尝试了许多StackOverflow的类似主题,但他们都不为我工作

 公共类ImageAdapter延伸BaseAdapter {
    INT mGalleryItemBackground;
    私人语境mContext;    私人整数[] = 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,
    };
    私人字符串[] 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};
    私人INT J = 0;    公共ImageAdapter(上下文C){
        mContext = C;
        TypedArray A = obtainStyledAttributes(R.styleable.HelloGallery);
        a.recycle();
    }    公众诠释的getCount(){
        返回mImageIds.length;
    }    公共对象的getItem(INT位置){
        返回的位置;
    }    众长getItemId(INT位置){
        返回的位置;
    }    公共查看getView(INT位置,查看ARG1,ARG2的ViewGroup){
        的LinearLayout布局=新的LinearLayout(getApplicationContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setLayoutParams(新Gallery.LayoutParams(250,200));        ImageView的I = NULL;
        // Riutilizziamo L'eventuale convertView。
        如果(ARG1 == NULL){
            I =新ImageView的(mContext);
        }其他{
            I =(ImageView的)ARG1;
        }
       i.setImageResource(mImageIds [位置]);
       i.setLayoutParams(新Gallery.LayoutParams(250,200));
       i.setScaleType(ImageView.ScaleType.FIT_XY);
       i.setBackgroundResource(mGalleryItemBackground);
        TextView的电视=新的TextView(mContext);
        tv.setTag(scoreIndex [位置]);
        tv.setText(scoreIndex [位置]);
        tv.setTextColor(0x000000处);
        tv.setTextSize(50);
        tv.setPadding(0,0,0,40);
        tv.setGravity(Gravity.CENTER);        layout.addView(ⅰ);        layout.addView(电视);        返回布局;
    }}


解决方案

要解决内存问题,请尝试重用你的一些看法,而不是每次都创建他们......第二个参数getView()是一个视图,可以进行转换。您应检查其是否非空(它可以为null),它是正确的类型(只需要您的列表包含异质的视图类型的;从这个code样品,我不认为这也适用于您)。然后,你可以清除该视图的内容CURENT并设置与图像/文本而不是膨胀的新观点。这应该节省时间/内存的显著金额列表变大。

要解决您的布局问题:不是完全通过创建code中的观点,定义的LinearLayout在其自己的布局XML文件库项目。然后,您可以使用该视图充气膨胀的。您应该能够使用可视化编辑器,以确保布局,你想用那种方式。

下面是我的意思样品(有两项建议):

 公共查看getView(最终诠释的位置,最后查看convertView,
            父母的ViewGroup){
       的LinearLayout viewToUse =(LinearLayout中)convertView;
        如果(viewToUse == NULL){
                   LayoutInflater吹气=(LayoutInflater)上下文
                    .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);
        (scoreIndes [位置])txtView.setText;
        imgView.setImageResource(mImageIds [位置]); }

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;


    } 

}

解决方案

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]);

 }

这篇关于Android图库 - 使用的TextView和ImageView的不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆