机器人,如何从XML添加的ListView项目? [英] android, how to add ListView items from XML?

查看:149
本文介绍了机器人,如何从XML添加的ListView项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML(absolutelayout)模板的我多么希望我的ListView项目,看起来像。

I have an XML ( absolutelayout ) template, of how I want my ListView items to look like.

什么是这个项目添加到我的ListView的最佳方式?

What would be the best way to add this items to my ListView?

在,还有一件事,我如何从Java改变ListView的高度是多少?

On, and one more thing, how do I change ListView's height from java?

谢谢! :)

推荐答案

请像这样(联系人列表样本)的列表视图适配器,

Make an list view adapter like this, (Sample for a contact list)

public class ContactListAdapter extends ArrayAdapter<Contact>
{

    private int resource;

    public ContactListAdapter(Context context, int resource, List<Contact> items)
    {
        super(context, resource, items);

        this.resource = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;
        LinearLayout contactListView;

        if (convertView == null)
        {
            contactListView = new LinearLayout(getContext());
            String inflater = Context.LAYOUT_INFLATER_SERVICE;
            LayoutInflater layoutInflater;
            layoutInflater = (LayoutInflater) getContext().getSystemService(inflater);
            layoutInflater.inflate(resource, contactListView, true);

            holder = new ViewHolder();

            holder.textViewName = (TextView) contactListView.findViewById(R.id.name);
            holder.textViewAddress = (TextView) contactListView.findViewById(R.id.address);

            contactListView.setTag(holder);
        }
        else
        {
            contactListView= (LinearLayout) convertView;

            holder = (ViewHolder) contactListView.getTag();
        }

        Contact item = getItem(position);

        holder.textViewName.setText(item.getName());
        holder.textViewAddress.setText(item.getAddress());

        return contactListView;
    }

    protected static class ViewHolder
    {
        TextView textViewName;
        TextView textViewAddress;
    }
}

设置该适配器将列表视图。通过在XML布局到该适配器的资源ID。它将从XML紧缩的观点,并将其添加到列表视图。

Set this adapter to your listview. Pass in the resource id of your xml layout to this adapter. It would deflate the view from the xml and add it to the listview.

您还可以调整列表视图项目的高度在上面的 getView()适配器的方法。使用的LayoutParams ofcourse。

You can also adjust the heights of the listview items in the above getView() method of the adapter. Using LayoutParams ofcourse.

这篇关于机器人,如何从XML添加的ListView项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆