GridView imageAdapter不起作用

时间:2012-12-11 11:18:39

标签: android

我在片段

中有gridView
     @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        //
        Bundle savedInstanceState) {




    if (container == null) {
        Log.e("Null container", "Null");

        return null;
    }
    Log.e("fragment1", "fragment1 reached!");
    View view =  inflater.inflate(R.layout.menu1_fragment, container, false);
    GridView gridView = (GridView)view.findViewById(R.id.gridview1);
    gridView.setAdapter(new ImageAdapter(this.getActivity()));


    Log.e("fragment1", "fragment1 reached!");

    return (GridView)inflater.inflate(R.layout.menu1_fragment, container, false);
}

但是适配器不起作用,它没有达到重写的getView方法。

构造

public ImageAdapter(Context c) {
    mContext = c;
    Log.e("ImageAdapter", "constructor");
}

GetView方法:

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    Log.e("ImageAdapter", "dsfdsf");
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        if (DefinedValues.width/3 < DefinedValues.height/4)
        imageView.setLayoutParams(new GridView.LayoutParams
                (DefinedValues.height/4, DefinedValues.height/4));
        else
            imageView.setLayoutParams(new GridView.LayoutParams
                    (DefinedValues.width/3-5, DefinedValues.width/3-5));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

2 个答案:

答案 0 :(得分:0)

onCreateView中,您正在对xml进行两次充气,正在编辑一个,然后您将返回一个未经编辑的x ..

public View onCreateView(LayoutInflater inflater, ViewGroup container,
    //
    Bundle savedInstanceState) {




if (container == null) {
    Log.e("Null container", "Null");

    return null;
}
Log.e("fragment1", "fragment1 reached!");
View view =  inflater.inflate(R.layout.menu1_fragment, container, false);
GridView gridView = (GridView)view.findViewById(R.id.gridview1);
gridView.setAdapter(new ImageAdapter(this.getActivity()));


Log.e("fragment1", "fragment1 reached!");

return view;
}

答案 1 :(得分:-1)

我认为你需要换成

public ImageAdapter(Context c) {
    this.mContext = c;
    Log.e("ImageAdapter", "constructor");
}

检查一下。