2012-06-19 70 views
2

我已經創建了20個項目的gridview。我已經使用自定義適配器來綁定這個GridView。當我在GridView中滾動時,項目的索引已經改變,或者某些項目已經隱藏。 N有時整個gridview被隱藏了。不知道它是什麼東西。 main.xml中的關於GridView Android的問題

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


    } 

內容

<?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> 

給我的是正確的解決方案。

感謝, 周杰倫帕特爾

+0

請發佈您的GridView代碼。 – Akilan

+0

hie ..這裏我已經發布了我用來綁定gridview的適配器。 – Jai

+0

layout/main.xml的內容是什麼? – monchote

回答

0

這是一個有點難以發現的錯誤沒有看到活動的代碼的其餘部分,但是我們可以從固定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; 
    }