そこでプロジェクトを作成しています。融合されたロケーション API を使用して、リサイクル ビュー アダプターで現在のロケーションを検索しています。
アダプターはフラグメント クラスによって使用されます。アダプター カード ビューで融合されたロケーション アクティビティ クラス lang および lat にアクセスする方法を知りたいです。
私はたくさん検索しましたが、適切な ans が見つかりませんでした。私はアンドロイドが初めてです。これが私のtasklistadapter
クラスまたはリサイクルビューアダプターであることを助けてください
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 .....................");
}
}
}