Android阵列列表

时间:2014-07-02 10:04:47

标签: android arraylist

我在此行收到错误: - “for(Item item:Items)”关键字上的“Items”,错误为“Type mismatch: cannot convert from element type Object to Item”。有人请帮帮我。

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;
    }

}

2 个答案:

答案 0 :(得分:2)

首先将ArrayList初始化为

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

答案 1 :(得分:0)

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