2014-07-02 58 views
0

我收到此错误消息: - “for(Item item:Items)”关于“Items”关键字,错误为“Type mismatch: cannot convert from element type Object to Item”。有人请帮我解决这个问题。Android阵列列表

import java.util.ArrayList; 

public class Model 
{ 
    public static ArrayList Items; 

    public static void LoadModel() 
    { 
     Items=new ArrayList(); 
     Items.add(new Item(1, "taj.png", "Taj Mahal", "arrow.png")); 
     Items.add(new Item(2, "agra_fort.png", "Agra Fort", "arrow.png")); 
     Items.add(new Item(3, "chini _ka_rauza.png", "Chini ka Rauza", "arrow.png")); 
     Items.add(new Item(4, "fatepur_sikri.png", "Fatepur Sikri", "arrow.png")); 
     Items.add(new Item(5, "guru_ka_tal.png", "Guru Ka Tal", "arrow.png")); 
     Items.add(new Item(6, "jamma_masjid.png", "Jamma Masjid", "arrow.png")); 
     Items.add(new Item(7, "mankameshwar_temple.png", "Mankameshwar", "arrow.png")); 
     Items.add(new Item(8, "mehtab_baug.png", "Mehtab Baug", "arrow.png")); 
     Items.add(new Item(9, "musamman_burj.png", "Musamman Burj", "arrow.png")); 
     Items.add(new Item(10, "panch_mahal.png", "Panch Mahal", "arrow.png")); 


    } 
    public static Item GetbyId(int id) 
    { 
     for(Item item:Items) 
     { 
      if(item.id==id) 
      { 
       return item; 
      } 
     } 
     return null; 
    } 

} 
+0

尝试这种方式申报清单:公共静态的ArrayList 项目; –

+0

嗨thnx ..错误已解决,但我的项目不工作..我越来越接近力量。 – Vibhuti

+0

尝试在比较之前打印item.id和id值。 –

回答

0

它应该是Items= new ArrayList<Item>();否则Java假设Object是最坏的情况。

+0

嘿thnx ..但它不工作..你可以建议我更多的东西..? – Vibhuti

+0

哦,正如@Aniruddha所说 - 你在声明中也需要。 –

2

首先初始化的ArrayList作为

ArrayList<Item> Items = new ArrayList<Item>();