Android 网格视图数据绑定

发布于 2024-11-18 14:59:08 字数 1169 浏览 2 评论 0原文

我有自己的类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待天淡蓝洁白时 2024-11-25 14:59:08

最简单的方法是为您的项目使用 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 会议 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 :

    @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;
    }

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 :

<?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>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文