为什么有必要重写Android上的Recycler类中的getData()?

问题描述:

为什么我需要重写继承某些类时返回泛型类如ArrayList的泛型方法?纠正我,如果我错了,但对于像ArrayList这样的泛型类,它看起来像是多余的和不必要的。为什么有必要重写Android上的Recycler类中的getData()?

public class Data { 
    public static ArrayList<Information> getData() { 
     ArrayList<Information> data = new ArrayList<>(); 
     int[] images = {R.drawable.img_1,R.drawable.img_2,R.drawable.img_3}; 
     String[] text = {"image_1", "image_2", "image_3"}; 

     for (int i = 0; i < images.length; i++) { 
      Information current = new Information(); 
      current.imageId = images[i]; 
      current.title = text[i]; 
      data.add(current); 
     } 
     return data; 
    } 
} 

public class Information { 
    public int imageId; 
    public String title; 
} 
+1

它是模型POJO。它包含您在回收视图中显示的数据\ –

+0

已编辑以尝试清除不明确的措辞。也认为这个问题可能不清楚这个概念。 – clearlight

Information是POJO数据和ArrayList<Information>意味着列表内容应该像Information POJO

例如在ArrayList中stringsList,列表的数据应该是string,您可以用方式字符串添加数据:

stringsList.add("firstString") 
    stringsList.add(" ") 
    stringsList.add(5+"") 
    and.... 

而是Information类型:

ArrayList<Information> infoList=new ArrayList(); 

    for(int i = 0 ; i<10){ 

    Information info=new Information; 
    info.imageId=i; 
    info.title="title : "+i; 

    infoList.add(info) 

    } 

,经过上面的代码,你infoList,包含10个Information对象项目。