2017-01-27 77 views
-1

如何循環顯示所有美食數據(新美國,日本,亞洲)。在RestaurantRVAdapter,我用這樣的 -
List<Restaurant> restaurant; // There is a data in restaurant holder.tv_cuisine.setText(restaurant.getCuisine().get(0).getCuisineName());如何使用Json數組循環顯示所有數據

只顯示新的美國,因爲我使用.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" 
     } 
    ], 
    } 
] 
+0

什麼/你怎麼想顯示在您的美食名單? – njzk2

+0

是的。我想要顯示美食清單。 – WPG

+1

發佈你的代碼,以便我可以建議你一個答案。 –

回答

0

在你RecyclerView適配器更新代碼這樣。

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

不,餐館列表中沒有.getCuisineName()。餐廳列表<料理列表 WPG

+0

發佈您的'餐廳'模型班。 – Shashanth

0

首先你jsonArray然後用法院地法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"); 
+0

我在哪裏得到jsonArray代碼?在RV適配器? – WPG

+0

將此代碼粘貼到您的類中,如Fragment或Activity,在此您將適配器設置爲listView –

+0

分享您的適配器 –

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

嗨,它說無法解決方法getAdapterPosition();然後concat; in String不能應用於com.wp.model.Cuisine'cuisineList.get(i)' – WPG

+0

只需使用'position'。看編輯。但是你也可以使用'holder.getAdapterPosition'。 – Wizard

+0

謝謝,但如何解決concat String to Model(cuisineList.get(i)) – WPG

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

應該像現在這樣,

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