Android 人口列表视图

发布于 2024-12-09 12:54:02 字数 291 浏览 1 评论 0原文

我有一个关于在 android 中填充列表视图的非常基本的问题。现在,如果我想这样做,我可以使用 SimpleCursorAdapter,它工作得很好。但由于我的程序变得越来越复杂,我想在显示数据之前对其进行操作(您知道,也格式化字符串)我面临的问题是,在从数据库请求到 ListElements 的过程中,并没有真正的点,我可以在其中操纵数据。由于每个 ListElement 有四个 TextView,因此据我所知,ArrayAdapter 是不可能的。因为我对 Android 很陌生,所以我什至不知道要谷歌搜索什么。

非常感谢您的帮助, 丹尼尔

I have a very basic question about populating listviews in android. Right now, if I wanna do so, I use a SimpleCursorAdapter and it works very well. But since my Programm is getting more sophisticated I want manipulate the data, before it is displayed (You know, formating the Strings a.s.o.) The problem I face is, that on the way from the database request to the ListElements, there is not really an point, where I can manipulate the data. Since I have four TextViews per ListElement, the ArrayAdapter is, as far as I know, not possible. Since I am very new to Android I don't even know what to google for.

Thanks a lot for your help,
Daniel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

桃气十足 2024-12-16 12:54:02

执行此操作的常见方法是在适配器的 getView 中。你可以这样做:

class ContactAdapter extends ArrayAdapter<String> {

        private LayoutInflater mInflater;

        public ContactAdapter(Context context, int textViewResourceId, List<String> items) {
            super(context, textViewResourceId, items);
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;
            if (convertView == null) {

                convertView = mInflater.inflate(R.layout.rowcontact, null);
                holder = new ViewHolder();
                holder.lastname = (TextView) convertView.findViewById(R.id.rowTop);
                holder.firstname = (TextView) convertView.findViewById(R.id.rowBottom);
                holder.iconContact = (ImageView) convertView.findViewById(R.id.iconContact);
                convertView.setTag(holder);
            } else {

                holder = (ViewHolder) convertView.getTag();
            }
            String item = getItem(position);
            int i = (int) (Math.random() * 5 + 1);

            switch (i) {
            ...
            }
            if (item != null) {
                holder.lastname.setText(item.split("-")[0]);
                holder.firstname.setText(item.split("-")[1]);
                holder.iconContact.setImageBitmap(bm);
            }
            return convertView;
        }
    }

    static class ViewHolder {
        TextView lastname;
        TextView firstname;
        ImageView iconContact;
    }
}

你可以找到完整的代码示例 那里。使用支架示例,您可以设置 4 个视图并更新它们。

The common way of doing so is in the getView of your adapter. You could do something like this :

class ContactAdapter extends ArrayAdapter<String> {

        private LayoutInflater mInflater;

        public ContactAdapter(Context context, int textViewResourceId, List<String> items) {
            super(context, textViewResourceId, items);
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder;
            if (convertView == null) {

                convertView = mInflater.inflate(R.layout.rowcontact, null);
                holder = new ViewHolder();
                holder.lastname = (TextView) convertView.findViewById(R.id.rowTop);
                holder.firstname = (TextView) convertView.findViewById(R.id.rowBottom);
                holder.iconContact = (ImageView) convertView.findViewById(R.id.iconContact);
                convertView.setTag(holder);
            } else {

                holder = (ViewHolder) convertView.getTag();
            }
            String item = getItem(position);
            int i = (int) (Math.random() * 5 + 1);

            switch (i) {
            ...
            }
            if (item != null) {
                holder.lastname.setText(item.split("-")[0]);
                holder.firstname.setText(item.split("-")[1]);
                holder.iconContact.setImageBitmap(bm);
            }
            return convertView;
        }
    }

    static class ViewHolder {
        TextView lastname;
        TextView firstname;
        ImageView iconContact;
    }
}

You can find full code example there. With the holder example, you can set up your 4 views and update them.

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