2015-12-22 73 views
0

我試圖從谷歌GridView示例中編寫一個簡單的GridView適配器;這裏是我的getView函數。爲GridView編譯android例子時出錯

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

     if (convertView == null) { 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridLayout.LayoutParams(120,120)); 
      imageView.setScaleType(ImageView.ScaleType.CENTER); 
      imageView.setPadding(2, 2, 2, 2); 
     } 
     else 
     { 
      imageView = (ImageView) convertView; 
     } 
     imageView.setImageResource(mThumbIds[position]); 
     return imageView; 
    } 

,但我得到一些像這樣的錯誤此行imageView.setLayoutParams(new GridLayout.LayoutParams(120,120));

Error:(39, 39) error: no suitable constructor found for LayoutParams(int,int) 
constructor android.widget.GridLayout.LayoutParams.android.widget.GridLayout.LayoutParam (Context,AttributeSet) is not applicable 
(actual argument int cannot be converted to Context by method invocation conversion) 
constructor android.widget.GridLayout.LayoutParams.android.widget.GridLayout.LayoutParams(android.widget.GridLayout.LayoutParams) is not applicable 
(actual and formal argument lists differ in length) 
constructor android.widget.GridLayout.LayoutParams.android.widget.GridLayout.LayoutParams(MarginLayoutParams) is not applicable 
(actual and formal argument lists differ in length) 
constructor android.widget.GridLayout.LayoutParams.android.widget.GridLayout.LayoutParams(android.view.ViewGroup.LayoutParams) is not applicable 
(actual and formal argument lists differ in length) 
constructor android.widget.GridLayout.LayoutParams.android.widget.GridLayout.LayoutParams() is not applicable 
(actual and formal argument lists differ in length) 
constructor android.widget.GridLayout.LayoutParams.android.widget.GridLayout.LayoutParams(Spec,Spec) is not applicable 
(actual argument int cannot be converted to Spec by method invocation conversion) 

有沒有什麼解決辦法嗎?

回答

1

檢查下面的代碼:

import android.widget.GridLayout.LayoutParams;//import this statement 

int dp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 120, getResources().getDisplayMetrics()); 
imageView.setLayoutParams(new LayoutParams(dp, dp));//120dp 
-1

嘗試刪除imageView.setLayoutParams(new GridLayout.LayoutParams(120,120));