Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将两个改装请求中的数据传递到单个recyclerview适配器_Java_Android_Google Maps_Retrofit_Google Places Api - Fatal编程技术网

Java 如何将两个改装请求中的数据传递到单个recyclerview适配器

Java 如何将两个改装请求中的数据传递到单个recyclerview适配器,java,android,google-maps,retrofit,google-places-api,Java,Android,Google Maps,Retrofit,Google Places Api,这里的菜鸟-作为练习,我正在使用google places搜索附近的餐馆并获取它们的详细信息。我还使用谷歌的地方细节,以获得更多的信息,具体的餐厅。我正在使用改型来解析数据 问题是: 我有三个ArrayList,restaurantName,restaurantAddress和RestaurantPhoneum。我无法将所有三个ArrayList传递到我的recyclerview适配器中 获取附近餐厅的方法: 获取有关餐厅方法的更多信息: 适配器: }好的,第一个建议,试着自己想一想。我知道你知

这里的菜鸟-作为练习,我正在使用google places搜索附近的餐馆并获取它们的详细信息。我还使用谷歌的地方细节,以获得更多的信息,具体的餐厅。我正在使用改型来解析数据

问题是: 我有三个ArrayList,restaurantName,restaurantAddress和RestaurantPhoneum。我无法将所有三个ArrayList传递到我的recyclerview适配器中

获取附近餐厅的方法:

获取有关餐厅方法的更多信息:

适配器:


}好的,第一个建议,试着自己想一想。我知道你知道如何解决这个问题

你的问题不清楚:

您希望在执行两个请求后更新适配器 您将在每次请求后更新适配器 我想您希望以第一种方式实现这一点。 您需要有执行requestOne和requestTwo的逻辑,当两个请求都成功时,更新适配器。您可以使用一些全局标志arePlacesLoaded和areDetailsLoaded来实现这一点,并使用第三个方法来接收请求结果并检查这些标志

人工方式

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操作符,在这种情况下,实现只需几行代码,只需考虑到改造已转移到应用程序的核心部分 以下是该操作员的大理石图:


所以,执行请求一,然后执行请求二。您将提供一个函数,该函数将数据合并到名为Hotel的更好的对象中,并向适配器提供Hotel对象数组

两种方式:1。若您想在两次迭代中提前更新您的适配器,并准备好与用户列表交互,那个么请保留一个公共对象,以便从两个调用向适配器提供信息。2.若你们想在两次调用后都显示完整的信息,那个么就按同步顺序进行调用,将所有信息输入到一个对象中,并将其交给适配器绘制。。。我希望这对你有意义:
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);
    }
}
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);
    }
}