将一个变量从recyclerview适配器传递给fragment

tnkciper  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(363)

这个问题在这里已经有答案了

什么是nullpointerexception,如何修复它(12个答案)
昨天关门了。
我正在尝试将一个基于回收器视图中用户选择的整数传递给另一个名为mapfragment的片段(walksfragment),但我不确定如何做。我尝试过使用一个bundle,但是我得到了一个空指针异常,一旦应用程序打开,mapfragment将是用户看到的第一个片段,可以选择转到walksfragment,然后选择一个选项,该选项应该将数据传递回mapfragment。这是可能的,我应该使用不同的方法来传输数据完全,任何帮助将不胜感激

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference

我理解为什么它会给出一个空指针的exeption,但是我不确定如何从recycler视图中使用bundle传递数据,但是仍然能够在传递任何数据之前首先调用mapfragment
这是MyAdapter类

package com.ouseburn.ouseburntrust.ui.walks;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.ouseburn.ouseburntrust.R;
import com.ouseburn.ouseburntrust.ui.map.MapFragment;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    String s1[], s2[];
    int img[];
    Context context;

    public MyAdapter(Context ct, String walksTitles[], String walksInfo[], int images[]){
        context = ct;
        s1 = walksTitles;
        s2 = walksInfo;
        img = images;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.my_row, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.textTitle.setText(s1[position]);
        holder.textInfo.setText(s2[position]);
        holder.imageView.setImageResource(img[position]);
        holder.mainLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putInt("walk", position);
                Fragment fragment = new MapFragment();
                fragment.setArguments(bundle);
            }
        });
    }

    @Override
    public int getItemCount() {
        return s1.length;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        TextView textTitle, textInfo;
        ImageView imageView;
        ConstraintLayout mainLayout;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textTitle = itemView.findViewById(R.id.textTitle);
            textInfo = itemView.findViewById(R.id.textInfo);
            imageView = itemView.findViewById(R.id.imageView);
            mainLayout = itemView.findViewById(R.id.mainLayout);
        }
    }

}

这是mapfragment片段中数据应该到达的部分

public class MapFragment extends Fragment {

    private com.ouseburn.ouseburntrust.ui.GeneralViewModel generalViewModel;
    private boolean granted;
    private MapView mapView;
    private GoogleMap googleMap;
    MapView mMapView;
    int temp;
    private FusedLocationProviderClient fusedLocationClient;
    LatLng userLocation = new LatLng(54.974814, -1.592351);
    ;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_map, container, false);

        mMapView = (MapView) rootView.findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);

        mMapView.onResume(); // needed to get the map to display immediately

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        mMapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap mMap) {
                googleMap = mMap;

                checkPermission();
                if (granted) {
                    if (checkGooglePlayServices()) {

                        // For showing a move to my location button
                        googleMap.setMyLocationEnabled(true);

                        try {
                            int chosenWalk = getArguments().getInt("walk");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
e3bfsja2

e3bfsja21#

首先,在适配器中创建一个接口

public interface OnItemClickListener {
  void onItemClicked(int position, Object object);//
}

在你的电话里

Adapter adapter = new Adapter(context, list, new Adapter.OnItemClickListener() {
            @Override
            public void onItemClicked(int position, Object object) {
                  // Handle Object of list item here
            }
        });

在你的适配器里 private OnItemClickListener onItemClickListener; 在构造函数调用中
this.onitemclicklistener=onitemclicklistener;
最后在你的项目点击事件

itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      onItemClickListener.onItemClicked(position, _list.get(position));
    }
});

相关问题