How to retrieve image in a gridview from fragment?

I am just retrieving image from the drawable and using a base adapter but the fragment doesn't show any image. following is my code... is there any problem or logical error... Can someone tell me!!! Here is fragment code ..

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_all_question, container, false); GridView gridview = (GridView) view.findViewById(R.id.gridview); imageAdapter = new ImageAdapter(mContext); gridview.setAdapter(imageAdapter);

    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getActivity(), "" + position,
                    Toast.LENGTH_SHORT).show();
        }
    });

    return view;
}

and here is the adapter code

public class ImageAdapter extends BaseAdapter{

private Context mContext;

public ImageAdapter(Context mContext) {
    this.mContext = mContext;
}



@Override
public int getCount() {
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes

        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}
// references to our images
private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
};

}


尝试改变你的适配器中的getItem(..)方法,看看它是怎么回事。

@Override
    public Object getItem(int position) {
        return position;
    }
链接地址: http://www.djcxy.com/p/81414.html

上一篇: “无法添加小部件”在Fragment中

下一篇: 如何从片段中检索gridview中的图像?