2015-12-22 6 views
0

Я пытаюсь написать простой адаптер для GridView точно из примера Google для GridView; вот моя функция getView.Ошибка при компиляции примера андроида для GridView

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

Попробуйте удалить изображениеView.setLayoutParams (новые GridLayout.LayoutParams (120,120));

Смежные вопросы