使用改造和recyclerview - 为什么我的ArrayList在适配器中为null?

时间:2017-02-11 01:42:55

标签: android google-maps android-recyclerview retrofit

我正在做什么 实际上,我正在谷歌的地方为餐馆及其一般信息做nearby search。另外,我使用google places details查找有关特定餐厅的更多详细信息。使用所有这些信息,我将把它放在recyclerview中。此项目中使用Retrofit来解析数据。

问题: 尽管使用restaurantPhoneNum方法,但其中一个ArrayList(null)为arraylist.add()。不知道为什么。

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

}

1 个答案:

答案 0 :(得分:0)

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

嗯,那不能为空。你刚刚初始化它......

Retrofit的限制是您只能拨打response.body() 一次

尝试提取它。

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

换句话说,我认为你应该基本上将Result重命名为Restaurant