如何使用Json Array中的循环显示所有数据

时间:2017-01-27 04:46:16

标签: android loops android-recyclerview

如何循环显示所有美食数据(新美国,日本,亚洲)。在RestaurantRVAdapter中,我这样使用了 -
List<Restaurant> restaurant; // There is a data in restaurant holder.tv_cuisine.setText(restaurant.getCuisine().get(0).getCuisineName());

它只显示新美国人,因为我使用.get(0).getCuisineName()

[
  {...},
  {...},
  {...},
  {...},
  {
    "restaurant_id": "41",
    "restaurant_logo": "52ee9f67ce39f62d9c0b1538ca26646f.jpg",
    "restaurant_name": "Shwe Lar Food Restaurant",
    "street_address": "Napier Road",
    "phone_no": "09940255323430",
    "rating": "4.5",
    "cuisine": [
      {
        "cuisine_name": "New American"
      },
      {
        "cuisine_name": "Japanese"
      },
      {
        "cuisine_name": "Asia"
      }
    ],
  }
]

5 个答案:

答案 0 :(得分:0)

在您的RecyclerView适配器中更新您的代码。

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Restaurant rest = restaurant.get(position);
    holder.tv_cuisine.setText(rest.getCuisineName());
}

答案 1 :(得分:0)

首先你得到jsonArray然后使用fori lool获取所有json对象然后你可以得到exat数据,比如

JSONArray jsonArray = obj.getJSONArray("cuisine");
  List<JSONObject> postList = new ArrayList<JSONObject>();

for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonPost = jsonArray.getJSONObject(i);
                        postList.add(jsonPost);
                    }
                    mAdapter.updateList(postList, mStockstype);

在适配器中,您可以创建类似

的方法
  public void updateList(List<JSONObject> list, String type) {
    mDataList.clear();
    mDataList.addAll(list);
    notifyDataSetChanged();
}

然后在适配器中为您提供数据

   JSONObject object = mDataList.get(position);
    String name = object.optString("cuisine_name");

答案 2 :(得分:0)

为美食制作以逗号分隔的文字,

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Restaurant rest = restaurant.get(position);
    List<Cuisine> cuisineList = rest.getCuisine();
    // Build comma separated text for Cuisines
    String strCuisine = "";
    for(i=0; i<cuisineList.size(); i++) {
        strCuisine.concat(cuisineList.get(i).getCuisineName());
        strCuisine.concat(",");
    }
    holder.tv_cuisine.setText(strCuisine);
}

答案 3 :(得分:0)

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Restaurant rest = restaurant.get(position); // get the specific restaurent
    List<Cuisine> cuisineList = rest.getCuisine();  // list to carry cuisineList
    String cuisineString = ""; //string to carrying cuisines
    for(int i=0; i<cuisineList.size(); i++) {
        cuisineString.concat(cuisineList.get(i));
        cuisineString.concat(",");
    }
    holder.tv_cuisine.setText(strCuisine);
}

答案 4 :(得分:0)

应该这样,

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Restaurant rest = restaurant.get(position); // get the specific restaurent
    List<Cuisine> cuisineList = rest.getCuisine();  // list to carry cuisineList
    String cuisineString = ""; //string to carrying cuisines
    for(int i=0; i<cuisineList.size(); i++) {
        cuisineString.concat(cuisineList.get(i).getCuisineName());
        cuisineString.concat(",");
    }
    cuisineString = cuisineString.substring(0, cuisineString.length() - 1);// Removes last ,
    holder.tv_cuisine.setText(cuisineString);
}