简体   繁体   English

关于GridView Android的问题

[英]Issue Regarding GridView Android

I have created gridview with 20 items. 我创建了20个项目的gridview。 I have used custom adapter to bind this gridview. 我使用了自定义适配器来绑定此gridview。 When i scroll in gridview the index of the items have been change, or sometime some of the items have been invisible. 当我在gridview中滚动时,项目的索引已更改,或者有时某些项目不可见。 N some times whole gridview has been invisible. 有时候,整个gridview是不可见的。 Not know what's worng with it. 不知道这有什么用。

      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"};


    }

content of main.xml 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>

Give me right solution for it. 给我正确的解决方案。

Thanks, Jay Patel 谢谢,杰伊·帕特尔

It's a bit difficult to spot the error without seeing the rest of the code of the Activity, but we can start from fixing the getView method (I've added some comments next to the changes I've made): 在不查看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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM