0
Good morning I had posted the doubt but now I tried to simplify to be simpler the help I hope you can help me. I’m using googleplaces and when I write in an editText I get suggestions of places in recyclerView, I followed a tutorial to do this in an Activity and it’s working but now I need to adapt the code to a Fragment .
The things I don’t know if I’m doing right is the following in the code of the Activity appears to put this and I put in Fragment Getactivity(). Getappcontext() to try to adapt the code and so replace does not give error but I do not know if it is correct.
The error may also be associated with a constructor I was forced to create in Recyclerlistener or maybe in Adapter
I don’t know if it’s right to go like this to the Adapter
mAutoCompleteAdapter = new PlacesAutoCompleteAdapter(getActivity().getApplicationContext(), R.layout.searchview_adapter,
mGoogleApiClient, BOUNDS_INDIA, null);
The code of the Adapter is this :
public PlacesAutoCompleteAdapter(Context context, int resource, GoogleApiClient googleApiClient,
LatLngBounds bounds, AutocompleteFilter filter) {
mContext = context;
layout = resource;
mGoogleApiClient = googleApiClient;
mBounds = bounds;
mPlaceFilter = filter;
}
I was forced to create this constructor in the Reclyclerlistener :
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
I have below the rest of the code and there is the constructor that I use for Activity and works at Activity now I needed to create a new constructor to work on Fragment and my attempt I do not know if it was successful.
The code of my Reclycler Listener :
public class RecyclerItemClickListener implements RecyclerView.OnItemTouchListener {
private OnItemClickListener mListener;
GestureDetector mGestureDetector;
public RecyclerItemClickListener(Context context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
public interface OnItemClickListener {
public void onItemClick(View view, int position);
}
public RecyclerItemClickListener(PlacesAutoCompleteActivity context, OnItemClickListener listener) {
mListener = listener;
mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
@Override public boolean onInterceptTouchEvent(RecyclerView view, MotionEvent e) {
View childView = view.findChildViewUnder(e.getX(), e.getY());
if (childView != null && mListener != null && mGestureDetector.onTouchEvent(e)) {
mListener.onItemClick(childView, view.getChildLayoutPosition(childView));
return true;
}
return false;
}
@Override public void onTouchEvent(RecyclerView view, MotionEvent motionEvent) { }
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
The code of my fragment1 :
import com.example.hp.tumg13.R;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.PendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.places.PlaceBuffer;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
public class Fragment1 extends Fragment implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, View.OnClickListener {
protected GoogleApiClient mGoogleApiClient;
private static final LatLngBounds BOUNDS_INDIA = new LatLngBounds(
new LatLng(-0, 0), new LatLng(0, 0));
private EditText mAutocompleteView;
private RecyclerView mRecyclerView;
private LinearLayoutManager mLinearLayoutManager;
private LinearLayout ll;
private PlacesAutoCompleteAdapter mAutoCompleteAdapter;
ImageView delete;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null){
return null;
}
LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.fragement1_layout, container, false);
mRecyclerView= (RecyclerView) ll.findViewById(R.id.recyclerView);
mAutocompleteView = (EditText)ll.findViewById(R.id.autocomplete_places);
delete=(ImageView)ll.findViewById(R.id.cross);
mAutoCompleteAdapter = new PlacesAutoCompleteAdapter(getActivity().getApplicationContext(), R.layout.searchview_adapter,
mGoogleApiClient, BOUNDS_INDIA, null);
mLinearLayoutManager=new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(new LinearLayoutManager(mRecyclerView.getContext()));
mRecyclerView.setAdapter(mAutoCompleteAdapter);
mRecyclerView.setHasFixedSize(true);
delete.setOnClickListener((View.OnClickListener) getActivity());
mAutocompleteView.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (!s.toString().equals("") && mGoogleApiClient.isConnected()) {
mAutoCompleteAdapter.getFilter().filter(s.toString());
} else if (!mGoogleApiClient.isConnected()) {
Toast.makeText(getActivity().getApplicationContext(), Constants.API_NOT_CONNECTED, Toast.LENGTH_SHORT).show();
Log.e(Constants.PlacesTag, Constants.API_NOT_CONNECTED);
}
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
}
});
mRecyclerView.addOnItemTouchListener(
new RecyclerItemClickListener(getActivity().getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
final PlacesAutoCompleteAdapter.PlaceAutocomplete item = mAutoCompleteAdapter.getItem(position);
final String placeId = String.valueOf(item.placeId);
mAutocompleteView.setText(item.description);
Log.i("TAG", "Autocomplete item selected: " + item.description);
/*
Issue a request to the Places Geo Data API to retrieve a Place object with additional details about the place.
*/
PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
.getPlaceById(mGoogleApiClient, placeId);
placeResult.setResultCallback(new ResultCallback<PlaceBuffer>() {
@Override
public void onResult(PlaceBuffer places) {
if (places.getCount() == 1) {
//Do the things here on Click.....
Toast.makeText(getActivity().getApplicationContext(), String.valueOf(places.get(0).getLatLng()), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity().getApplicationContext(), Constants.SOMETHING_WENT_WRONG, Toast.LENGTH_SHORT).show();
}
}
});
Log.i("TAG", "Clicked: " + item.description);
Log.i("TAG", "Called getPlaceById to get Place details for " + item.placeId);
}
})
);
return (LinearLayout)inflater.inflate(R.layout.fragement1_layout, container, false);
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) getActivity())
.addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) getActivity())
.addApi(LocationServices.API)
.addApi(Places.GEO_DATA_API)
.build();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onClick(View v) {
if(v==delete){
mAutocompleteView.setText("");
}
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
}
What error does it make when it replaces
this
forgetActivity()
?– ramaral
There is no error just cannot influence the fact that I am not receiving any kind of location suggestions
– Tiago Coelho