不可转换类型错误

时间:2017-12-31 21:33:59

标签: java android list arraylist type-conversion

我想menus.add(menuTemp);,但会显示Inconvertible types error。我试过menus.add((Restaurant.Menu) menuTemp),这没用。有什么建议吗?

add (java.util.List<oz.ncclife.model.Restaurant.Menu>)
to (java.util.ArrayList<java.lang.String>)

项目的一部分可以提供帮助

ArrayList<String> menuTemp = new ArrayList<>();
List<List<Restaurant.Menu>> menus = new ArrayList<>();

//Change structure
ArrayList<Object> objPhones = new ArrayList<Object>();
for(int i = 0; i < phones.size(); i++)
{
        objPhones.add(phones.get(i));
}

ArrayList<Object> objMenus = new ArrayList<Object>();
for(int i = 0; i < menus.size(); i++)
{
         objMenus.add(menus.get(i));
}

tinydb.putListObject("restMenus",objMenus);
tinydb.putListObject("restPhones",objPhones);



//restore part
ArrayList<Object> objPhones = tinydb.getListObject("restPhones",Object.class);
ArrayList<Object> objMenus = tinydb.getListObject("restMenus",Object.class);

ArrayList<String> phoneTemp = new ArrayList<String>();
ArrayList<String> menuTemp = new ArrayList<>();

for(int i = 0; i < objPhones.size(); i++)
{
            phoneTemp.add(objPhones.get(i).toString());
            menuTemp.add(objMenus.get(i).toString());
}
phones.add(phoneTemp);
menus.add(menuTemp);

另外,Restaurant.java

public class Restaurant
{
@SerializedName("cacheVersion")
@Expose
public String cacheVersion;
@SerializedName("id")
@Expose
public Integer id;
@SerializedName("name")
@Expose
public String name;
@SerializedName("desc")
@Expose
public String desc;
@SerializedName("phones")
@Expose
public List<String> phones = null;
@SerializedName("menus")
@Expose
public List<Menu> menus = null;
@SerializedName("image")
@Expose
public String image;


public class Menu
{
    @SerializedName("name")
    @Expose
    public String name;
    @SerializedName("foods")
    @Expose
    public List<Food> foods = null;
}


public class Food
{
    @SerializedName("name")
    @Expose
    public String name;
    @SerializedName("desc")
    @Expose
    public String desc;
    @SerializedName("price")
    @Expose
    public String price;
}
}

1 个答案:

答案 0 :(得分:2)

您无法添加

ArrayList<String> menuTemp = new ArrayList<>();

进入此变量

List<List<Restaurant.Menu>> menus = new ArrayList<>();

因为编译器正在等待List Restaurant.Menu而不是List String

您应该像这样更改要添加的List的类型:

  List<Restaurant.Menu> menuTemp = new ArrayList<>();
  

<强>更新

要向menuTemp ArrayList添加数据,您应该使用Restaurant.Menu类的构造函数:

for(int i = 0; i < objPhones.size(); i++)
{
            phoneTemp.add(objPhones.get(i).toString());
            Restaurant.Menu menuClass = new Restaurant.Menu();
            menuClass.name = objMenus.get(i).toString();
            menuTemp.add(menuClass);
}

更新2

改变这个:

ArrayList<Object> objMenus = tinydb.getListObject("restMenus",Object.class);

到此:

ArrayList<Menu> objMenus = tinydb.getListObject("restMenus",Menu.class);


for(int i = 0; i < objPhones.size(); i++)
    {
                phoneTemp.add(objPhones.get(i).toString());
                Restaurant.Menu menuClass = new Restaurant.Menu();
                menuClass.name = objMenus.get(i).name;
                menuTemp.add(menuClass);
    }