Adapter

标题就随便起起就好了

整体效果如图

人员就是我们宿舍的人

布局分为两个布局

主布局和次要布局

    <TextView
android:id="@+id/title1"
style="@style/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="王千" />

<TextView
android:id="@+id/title2"
style="@style/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="21" />

<TextView
android:id="@+id/title3"
style="@style/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="邮箱:wangqian@qq.com" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/dizhi"
style="@style/text"
android:id="@+id/title4"/>
</LinearLayout>
代码如下
还有一部分整体布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/show"
android:orientation="vertical">


</LinearLayout>
</LinearLayout>
这样我们布局的部分就弄好了
然后我们要定义方法变量
将所有的getter和setter方法写入
添加对象的方法
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_adapter, container, false);
List<ClassInfo> datas = new ArrayList<>();
datas.add(new ClassInfo("姓名:王千","年龄:21","邮箱:wangqian@qq.com","地址:南京市"));
datas.add(new ClassInfo("姓名:徐梦阳","年龄:21","邮箱:xumengyang@qq.com","地址:南京市"));
datas.add(new ClassInfo("姓名:丁金涛","年龄:20","邮箱:dingjintao@qq.com","地址:南京市"));
datas.add(new ClassInfo("姓名:刘鹏","年龄:20","邮箱:liupeng@qq.com","地址:南京市"));
datas.add(new ClassInfo("姓名:姚佳伟","年龄:21","邮箱:yaojiawei@qq.com","地址:南京市"));
datas.add(new ClassInfo("姓名:陈怀斌","年龄:30","邮箱:chenhuaibin@qq.com","地址:南京市"));

final CustomGridViewAdapter adapter = new CustomGridViewAdapter(getActivity(),datas);

GridView gridView = (GridView)view.findViewById(R.id.fragment_gridview);
gridView.setAdapter(adapter);


return view;

}
}

这里是将数据导入
 this.datas=datas;
this.context= context;
}
@Override
public int getCount() {
return datas.size();//返回数据的长度*****
}

@Override
public Object getItem(int i) {
return datas.get(i);
}

@Override
public long getItemId(int i) {
return i;
}
//自定义Adapter,一定要重写getView()方法
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
//获取view
if(view==null){
view = LayoutInflater.from(context).inflate(R.layout.gridview_item,null);
}

TextView title1 =(TextView)view.findViewById(R.id.title1) ;
TextView title2 =(TextView)view.findViewById(R.id.title2) ;
TextView title3 =(TextView)view.findViewById(R.id.title3) ;
TextView title4 =(TextView)view.findViewById(R.id.title4) ;


ClassInfo classInfo = datas.get(i);

title1.setText(classInfo.getTitle1());
title2.setText(classInfo.getTitle2());
title3.setText(classInfo.getTitle3());
title4.setText(classInfo.getTitle4());

return view;
}
}
这里是重写getViewde方法


好吧,看写到这部分的时候,看别人的作业也不是很明白
后来程序最后做出来无法运行
后来是别的同学帮我才能做后最出来
所以最后这部分我说的不清不楚
有点懵逼
posted @ 2017-05-02 22:47  要加伟哥  阅读(154)  评论(0编辑  收藏  举报