2017-02-09 45 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对象的数组提供适配器。

  • 相关问题