我在那裏製作項目我正在使用融合位置API在回收視圖適配器中查找當前位置。
適配器由片段類使用,我想知道如何在我的適配器卡視圖中訪問我的融合位置活動類lang和lat。
我搜索了很多,但我沒有找到任何合適的答,我是在Android的新請幫助 這是我的tasklistadapter
類或回收視圖適配器我正在使用融合的位置API來查找回收視圖適配器中的當前位置,適配器正在使用的片段類
public class TaskListAdapter extends RecyclerView.Adapter<TaskListAdapter.ViewHolder> {
String setdata;
LocationActivity mlocationAct;
Context mContext;
private class User {
public String name;
public String party;
public String city;
public String country;
public String identity;
public int image;
public int imagePrty;
public User(int image, int imageParty, String name, String city, String party, String country,String identity) {
this.image = image;
this.imagePrty = imageParty;
this.identity = identity;
this.name = name;
this.city = city;
this.party = party;
this.country = country;
}
}
private ArrayList<User> mRecyclerData;
public TaskListAdapter(Context mContext) {
mRecyclerData = new ArrayList<>();
mContext = mContext.getApplicationContext();
mlocationAct = new LocationActivity(mContext);
//虛擬數據
mRecyclerData.add(new User(R.drawable.mp,R.drawable.bjplogo,"D. V. Sadananda Gowda", "MP", "Bjp", "India","MP"));
mRecyclerData.add(new User(R.drawable.mla,R.drawable.congresslogo,"Akhanda Srinivas Murthy R", "MLA", "Congres", "India","MLA"));
@Override
public void onBindViewHolder(final ViewHolder holder, final int i) {
final Context context = holder.titleView.getContext();
User user = mRecyclerData.get(i);
holder.titleView.setText(user.name);
//這裏我想顯示來自融合位置活動類的數據。
holder.tvState.setText(mlocationAct.getStateName(context));
@Override
public int getItemCount() {
return (mRecyclerData != null) ? mRecyclerData.size() : 0;
}
static class ViewHolder extends RecyclerView.ViewHolder {
RadioGroup radioGroup;
CardView cardView;
String setdata;
TextView titleView, tvConnect, cityView, countryView, partyView, tvResult,tvAdd,tvState,idProfsn;
ImageView imageView,imagelgo ;
public ViewHolder(CardView card) {
super(card);
cardView = card;
titleView = (TextView) card.findViewById(R.id.tvName);
imageView = (ImageView) card.findViewById(R.id.image);
cityView = (TextView) card.findViewById(R.id.tvCity);
countryView = (TextView) card.findViewById(R.id.tvCountry);
partyView = (TextView) card.findViewById(R.id.tvParty);
tvConnect = (TextView) card.findViewById(R.id.tvConnect);
radioGroup = (RadioGroup) card.findViewById(R.id.radioG);
tvResult = (TextView) card.findViewById(R.id.tvResult);
tvAdd=(TextView)card.findViewById(R.id.tvAdd);
tvState=(TextView)card.findViewById(R.id.tvState);
idProfsn=(TextView)card.findViewById(R.id.tvId);
imagelgo=(ImageView)card.findViewById(R.id.imageLogo);
}
}
}
這是正在使用該適配器我的片段類
public class FragmentA extends Fragment {
private static final String TAG = "LocationActivity";
private static final long INTERVAL = 1000 * 10;
private static final long FASTEST_INTERVAL = 1000 * 5;
Button btnFusedLocation;
TextView tvLocation;
GoogleApiClient mGoogleApiClient;
String mLastUpdateTime;
RecyclerView recyclerView;
TaskListAdapter taskListAdapter;
Context mContext;
private List<Data> mData = new ArrayList<>();
public FragmentA() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = getActivity();
taskListAdapter = new TaskListAdapter(mContext);
LocationActivity locationActivity = new LocationActivity(mContext);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View v = inflater.inflate(R.layout.fragment_a, container, false);
recyclerView = (RecyclerView) v.findViewById(R.id.recycler);
recyclerView.setAdapter(taskListAdapter);
recyclerView.setHasFixedSize(true);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(linearLayoutManager);
return v;
}
}
最後,這是我的融合位置活動類
public class LocationActivity extends Activity implements LocationListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener{
double latitude;
double longitude;
private static final String TAG = "LocationActivity";
private static final long INTERVAL = 1000 * 10;
private static final long FASTEST_INTERVAL = 1000 * 5;
Button btnFusedLocation;
TextView tvLocation;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mCurrentLocation;
String mLastUpdateTime;
String lat;
Context mContext;
public LocationActivity(Context mContext) {
this.mContext = mContext;
updateUI();
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate ...............................");
//show error dialog if GoolglePlayServices not available
if (!isGooglePlayServicesAvailable()) {
finish();
}
createLocationRequest();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
setContentView(R.layout.activity_main);
// tvLocation = (TextView) findViewById(R.id.tvLocation);
// btnFusedLocation = (Button) findViewById(R.id.btnShowLocation);
btnFusedLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
updateUI();
}
});
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart fired ..............");
mGoogleApiClient.connect();
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "onStop fired ..............");
mGoogleApiClient.disconnect();
Log.d(TAG, "isConnected ...............: " + mGoogleApiClient.isConnected());
}
private boolean isGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == status) {
return true;
} else {
GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
return false;
}
}
@Override
public void onConnected(Bundle bundle) {
Log.d(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
startLocationUpdates();
}
protected void startLocationUpdates() {
PendingResult<Status> pendingResult = LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
Log.d(TAG, "Location update started ..............: ");
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d(TAG, "Connection failed: " + connectionResult.toString());
}
@Override
public void onLocationChanged(Location location) {
Log.d(TAG, "Firing onLocationChanged..............................................");
mCurrentLocation = location;
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
updateUI();
}
private void updateUI() {
Log.d(TAG, "UI update initiated .............");
if (null != mCurrentLocation) {
latitude = mCurrentLocation.getLatitude();
longitude = mCurrentLocation.getLongitude();
tvLocation.setText("At Time: " + mLastUpdateTime + "\n" +
"Latitude: " + latitude + "\n" +
"Longitude: " + longitude+ "\n" +
"Accuracy: " + mCurrentLocation.getAccuracy() + "\n" +
"Provider: " + mCurrentLocation.getProvider());
} else {
Log.d(TAG, "location is null ...............");
}
}
public List<Address> getGeocoderAddress(Context context)
{
if (mCurrentLocation != null)
{
Geocoder geocoder = new Geocoder(context, Locale.ENGLISH);
try
{
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
return addresses;
}
catch (IOException e)
{
//e.printStackTrace();
Log.e("Error : Geocoder", "Impossible to connect to Geocoder", e);
}
}
return null;
}
public String getStateName(Context context) {
List<Address> addresses = getGeocoderAddress(context);
if (addresses != null && addresses.size() > 0) {
Address address = addresses.get(0);
String state = address.getAdminArea();
return state;
} else {
return null;
}
}
public double getLatitude() {
if(mCurrentLocation != null) {
latitude = mCurrentLocation.getLatitude();
}
return latitude;
}
public double getLongitude(){
if(mCurrentLocation !=null){
longitude = mCurrentLocation.getLongitude();
}
return longitude;
}
@Override
protected void onPause() {
super.onPause();
stopLocationUpdates();
}
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
Log.d(TAG, "Location update stopped .......................");
}
@Override
public void onResume() {
super.onResume();
if (mGoogleApiClient.isConnected()) {
startLocationUpdates();
Log.d(TAG, "Location update resumed .....................");
}
}
}