gridview Adapter怎么写

rocksent 2013-09-03 09:33:45
重写了BaseAdapter

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView imageView;
if(arg1 == null){
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(45,45));
imageView.setAdjustViewBounds(false);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}else{
imageView = (ImageView) arg1;
}
Resources res=getResources();
int i=res.getIdentifier(arr[arg0],"drawable",getPackageName());
imageView.setImageResource(R.drawable.default_pic);
return imageView;
}


以上正确的
现在想用布局文件来完成,我把它修改如下

public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
View v ;
if(arg1 == null){
v = (View) findViewById(R.layout.items_img);
ImageView iv1 = (ImageView) v.findViewById(R.id.ItemImage1);
ImageView iv2 = (ImageView) v.findViewById(R.id.ItemImage2);
iv1.setImageResource(R.drawable.default_pic);
iv2.setImageResource(R.drawable.checkbox1);
}else{
v = (ImageView) arg1;
}
return v;
}

items_img.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/ItemImage1"
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_centerHorizontal="true"/>
<ImageView
android:id="@+id/ItemImage2"
android:layout_width="40dip"
android:layout_height="40dip"
android:src="@drawable/checkbox1"
/>
</RelativeLayout>

运行报错,说我这行空指针,不可能啊!
ImageView iv1 = (ImageView) v.findViewById(R.id.ItemImage1);
...全文
108 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
悲催的码农 2013-09-04
  • 打赏
  • 举报
回复
1楼正解,或者直接获取系统服务 LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); ViewGroup menuView = (ViewGroup) mLayoutInflater.inflate( R.layout.gridview_pop, null, true); menuGrid = (GridView) menuView.findViewById(R.id.gridview); just like this
聚集美丽 2013-09-04
  • 打赏
  • 举报
回复
要先手动 加载xml文件,然后再find,随便找一个demo 就可以看到了
聚集美丽 2013-09-04
  • 打赏
  • 举报
回复
要先手动 加载xml文件,然后再find,随便找一个demo 就可以看到了
b87936260 2013-09-04
  • 打赏
  • 举报
回复
v = (View) findViewById(R.layout.items_img);这里错了,v不是find出来的,是LayoutInflator加载出来的 v = LayoutInflater.from(mContext).inflate(R.layout.home_grid_item, null);
hotkarriver 2013-09-04
  • 打赏
  • 举报
回复
1楼正解inflate

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧