0

我正在那里制作一个项目,我正在使用融合位置 API 在回收视图适配器中查找当前位置。
适配器由片段类使用,我想知道如何在适配器卡视图中访问我的融合位置活动类 lang 和 lat。
我搜索了很多,但我没有找到任何合适的 ans,我是 android 新手,请帮助这是我的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 .....................");
        }
    }
}
4

1 回答 1

1

如果您需要 ViewHolder 中的 Activity 实例,请在此处执行:in Fragment: TaskListAdapter(mContext) change on TaskListAdapter(Fragment fragment)

在任务列表适配器中: public void TaskListAdapter(Fragment fragment) { mActivity = (LocationActivity) fragment.getActivity(); }

覆盖方法 onCreate ViewHolder,像这样 @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.your_layout, viewGroup, false); rerurn new ViewHolder(view, mActivity); }

在 View ViewHolder 中更改:public ViewHolder(CardView card) {} on public ViewHolder(CardView card, LocationActivity activity) {}

于 2016-02-12T08:00:32.620 回答