2017-02-09 83 views
0

新手在這裏 - 作爲實踐,我使用google places搜索nearby餐廳,並得到他們的詳細資料。我還使用google place details獲取有關特定餐廳的更多信息。我正在使用Retrofit解析數據。如何從兩個改造請求數據傳遞到一個recyclerview適配器

問題: 我有三個ArrayListrestaurantNamerestaurantAddressrestaurantPhoneNum。我不能全部三個ArrayLists傳遞到我的recyclerview適配器。

獲取附近的餐廳方法:

private void getNearbySearchUrl(final double latitude, final double longitude, final String nearbyPlace){ 

    MapInterface retrofit = new Retrofit.Builder() 
      .baseUrl("https://maps.googleapis.com/maps/api/place/nearbysearch/") 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build().create(MapInterface.class); 

    Call<Result> call = retrofit.getResults("65.9667,-18.5333", PROXIMITY_RADIUS, "restaurant", placesKey); 

    call.enqueue(new Callback<Result>() { 
     @Override 
     public void onResponse(Call<Result> call, Response<Result> response) { 
      ArrayList<String> restaurantName = new ArrayList<>(); 
      ArrayList<String> restaurantAddress = new ArrayList<>(); 
      GetNearbyPlaces b = new GetNearbyPlaces(); 

      for(int i = 0; i <= response.body().getResults().size() - 1 ; i++) { 
       restaurantName.add(response.body().getResults().get(i).getName()); 
       restaurantAddress.add(response.body().getResults().get(i).getVicinity()); 

       //get telephone number through this method 
       getPlaceDetailsUrl(response.body().getResults().get(i).getPlaceId()); 

      } 

      Collections.reverse(restaurantName); 
      Collections.reverse(restaurantAddress); 

      adapter = new RestaurantListAdapter(restaurantName, restaurantAddress, 
        response.body().getResults().size()); 
      mRecyclerView.setAdapter(adapter); 
     } 
     @Override 
     public void onFailure(Call<Result> call, Throwable t) { 
      Log.d("Matt", "fail"); 
     } 
    }); 
} 

獲取有關餐廳方法的更多信息:

public String getPlaceDetailsUrl(final String placeId) { 
    final String mPlace = placeId; 

    MapInterface retrofit = new Retrofit.Builder() 
      .baseUrl("https://maps.googleapis.com/maps/api/place/details/") 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build().create(MapInterface.class); 

    Call<Result> call = retrofit.getPlaceDetails(placeId, placesKey); 

    call.enqueue(new Callback<Result>() { 
     @Override 
     public void onResponse(Call<Result> response) { 
      ArrayList<String> restaurantPhoneNum = new ArrayList<>(); 
      if (response.body().getResult().getFormattedPhoneNumber() == null){ 
       return; 
      } 
      restaurantPhoneNum.add(response.body().getResult().getFormattedPhoneNumber()); 

     } 

     @Override 
     public void onFailure(Call<Result> call, Throwable t) { 
      return; 
     } 
    }); 
} 

適配器:

public class RestaurantListAdapter extends RecyclerView.Adapter<RestaurantListAdapter.RestaurantListHolder> { 
    ArrayList<String> restaurantName = new ArrayList<>(); 
    ArrayList<String> restaurantAddress = new ArrayList<>(); 
    ArrayList<String> restaurantPhoneNum = new ArrayList<>(); 

private int restaurantCount; 

public RestaurantListAdapter(ArrayList<String> resName, ArrayList<String> resAddress, 
          int resCount){ 
    restaurantName = resName; 
    restaurantAddress = resAddress; 
    restaurantCount = resCount; 
} 

public RestaurantListAdapter(ArrayList<String> resPhoneNum){ 
    restaurantPhoneNum = resPhoneNum; 
} 



@Override 
public RestaurantListHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = LayoutInflater.from(parent.getContext()) 
      .inflate(R.layout.restaurant_list_item, parent, false); 
    RestaurantListHolder viewHolder = new RestaurantListHolder(view); 
    return viewHolder; 
} 

@Override 
public void onBindViewHolder(RestaurantListHolder holder, int position) { 
    Log.d("Matt1", String.valueOf(position) + " size:" + String.valueOf(restaurantName.size()) + restaurantName.toString()); 
    holder.bindView(position); 
} 



@Override 
public int getItemCount() { 
    return restaurantCount; 
} 

public class RestaurantListHolder extends RecyclerView.ViewHolder{ 
    public TextView mRestaurantName; 
    public TextView mAddress; 
    public TextView mPhone; 
    public TextView mDistance; 
    public ImageView mRestaurantPic; 

    public RestaurantListHolder(View itemView) { 
     super(itemView); 
     mRestaurantName = (TextView) itemView.findViewById(R.id.tvRestaurantName); 
     mAddress = (TextView) itemView.findViewById(R.id.tvAddress); 
     mPhone = (TextView) itemView.findViewById(R.id.tvPhone); 
    } 
    public void bindView(int arrayNum){ 
     mRestaurantName.setText(restaurantName.get(arrayNum)); 
     mAddress.setText(restaurantAddress.get(arrayNum)); 
     mPhone.setText("123 123 123 123 "); 
     mRestaurantPic.setImageResource(R.drawable.rzkibble_chinese); 
    } 
} 

}

+0

兩種方式:1。如果要在兩次迭代中更新適配器(儘早準備好與用戶列表交互),請保留一個通用對象,以便從兩個調用中提供適配器。 2.如果要在兩次調用後顯示完整信息,則按同步順序進行調用,並將所有信息提供給一個對象,然後交給適配器進行繪製。 ...我希望這對你有意義:) – harneev

回答

1

好吧,首先建議,儘量去想它一點點在你自己。我知道你知道如何解決這個:)。

你的問題留給不清楚:

    要更新適配器後兩個請求被執行
  • 你們每個人都要求

我想,要實現這後更新適配器

  • 以第一種方式。 您需要具有執行requestOne邏輯,執行requestTwo當兩個請求都成功,更新適配器。你可以用arePlacesLoadedareDetailsLoaded並有接收請求結果並檢查這些標誌第三種方法的一些全局標誌做到這一點。

    手動方式(僞)

    boolean arePlacesLoaded; 
    boolean areDetailsLoaded; 
    
    void loadPlaces(){ 
        if(success){ 
         arePlacesLoaded = true; 
         savePlaces(places) 
         updateAdapter() 
        } 
    } 
    
    void loadDetails(){ 
        if(success){ 
         areDetailsLoaded = true; 
         saveDetails(details) 
         updateAdapter() 
        } 
    } 
    
    void updateAdaper(){ 
        if(arePlacesLoaded && areDetailsLoaded){ 
         adaper.onDataChange(places, details); 
        } 
    } 
    

    你可以使用一些隊列,你把請求,一旦請求被執行,隊列爲空 - >更新適配器。

    另外,更好的方式是用RxJava及其zipWith運算符,其中實施需要幾行只(考慮具有改進的地方移動到應用程序的核心)的使用 這裏就是一個大理石圖運營商: zipWith marble

    所以,請求一個執行,然後請求兩個。您將提供一個將數據合併到名爲Hotel的更好對象的函數,並且您將使用Hotel對象的數組提供適配器。

  • 相關問題