2

我创建了包含 20 个项目的 gridview。我已经使用自定义适配器来绑定这个 gridview。当我在 gridview 中滚动时,项目的索引已更改,或者有时某些项目已不可见。N 有时整个网格视图是不可见的。不知道它是怎么回事。

      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>

给我正确的解决方案。

谢谢,杰伊帕特尔

4

1 回答 1

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;
    }
于 2012-06-20T09:05:59.753 回答