片段更改时没有获得完整的包

时间:2017-09-09 05:50:27

标签: android google-maps android-fragments android-recyclerview

您好我正在尝试创建ola / uber排序应用。 我正在编辑文本中键入一些文本,并基于调用Place API并形成建议数组。该数组是使用下面给出的类的数组形成的。

import com.google.android.gms.location.places.Place;

import java.io.Serializable;

public class SearchListProvider implements Serializable {
    private int imageId;
    private String name;
    private Place place;

    public SearchListProvider(int imageId, String name, Place place) {
        this.imageId = imageId;
        this.name = name;
        this.place = place;
    }

    public String getName() {
        return name;
    }

    public int getImageId() {
        return imageId;
    }


    public Place getPlace() {
        return place;
    }

    public void setPlace(Place place) {
        this.place = place;
    }

    public void setImageId(int imageId) {

        this.imageId = imageId;
    }

    public void setName(String name) {
        this.name = name;
    }

}

我的适配器部分在下面给出了我用下拉列表附加所有值的部分。

public void onBindViewHolder(final RecyclerViewHolder holder, int position) {
    SearchListProvider listProvider = null;
    if(holder.viewtype == TYPE_LIST){
        listProvider= arrayList.get(position);
        holder.list_image.setImageResource(listProvider.getImageId());
        holder.address_name.setText(listProvider.getName());
        (holder.itemView).setTag(listProvider);
    }
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Fragment f= new AmbulanceMap();
            Bundle b= new Bundle();
            SearchListProvider p= (SearchListProvider) (v).getTag();
            b.putSerializable("selectedLocation",  p);
            f.setArguments(b);
            activity.getSupportFragmentManager().beginTransaction().replace(R.id.main_container, f).commit();
        }
    });
}

到此为止,一切运作良好。现在,当我点击下拉列表中的任何项目时,片段会发生变化,我会将序列化对象发送到另一个片段,并尝试获取它。但是那里发生的事情是我只获得了一半的数据。

Bundle b= getArguments();
    if(b != null){
        SearchListProvider p= (SearchListProvider)b.getSerializable("selectedLocation");
        if(p != null){
            LatLng latLng= p.getPlace().getLatLng();
            //setCurrentLocationMarker(latLng);
            Toast.makeText(getContext(), latLng.latitude + "", Toast.LENGTH_SHORT).show();
        }

    }

在调试时我发现place的值为null。 我不明白为什么? 任何人都可以帮助我吗?

注意:getTag()函数本身给出了空值。

现在我尝试评估功能

p.getPlace().getName()

我的错误是

    Method threw 'java.lang.IllegalArgumentException' exception.
    cause = Object has been collected
detailMessage = Object has been collected
stackState = Object has been collected
stackTrace = Object has been collected

新编辑 - 此功能发生在PlaceId和返回地点

protected ArrayList<SearchListProvider> doInBackground(String... params) {
    String inputText= params[0];
    PendingResult<AutocompletePredictionBuffer> autocompletePredictions = geoDataApi.
            getAutocompletePredictions(GoogleClient.getIstance(), inputText, null, null);
    AutocompletePredictionBuffer autocompletePredictionBuffer = autocompletePredictions.await();
    ArrayList<SearchListProvider> searchList= new ArrayList<>();
    for(int i=0; i<autocompletePredictionBuffer.getCount(); i++){

        geoDataApi.getPlaceById(GoogleClient.getIstance(), autocompletePredictionBuffer.get(i).getPlaceId()).setResultCallback(new ResolvingResultCallbacks<PlaceBuffer>(fragmentActivity, 0) {
            @Override
            public void onSuccess(@NonNull PlaceBuffer places) {
                p = places.get(0);
                places.release();
            }

            @Override
            public void onUnresolvableFailure(@NonNull com.google.android.gms.common.api.Status status) {

            }
        });
        searchList.add(
                new SearchListProvider(
                        R.mipmap.ic_launcher,
                        autocompletePredictionBuffer.get(i).getFullText(null).toString(),
                        p)
        );
    }
    return searchList;
}

2 个答案:

答案 0 :(得分:0)

试试这个:

holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Fragment f= new AmbulanceMap();
        Bundle b= new Bundle();
        b.putSerializable("selectedLocation",  listProvider);
        f.setArguments(b);
        activity.getSupportFragmentManager().beginTransaction().replace(R.id.main_container, f).commit();
    }
});

答案 1 :(得分:0)

问题是你的SearchListProvider类实现了SerializablePlace对象没有实现Serializable这就是为什么place对象没有被序列化的原因一片空白。

现在,您可以通过仅将地点对象中的必填字段添加到SearchListProvider对象来解决此问题。

尝试使用此自动填充预测列表的回调

private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback
            = new ResultCallback<PlaceBuffer>() {
        @Override
        public void onResult(@NonNull PlaceBuffer places) {
            if (!places.getStatus().isSuccess()) {
                // Request did not complete successfully
                places.release();
                return;
            }
            // Get the Place object from the buffer.
            final Place place = places.get(0);
            lat = place.getLatLng().latitude;
            lng = place.getLatLng().longitude;

            places.release();
        }
};

并使用它:

locationAutoComplete.setAdapter(mAdapter);
        locationAutoComplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                final AutocompletePrediction item = mAdapter.getItem(position);
                if (item != null) {
                    final CharSequence primaryText = item.getFullText(null);
                    locationAutoComplete.setText(primaryText);
                    final String placeId = item.getPlaceId();


                    PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
                            .getPlaceById(client, placeId);
                    placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
                }
            }
        });