使用GridView控件与ImageViews和TextViews问题 [英] Issue using GridView with ImageViews and TextViews

查看:134
本文介绍了使用GridView控件与ImageViews和TextViews问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用GridView与ImageView的每个单元格内一个TextView。所以我创建的单元布局,网格布局,imageAdapter和主要活动,当然,但我不断收到以下问题:

I'm trying to use a GridView with an ImageView and a TextView inside each cell. So i created the cell layout, the grid layout, the imageAdapter and the main activity, of course, but i keep getting the following problem:

当我尝试这在模拟器上,最初的图像和字幕显示正确,但只要我向下滚动,几个项目开始陷入困境,并不断变化,甚至有些重复多次。

When i try this on the emulator, the initial images and captions are shown correctly but as soon as i scroll down, a few of the items start to mess up and keep changing and even duplicating some times.

我使用的是2个并行阵列(图像和字幕)。我尝试使用Log.v功能找出哪些指标和图像时getView被称为是被显示,但只有最初的那些(可无需滚动看到)的正​​确分配。

I'm using 2 parallel arrays (images and caption). I tried using the Log.v function to find out which indexes and images were being shown when the getView was called but only the initial ones (which can be seen without scrolling) are assigned correctly.

我按了一遍又一遍产生的观点来解决问题,但是这显然是不正确的做法。

I got to solve the problem by generating the view over and over but that's obviously not the right way.

下面是我使用的文件:

网格单元格:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/GridItem"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="vertical"
   android:gravity="center_horizontal">

   <ImageView android:id="@+id/grid_item_image"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
   </ImageView>

   <TextView android:id="@+id/grid_item_text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="TextView"
      android:gravity="center_horizontal"
      android:textColor="#FFFFFF">
   </TextView>

</LinearLayout>

GridView的:

GridView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:gravity="center_vertical"
        android:text="@string/txtMenu"
        />
    <GridView
        android:id="@+id/grdMenu"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:padding="10dp"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:numColumns="3" >
    </GridView>

</RelativeLayout>

GridActivity:(唯一实现的方法)

GridActivity: (The only implemented method)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.menu);

    GridView gridview = (GridView) findViewById(R.id.grdMenu);
    gridview.setAdapter(new ImageAdapter(this));

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(getApplicationContext(), "" + position, Toast.LENGTH_SHORT).show();
        }
    });
}

ImageAdapter:

ImageAdapter:

public class ImageAdapter extends BaseAdapter {

    private Context mContext;
    private Integer[] mThumbIds = {R.drawable.potencia32x32,R.drawable.detalle_cuenta32x32,R.drawable.solicitud32x32,R.drawable.agregar32x32,R.drawable.cyr_32x32,
            R.drawable.usuarios,R.drawable.cambio_med64x64,R.drawable.cobranza_ex64x64,R.drawable.convenio_pagos64x64,R.drawable.copiabf64x64,
            R.drawable.info_cliente64x64,R.drawable.mant_exp64x64,R.drawable.ordenes64x64,R.drawable.reembolsos64x64,R.drawable.seguro64x64,R.drawable.solicitudes64x64,
            R.drawable.suministro64x64

    };
    private String[] Caption = {"Consumo","Facturaciones","Solicitudes","Pagos","Cortes y Rcnx","Datos Generales","Cambios de Medidores","Cobranza Externa","Convenio Pagos","Copia Fac. o Bol.",
            "Info. Cliente","Mant. Expediente","Consulta Ordenes","Historia Reembolsos","Seguros","Solicitudes","Caracteristicas Suministro"

    };
    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }


    public View getView(int position, View convertView, ViewGroup parent) {


        View myView  = null;

        if(convertView==null)
        {
            LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            myView = li.inflate(R.layout.grdmenu_cell, null);

            TextView tv = (TextView) myView.findViewById(R.id.grid_item_text);
            Log.v("D:<",String.valueOf(Caption.length) +" y: "+ String.valueOf(position));
            tv.setText(Caption[position]);


            ImageView iv = (ImageView) myView.findViewById(R.id.grid_item_image);
            Log.v("D:<",String.valueOf(mThumbIds.length) +" y: "+ String.valueOf(position));
            iv.setImageResource(mThumbIds[position]);
        }
        else
        {
            myView = convertView;
        }

        return myView;
    }

}

这是我的第一个问题,所以如果我犯了任何错误,请告诉我。先谢谢了。

This is my very first question so if I committed any mistakes, please tell me. Thanks in advance.

推荐答案

我认为错误是在你的适配器的getView。 ConvertView不会举行到您的资源,通过findViewById()和诸如此类的东西,只有充气视图访问。试着将其更改为是这样的:

I think the error is in your getView of your Adapter. ConvertView won't hold on to your resources accessed through findViewById() and whatnot, only the inflated view. Try changing it to something like this:

public View getView(int position, View convertView, ViewGroup parent) {


    View myView  = null;

    if(convertView==null)
    {
        LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        myView = li.inflate(R.layout.grdmenu_cell, null);
    }else{
        myView = convertView;
    }


    TextView tv = (TextView) myView.findViewById(R.id.grid_item_text);
    Log.v("D:<",String.valueOf(Caption.length) +" y: "+ String.valueOf(position));
    tv.setText(Caption[position]);

    ImageView iv = (ImageView) myView.findViewById(R.id.grid_item_image);
    Log.v("D:<",String.valueOf(mThumbIds.length) +" y: "+ String.valueOf(position));
    iv.setImageResource(mThumbIds[position]);

    return myView;

}

这篇关于使用GridView控件与ImageViews和TextViews问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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