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

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

问题描述

我与一个ArrayAdapter一个ListView。有用。但我不得不把该项目的布局的资源ID的两倍:在适配器定义,并在getView,在的情况下,当在视图参数为空。

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确实与创建一个新的,当我们喂给它的资源是什么?它的所有构造函数取该项目的资源和我们管理这个资源和膨胀为手动。它不是有效的方法。

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源$ C ​​$ C,它已经这样做了大约视图创建所有这些东西:

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允许使用无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始终,即使它并不需要真的,如果数组包含对象,而不是字符串。 Otherway它检查项目的布局为是一个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天全站免登陆