Android 网格视图数据绑定
我有自己的类 arraylist,我需要绑定该列表并仅向用户显示一些字段。我通过扩展 BaseAdapter 类来创建自己的适配器类来尝试此操作。但我只显示了一个字段(名字),我需要显示更多。这是我的适配器类,
private class MyGriAdapter extends BaseAdapter{
ArrayList<Doctor> data;
public MyGriAdapter(ArrayList<Doctor> data){
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView vv = new TextView(getApplicationContext());
vv.setTextColor(Color.BLACK);
vv.setText(data.get(position).firstname);
return vv;
}
@Override
public Object getItem(int arg0) {
return data.get(arg0);
}
@Override
public long getItemId(int position) {
return position;
}
}
我的按钮单击事件正在绑定数据(doctorResultList 是 Doctor 类型数组列表),
GridView grid_main = (GridView)findViewById(R.id.GridView01);
MyGriAdapter grdAdapter = new MyGriAdapter(doctorResultList);
grid_main.setAdapter(grdAdapter);
I have my own class arraylist and I need to bind that list and show only some fields to user. I have tried this by creating my own adapter class by extending BaseAdapter class. But I just showed only one field(firstname) I need to show more. here is my adapter class,
private class MyGriAdapter extends BaseAdapter{
ArrayList<Doctor> data;
public MyGriAdapter(ArrayList<Doctor> data){
this.data = data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView vv = new TextView(getApplicationContext());
vv.setTextColor(Color.BLACK);
vv.setText(data.get(position).firstname);
return vv;
}
@Override
public Object getItem(int arg0) {
return data.get(arg0);
}
@Override
public long getItemId(int position) {
return position;
}
}
and my button click event I'm binding data (doctorResultList is the Doctor type arraylist),
GridView grid_main = (GridView)findViewById(R.id.GridView01);
MyGriAdapter grdAdapter = new MyGriAdapter(doctorResultList);
grid_main.setAdapter(grdAdapter);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的方法是为您的项目使用 xml 布局,并使用适配器来扩充该项目,然后更改其内容:
当 ConvertView 为 null 时,您创建一个新的,然后在每种情况下重用该视图。 xml 布局可能是这样的:
这可以通过在充气器上保留引用并使用视图的标签系统来跟踪内部视图来进一步优化,但这有点偏离主题...如果您想了解更多信息关于它,我推荐 Google IO 会议 World of列表视图
The easiest way is to use an xml layout for your items, and use the adapter to inflate that item, and then change it's contents :
When convertView is null, you create a new one, and then in every case you reuse that view. The xml layout could be something like this :
This can be further optimized by keeping a reference on the inflater, and using the view's tag system to keep track of inner views, but that's a bit off topic... If you want to learn more about it, I recommend the Google IO conference World of ListView