2017-02-11 7 views
-1

私がやっていること 練習として、私はレストランとその一般情報のためにnearby searchを行うGoogleの場所を使用しています。加えて、私はgoogle places detailsを使って特定のレストランの詳細を調べています。すべての情報を使って、私はそれをrecyclerviewに入れようとしています。このプロジェクトではデータを解析するためにRetrofitが使用されています。retrofitとrecyclerviewを使用する - なぜArrayListのArrayListがnullですか?

問題:ArrayListの(restaurantPhoneNum)の 一つはarraylist.add()方法を使用してもかかわらずnullあります。理由は分かりません。

MainActivity:

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) { 
      adapter = new RestaurantListAdapter(
        response.body().getResults().size()); 
      for(int i = 0; i <= response.body().getResults().size() - 1 ; i++) { 
       adapter.addNearByData(response.body().getResults().get(i).getName(), response.body().getResults().get(i).getVicinity()); 
       getPlaceDetailsUrl(response.body().getResults().get(i).getPlaceId()); 
      } 
      mRecyclerView.setAdapter(adapter); 
     } 
     @Override 
     public void onFailure(Call<Result> call, Throwable t) { 
      Log.d("Matt", "fail"); 
     } 
    }); 
} 

public void 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<googleplayservices.samples.android.teamtreehouse.com.kibbledriverb.Maps.Retrofit.PlaceDetailsPojo.Result> call = retrofit.getPlaceDetails(placeId, placesKey); 

    call.enqueue(new Callback<googleplayservices.samples.android.teamtreehouse.com.kibbledriverb.Maps.Retrofit.PlaceDetailsPojo.Result>() { 
     @Override 
     public void onResponse(Call<googleplayservices.samples.android.teamtreehouse.com.kibbledriverb.Maps.Retrofit.PlaceDetailsPojo.Result> call, Response<googleplayservices.samples.android.teamtreehouse.com.kibbledriverb.Maps.Retrofit.PlaceDetailsPojo.Result> response) { 
      if (response.body().getResult().getFormattedPhoneNumber() == null){ 
       Log.d("wtf", "null value"); 
       return; 
      } 
      adapter.addPlacesData(response.body().getResult().getFormattedPhoneNumber()); 
     } 

     @Override 
     public void onFailure(Call<googleplayservices.samples.android.teamtreehouse.com.kibbledriverb.Maps.Retrofit.PlaceDetailsPojo.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<>(); //this is null 

    private int restaurantCount; 

    public RestaurantListAdapter(int resCount){ 
     restaurantCount = resCount; 
    } 


    @Override 
    public RestaurantListHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     Log.d("blue1", restaurantPhoneNum.toString()); //restaurantPhoneNum is null 
     View view = LayoutInflater.from(parent.getContext()) 
       .inflate(R.layout.restaurant_list_item, parent, false); 
     RestaurantListHolder viewHolder = new RestaurantListHolder(view); 
     Collections.reverse(restaurantName); 
     Collections.reverse(restaurantAddress); 
     Collections.reverse(restaurantPhoneNum); 
     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(RestaurantListHolder holder, int position) { 
     Log.d("blue2", restaurantPhoneNum.toString());//restaurantPhoneNum is null 
     holder.bindView(position); 
    } 
    public void addNearByData(String resName, String resAddress){ 
     restaurantName.add(resName); 
     restaurantAddress.add(resAddress); 
    } 

    public void addPlacesData(String resPhoneNum){ 
     restaurantPhoneNum.add(resPhoneNum); 
     Log.d("blue3", restaurantPhoneNum.toString()); //not null when logged here, why is that? 
    } 

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

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

     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(""); 
     } 
    } 

} 

答えて

0
ArrayList<String> restaurantPhoneNum = new ArrayList<>(); //this is null 

まあ、nullにすることはできません。あなたはちょうどそれを初期化しました...

response.body()としか呼べないRetrofitの制限です。

それを抽出してください。これに代えても

final List<Result> results = response.body().getResults(); 
adapter = new RestaurantListAdapter(results.size()); 
for(Result r : results) { 
    adapter.addNearByData(r.getName(), r.getVicinity()); 
    getPlaceDetailsUrl(r.getPlaceId()); 
} 

、...

ArrayList<String> restaurantName = new ArrayList<>(); 
ArrayList<String> restaurantAddress = new ArrayList<>(); 
ArrayList<String> restaurantPhoneNum = new ArrayList<>(); //this is null 

この

public class Restaurant { 
    String name, address, phone; 
} 

public class RestaurantListAdapter extends ... { 

    List<Restaurant> restaurants = new ArrayList<Restaurant>(); 
を実行します。 210つまり、基本的には Resultから Restaurantに名前を変更する必要があります。

関連する問題