0

我必须在两个数组的单个 recyclerview 上显示数据。当我从菜单中获取数据时,我只从菜单中获取最后一个数据。如何从菜单中获取所有数据并将其设置在 RecyclerView 上。

这是我的代码:

JSONObject jsono = new JSONObject(response);
JSONArray jarray = jsono.getJSONArray("data");
for (int i = 0; i < jarray.length(); i++) {
    JSONObject object = jarray.getJSONObject(i);

    Username= object.getString("name");
    resID = object.getString("id");


    JSONArray jarray1 = object.getJSONArray("menu");
    for (int j = 0; j < jarray1.length(); j++) {

        JSONObject object1 = jarray1.getJSONObject(j);
        Proname = object1.getString("menu_name");
        Timing = object1.getString("collection_time");
        Price = object1.getString("menu_rate");
        Quantity = object1.getString("quantity_left");
        Proimage = object1.getString("img1");
        Availimg = object1.getString("img2");
        Fav = object1.getString("img3");
        menuID = object1.getString("id");

        Foodtype = object1.getString("food_type");

}

rcview.setAdapter(homeadapter);
homeadapter.notifyDataSetChanged();
4

3 回答 3

0

你的家庭模型看起来像这样

public class Data {

private String id;
private String name;
private String position;
private String storeName;
private String city;
private String address;
private String latitude;
private String longtitude;
private String phone1;
private String phone2;
private String email;
private String password;
private String typeOfCuisine;
private String openingDate;
private String openingDays;
private String storeCloseTime;
private String website;
private String fbUrl;
private String otherInfo;
private String date;
private String lastUpdated;
private String logo;
private String zip;
private String favorite;
private String regStatus;
private String regCode;
private List<Menu> menu = null;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPosition() {
    return position;
}

public void setPosition(String position) {
    this.position = position;
}

public String getStoreName() {
    return storeName;
}

public void setStoreName(String storeName) {
    this.storeName = storeName;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getLatitude() {
    return latitude;
}

public void setLatitude(String latitude) {
    this.latitude = latitude;
}

public String getLongtitude() {
    return longtitude;
}

public void setLongtitude(String longtitude) {
    this.longtitude = longtitude;
}

public String getPhone1() {
    return phone1;
}

public void setPhone1(String phone1) {
    this.phone1 = phone1;
}

public String getPhone2() {
    return phone2;
}

public void setPhone2(String phone2) {
    this.phone2 = phone2;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getTypeOfCuisine() {
    return typeOfCuisine;
}

public void setTypeOfCuisine(String typeOfCuisine) {
    this.typeOfCuisine = typeOfCuisine;
}

public String getOpeningDate() {
    return openingDate;
}

public void setOpeningDate(String openingDate) {
    this.openingDate = openingDate;
}

public String getOpeningDays() {
    return openingDays;
}

public void setOpeningDays(String openingDays) {
    this.openingDays = openingDays;
}

public String getStoreCloseTime() {
    return storeCloseTime;
}

public void setStoreCloseTime(String storeCloseTime) {
    this.storeCloseTime = storeCloseTime;
}

public String getWebsite() {
    return website;
}

public void setWebsite(String website) {
    this.website = website;
}

public String getFbUrl() {
    return fbUrl;
}

public void setFbUrl(String fbUrl) {
    this.fbUrl = fbUrl;
}

public String getOtherInfo() {
    return otherInfo;
}

public void setOtherInfo(String otherInfo) {
    this.otherInfo = otherInfo;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public String getLastUpdated() {
    return lastUpdated;
}

public void setLastUpdated(String lastUpdated) {
    this.lastUpdated = lastUpdated;
}

public String getLogo() {
    return logo;
}

public void setLogo(String logo) {
    this.logo = logo;
}

public String getZip() {
    return zip;
}

public void setZip(String zip) {
    this.zip = zip;
}

public String getFavorite() {
    return favorite;
}

public void setFavorite(String favorite) {
    this.favorite = favorite;
}

public String getRegStatus() {
    return regStatus;
}

public void setRegStatus(String regStatus) {
    this.regStatus = regStatus;
}

public String getRegCode() {
    return regCode;
}

public void setRegCode(String regCode) {
    this.regCode = regCode;
}

public List<Menu> getMenu() {
    return menu;
}

public void setMenu(List<Menu> menu) {
    this.menu = menu;
}

public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
}
}

和这样的菜单模型

public class Menu {

private String id;
private String restId;
private String menuRate;
private String collectionTime;
private String quantityLeft;
private String foodType;
private String img1;
private String img2;
private String img3;
private String date;
private String menuName;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getRestId() {
    return restId;
}

public void setRestId(String restId) {
    this.restId = restId;
}

public String getMenuRate() {
    return menuRate;
}

public void setMenuRate(String menuRate) {
    this.menuRate = menuRate;
}

public String getCollectionTime() {
    return collectionTime;
}

public void setCollectionTime(String collectionTime) {
    this.collectionTime = collectionTime;
}

public String getQuantityLeft() {
    return quantityLeft;
}

public void setQuantityLeft(String quantityLeft) {
    this.quantityLeft = quantityLeft;
}

public String getFoodType() {
    return foodType;
}

public void setFoodType(String foodType) {
    this.foodType = foodType;
}

public String getImg1() {
    return img1;
}

public void setImg1(String img1) {
    this.img1 = img1;
}

public String getImg2() {
    return img2;
}

public void setImg2(String img2) {
    this.img2 = img2;
}

public String getImg3() {
    return img3;
}

public void setImg3(String img3) {
    this.img3 = img3;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public String getMenuName() {
    return menuName;
}

public void setMenuName(String menuName) {
    this.menuName = menuName;
}

public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
}
}
于 2018-02-01T07:04:46.810 回答
0

您可以使用库 Gson 来简单地解析数据。 https://github.com/google/gson

String founderJson = "[{'name': 'Christian','flowerCount': 1}, {'name': 'Marcus', 'flowerCount': 3}, {'name': 'Norman', 'flowerCount': 2}]";

Gson gson = new Gson();

Type founderListType = new TypeToken<ArrayList<Founder>>(){}.getType();

List<Founder> founderList = gson.fromJson(founderJson, founderListType); 
于 2018-02-01T07:05:15.973 回答
0

您只创建一次 Home 对象,因此它会覆盖前一个对象并只为您提供最后一个值。在 j 的 for 循环中创建它。您将获得所有值。

for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);

Username= object.getString("name");
resID = object.getString("id");
// jarray = jsono.getJSONArray("data");

JSONArray jarray1 = object.getJSONArray("menu");
for (int j = 0; j < jarray1.length(); j++) {
    Home home= new Home();
    JSONObject object1 = jarray1.getJSONObject(j);
    Proname = object1.getString("menu_name");
    Timing = object1.getString("collection_time");
    Price = object1.getString("menu_rate");
    Quantity = object1.getString("quantity_left");
    Proimage = object1.getString("img1");
    Availimg = object1.getString("img2");
    Fav = object1.getString("img3");
    menuID = object1.getString("id");

    Foodtype = object1.getString("food_type");
    if (Foodtype.equals("Veg")){
        home.setAvailimg(R.drawable.green);
    }else if (Foodtype.equals("Non-Veg")){
        home.setAvailimg(R.drawable.red);
    }

    home.setUsername(Username);
    home.setProname(Proname);
    home.setTiming(Timing);
    home.setPrice(Price);
    home.setQuantity(Quantity);
    home.setProimage(Proimage);
    homeList.add(home);
}
}
于 2018-02-01T07:21:13.923 回答