如何从片段中检索gridview中的图像?
我只是从drawable中检索图像,并使用基础适配器,但片段不显示任何图像。 以下是我的代码...是否有任何问题或逻辑错误...有人可以告诉我! 这里是片段代码..
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){//膨胀这个片段的布局View view = inflater.inflate(R.layout.fragment_all_question,container,false); GridView gridview =(GridView)view.findViewById(R.id.gridview); imageAdapter =新的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;
}
这里是适配器代码
公共类ImageAdapter扩展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/81413.html