我有自己的类arraylist,我需要绑定这个列表并只向user显示一些字段。我尝试过通过扩展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是博士类型数组列表),
GridView grid_main = (GridView)findViewById(R.id.GridView01);
MyGriAdapter grdAdapter = new MyGriAdapter(doctorResultList);
grid_main.setAdapter(grdAdapter);发布于 2011-07-06 05:24:32
最简单的方法是对项目使用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>这可以通过在扩张器上保持引用,并使用视图的标记系统来跟踪内部视图来进一步优化,但这有点离题……如果您想了解更多信息,我推荐使用谷歌IO会议World of ListView
https://stackoverflow.com/questions/6588947
复制相似问题