简体   繁体   中英

Inconvertible types error

I want to do menus.add(menuTemp); but it shows Inconvertible types error . I tried menus.add((Restaurant.Menu) menuTemp) , this didn't work. Any suggestion?

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

A part of project may help

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

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

You can't add

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

into this variable

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

because the compiler is waiting for a List of Restaurant.Menu not a List of String .

You should change the type of the List that you want to add like this way:

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

UPDATE

To add data to your menuTemp ArrayList you should us the constructor of the Restaurant.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).toString();
            menuTemp.add(menuClass);
}

UPDATE 2

Change this :

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

to this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM