2017-01-03 36 views
-1

爲什麼我需要重寫繼承某些類時返回泛型類如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

回答

0

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對象項目。

相關問題