有关GridView Android的问题

时间:2012-06-19 10:05:06

标签: android gridview

我创建了包含20个项目的gridview。我使用自定义适配器来绑定此gridview。当我在gridview中滚动时,项目的索引已经改变,或者有时某些项目是不可见的。 N有时候整个gridview都是看不见的。不知道它有什么用途。

      public class MyAdapter extends BaseAdapter {

        final int NumberOfItem = 90;
        private Bitmap[] bitmap = new Bitmap[NumberOfItem];

        View grid;

        private Context context;
        private LayoutInflater layoutInflater;

        MyAdapter(Context c){
         context = c;
         layoutInflater = LayoutInflater.from(context);

         //init dummy bitmap,
         //using R.drawable.icon for all items
       /*  for(int i = 1; i < NumberOfItem; i++){
          bitmap[i] = BitmapFactory.decodeResource(context.getResources(), R.drawable.image1);
         }*/
        }

        @Override
        public int getCount() {
         // TODO Auto-generated method stub
         return imageIDs.length;
        }

        @Override
        public Object getItem(int position) {
         // TODO Auto-generated method stub
         return position;
        }

        @Override
        public long getItemId(int position) {
         // TODO Auto-generated method stub
         return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
         // TODO Auto-generated method stub


         if(convertView==null){
          grid = new View(context);
          grid = layoutInflater.inflate(R.layout.main, null);

          ImageView imageView = (ImageView)grid.findViewById(R.id.image);
          imageView.setBackgroundResource(imageIDs[position]);


          TextView textView = (TextView)grid.findViewById(R.id.text);
          textView.setText(names[position]);


         }else{

             grid = (View)convertView;


         }


         return grid;
        }


        Integer[] imageIDs = {

                R.drawable.attorneys_180, R.drawable.auto_repair_180, R.drawable.coffee_180, R.drawable.gas_stations_180,
                R.drawable.grocery_180, R.drawable.hotels_180, R.drawable.locksmith_180, R.drawable.nightclubs_180,
                R.drawable.plumbers_180

        };

        String[] names = {"Attorneys","Auto Repair","Coffee","Gas Stations","Grocery","Hotels","Locksmith","NightClubs","Plumbers"};


    }

main.xml的内容

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

<ImageView
android:id="@+id/image"
android:layout_width="67dp"
android:layout_height="67dp" android:background="@drawable/ic_launcher"/>

<TextView
 android:id="@+id/text"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_alignRight="@+id/image"
 android:layout_below="@+id/image"
 android:text="gsdfgsd"
 android:textColor="#000000" android:gravity="center"/>

 </RelativeLayout>

给我正确的解决方案。

谢谢, 杰伊帕特尔

1 个答案:

答案 0 :(得分:0)

在没有看到Activity的其余代码的情况下发现错误有点困难,但我们可以从修复getView方法开始(我在我所做的更改旁边添加了一些注释) ):

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    {
         if(convertView == null){
             //grid = new View(context); You don't need this because next line creates it for you
             grid = layoutInflater.inflate(R.layout.main, null);
         }
         else
         {
             grid = convertView;
         }

         // You always need to set the right image and text, even when you have a non-null convertView

         ImageView imageView = (ImageView)grid.findViewById(R.id.image);
         imageView.setBackgroundResource(imageIDs[position]);

         TextView textView = (TextView)grid.findViewById(R.id.text);
         textView.setText(names[position]);

         return grid;
    }