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