新手在这里 - 作为实践,我使用google places
搜索nearby
餐厅,并得到他们的详细资料。我还使用google place details
获取有关特定餐厅的更多信息。我正在使用Retrofit
解析数据。如何从两个改造请求数据传递到一个recyclerview适配器
问题: 我有三个ArrayList
,restaurantName
,restaurantAddress
和restaurantPhoneNum
。我不能全部三个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);
}
}
}
两种方式:1。如果要在两次迭代中更新适配器(尽早准备好与用户列表交互),请保留一个通用对象,以便从两个调用中提供适配器。 2.如果要在两次调用后显示完整信息,则按同步顺序进行调用,并将所有信息提供给一个对象,然后交给适配器进行绘制。 ...我希望这对你有意义:) – harneev