列表适配器设置 - 如何不重复列表项布局资源? [英] List Adapter setting - How can I not to repeat the list item layout resource?

查看:21
本文介绍了列表适配器设置 - 如何不重复列表项布局资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 ArrayAdapter 创建了一个 ListView.有用.但是我不得不将项目布局的资源 ID 放置两次:在适配器定义中和在 getView 中,在 View 参数为空的情况下.

I have made a ListView with an ArrayAdapter. It works. But I had to put the resource id for the item layout twice: in the adapter definition and in the getView, in the case when the View parameter is null.

// --------------------------------------------here is the FIRST use
lvShows.setAdapter(new ArrayAdapter<TvShow>(this, R.layout.show_row, allKnownShows) {
    @Override
    public View getView(int position, final View rowView, ViewGroup parent) {
        LinearLayout showView;
        if (rowView == null) {

            // --------------------------------- here is the SECOND use
            showView = (LinearLayout) inflater.inflate(R.layout.show_row, parent, false);
        }
        else {
            showView = (LinearLayout) rowView;
        }
        ((TextView) showView.getChildAt(0)).setText(time));
        ((TextView) showView.getChildAt(1)).setText(name);
        return showView;
    }
});

这样的作风当然令人作呕.能否请您指教,我的理解有误,我如何才能仅使用一次资源?

Such style is disgustful, of course. Could you kindly advise, what am I understanding wrong and how can I use the resource only once?

如果创建一个新的 ArrayAdapter 我正在设置布局 ID,它应该知道它并以某种方式使用.我怎么能达到呢?或者更好的是我希望适配器自动创建项目视图.再次 - 我该如何使用它?

If creating a new ArrayAdapter I am setting the layout id, it should know it and use somehow. How could I reach it? Or better I would expect the Adapter to create item views automatically. Again - how can I use it?

在创建新资源时,ArrayAdapter 如何处理我们提供给它的资源?它的所有构造函数都获取 item 资源,我们管理此资源并手动"对其进行膨胀.这不是有效的方法.

What ArrayAdapter does with that resource we feed to it when creating a new one? All its constructors take the item resource and we manage this resource and inflate it "by hand". It is not the effective way.

推荐答案

我已经查看了 ArrayAdapter 的源代码,它已经完成了有关创建视图的所有工作:

I had looked into the source code of the ArrayAdapter, and it already does all this stuff about view creation:

// citation from public class ArrayAdapter<T>
private View createViewFromResource(int position, View convertView, ViewGroup parent,
        int resource) {
    View view;
    TextView text;
    if (convertView == null) {
        view = mInflater.inflate(resource, parent, false);
    } else {
        view = convertView;
    }

所以,我们应该简单地使用它:

So, we should simply use it:

lvShows.setAdapter(new ArrayAdapter<TvShow>(this, 
                                            R.layout.show_row, 
                                            R.id.nameField, 
                                            allKnownShows){
public View getView(int position, final View convertView, ViewGroup parent) {
    LinearLayout showView = (LinearLayout) super.getView(position,  convertView, parent);

    setAnythingForItem(showView);

    return showView;
}

注意:我们改变了构造函数!

Attention: we have changed the constructor!

ArrayAdapter 允许使用 no-TextView 项目布局.但是如果它不是 TextView 本身,它应该有一个,并且这个非常内部的 TextView 字段的 id 应该作为第三个参数提供给构造函数.ArrayAdapter 需要设置它,如果连接到它的数组具有字符串元素,则在那里写入字符串.

ArrayAdapter allows to use no-TextView item layout. But if it is not TextView itself, it should have one and the id of this very inner TextView field should be given to the constructor as the third parameter. ArrayAdapter needs it to be set, to write there Strings if the array connected to it has String elements.

它总是想要一个 TextView,即使它真的不需要它,如果数组由对象组成,而不是字符串.否则,它会检查项目布局是否为 TextView,如果不是,则抛出错误.

It wants a TextView always, even if it doesn't need it really, if the array consists of Objects, not Strings. Otherway it checks the item layout for being a TextView and if it is not, throws an error.

这篇关于列表适配器设置 - 如何不重复列表项布局资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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