Android网格视图数据绑定
我有自己的类arraylist,我需要绑定该列表,并只向用户显示一些字段。我尝试过通过扩展BaseAdapter类来创建自己的适配器类。但我只显示了一个字段(firstname),我需要显示更多。这是我的适配器类Android网格视图数据绑定,android,data-binding,gridview,baseadapter,Android,Data Binding,Gridview,Baseadapter,我有自己的类arraylist,我需要绑定该列表,并只向用户显示一些字段。我尝试过通过扩展BaseAdapter类来创建自己的适配器类。但我只显示了一个字段(firstname),我需要显示更多。这是我的适配器类 private class MyGriAdapter extends BaseAdapter{ ArrayList<Doctor> data; public MyGriAdapter(ArrayList<Doctor> dat
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;
}
}
最简单的方法是为项目使用xml布局,并使用适配器将该项目充气,然后更改其内容:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
// Create new view
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.item, parent, false);
}
// Set information
TextView titleView = (TextView)convertView.findViewById(R.id.title);
titleView.setText(data.get(position).title);
TextView otherView = (TextView)convertView.findViewById(R.id.other);
titleView.setText(data.get(position).other);
return convertView;
}
当convertView为null时,您将创建一个新视图,然后在任何情况下都将重用该视图。xml布局可以是这样的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/title" android:layout_width="fill_parent"
android:layout_height="match_parent" />
<TextView android:id="@+id/other" android:layout_width="fill_parent"
android:layout_height="match_parent" />
</LinearLayout>
这可以通过在充气机上保留一个参考来进一步优化,并使用视图的标记系统来跟踪内部视图,但这有点脱离主题。。。如果您想了解更多信息,我建议Google IO会议不要使用应用程序上下文创建视图。这会给你的主题设置和其他方面带来问题。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/title" android:layout_width="fill_parent"
android:layout_height="match_parent" />
<TextView android:id="@+id/other" android:layout_width="fill_parent"
android:layout_height="match_parent" />
</LinearLayout>