我想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;
}
}
答案 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);
}