2014-02-13 21 views
0

在我的android应用程序中,我有ItemCategory POJO和ItemSubcategory POJO,它们都扩展了包含一些共同特征的POJO类。是否有可能将这两种方法合并为一个,可能使用..多态性?

我有传递对象到的ArrayList定制负载微调方法,并且它它们加载到一个旋转器(用于下拉了Android名称/选择)

我试图通过一个ArrayList到单个方法loadCategoriesIntoSpinner(它接受一个ArrayList作为参数),我期望它能够理清Im传递的是一个ArrayList,但它没有。

public static void loadCategoriesIntoSpinner(Context context, ArrayList<ItemCategory> array, Spinner spinner) { 

    // Creating adapter for spinner 
    ArrayAdapter<ItemCategory> dataAdapter = new ArrayAdapter<ItemCategory>(context, 
      R.drawable.simple_spinner_item, array); 

    // Drop down layout style - list view with radio button 
    dataAdapter 
      .setDropDownViewResource(R.drawable.simple_spinner_dropdown_item); 

    // Attaching data adapter to spinner 
    spinner.setAdapter(dataAdapter); 
} 

public static void loadSubcategoriesIntoSpinner(Context context, ArrayList<ItemSubcategory> array, Spinner spinner) { 

    // Creating adapter for spinner 
    ArrayAdapter<ItemSubcategory> dataAdapter = new ArrayAdapter<ItemSubcategory>(context, 
      R.drawable.simple_spinner_item, array); 

    // Drop down layout style - list view with radio button 
    dataAdapter 
      .setDropDownViewResource(R.drawable.simple_spinner_dropdown_item); 

    // Attaching data adapter to spinner 
    spinner.setAdapter(dataAdapter); 
} 

下面是分类对象:

主要类类别:

abstract public class Category { 

    private int id; 
    private String name; 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    @Override 
    public String toString() { 
     return this.name; 
    } 

} // End of Class 

继承类ItemCategory

public class ItemCategory extends Category { 

    private String logo; 

    public String getLogo() { 
     return logo; 
    } 

    public void setLogo(String logo) { 
     this.logo = logo; 
    } 

} // End of Class 

继承类ItemSubcategory:

public class ItemSubcategory extends Category { 

    private int parentID; 

    public void setParentID(int parentID) { 
     this.parentID = parentID; 
    } 

    public int getParentID() { 
     return parentID; 
    } 

} // End of Class 

回答

2
public static void <T extends Category> loadCategoriesIntoSpinner(Context context, List<T> array, Spinner spinner) { 
    ArrayAdapter<T> dataAdapter = new ArrayAdapter<T>(context, R.drawable.simple_spinner_item, array); 
    ... 
3
public static void loadSubcategoriesIntoSpinner(Context context, List<? extends Category> array, Spinner spinner) 

或也

public static void loadSubcategoriesIntoSpinner(Context context, List<Category> array, Spinner spinner) 
如果您创建列表, List<Category>

我建议你使用接口List而不是像ArrayList这样的具体实现。这样,您可以更改实施(例如,以LinkedList为例),而无需修改代码。

+0

什么功能我不得不放弃,如果不使用ArrayList而不是List? –

+2

使用List接口意味着你可以改变你的实现细节(即使用不同的List实现)而不需要改变你的其他代码。这样做是最好的做法。 –

+0

所以无论我有什么清单(linkedList,arrayList)我总是可以将它传递给接受列表的方法 –

相关问题