简体   繁体   English

无法使用清单 <String> 在实现CustomListAdapter android时

[英]Can't use List<String> when implementing CustomListAdapter android

I have a collection of animal objects that are in a polymorphic hierarchy (ie Animal > Reptile > Snake). 我有一组处于多态层次结构中的动物对象(即动物>爬行动物>蛇)。 I want to create a List view where each item in the list is inflated with a different layout depending on the object type (eg one item layout for reptiles, one for cats, one for primates, etc). 我想创建一个列表视图,其中列表中的每个项目根据对象类型使用不同的布局进行膨胀(例如,爬行动物的一项布局,猫的一项布局,灵长类的一项布局等)。 This activity is just the categories, not the actual list of animals. 此活动只是类别,而不是实际的动物清单。 So I just want to create a "group" item. 所以我只想创建一个“组”项。

To do this, I'm trying to just use a list of all of the possible types of animals (again, reptile, primate, cat, etc). 为此,我试图仅使用所有可能类型的动物(再次,爬行动物,灵长类,猫等)的列表。 in the form of a List collection. 以列表集合的形式。 How can I use a CustomListAdapter to accomplish this? 如何使用CustomListAdapter完成此操作? It doesn't seem to want to let me give the adapter a List of Strings, it wants a List of Objects. 它似乎不想让我给适配器一个字符串列表,它想要一个对象列表。

Just check the type by using switch or if statement and inflate accordingly: 只需使用switch或if语句检查类型并相应地充气:

Here is a sample code: 这是一个示例代码:

  @Override 
    public View getView()
    {
        View row = null;
            if (activity.equalsIgnoreCase("Ni_activity") || activity.equalsIgnoreCase("Current_NI_Activity") || activity.equalsIgnoreCase("Ni_Test_Activity")) {
                row = inflater.inflate(R.layout.ni_spinner_row, parent, false);
            } else if (activity.equalsIgnoreCase("RFTestActivity")) {
                row = inflater.inflate(R.layout.spinner_row, parent, false);
            } else if (activity.equalsIgnoreCase("SiteInfoFragment") || activity.equalsIgnoreCase("LayerInfoFragment")) {
                row = inflater.inflate(R.layout.site_frag_spinner, parent, false);
            }

            TextView tvCategory = (TextView) row.findViewById(R.id.tvCategory);
            tvCategory.setTypeface(null, Typeface.BOLD);
            try {
                tvCategory.setText(data.get(position));
            } catch (Exception e) {
                e.printStackTrace();
            }
            return row;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM