-1

I want to pass a string value from my adapter class to my fragment. I tried storing the string in a bundle. To retrieve the value i used Bundle b = getArguments(); b.getString("key") the problem is im getting a null pointer exception. Below is the code that saves the string in a bundle. So my question is how can i pass a string value from adapterA to fragmentB.

Thanks in advance.

Adapter.java

public class ToDoRecyclerViewAdapter extends RecyclerView.Adapter<ToDoRecyclerViewAdapter.ViewHolder> {
    private Context context;
    private  List<Aktivnost_> mValues;
    private final OnListFragmentInteractionListener mListener;
    public ToDoRecyclerViewAdapter td;


    public ToDoRecyclerViewAdapter(List<Aktivnost_ > items, Context context,  OnListFragmentInteractionListener listener) {
        mValues = items;
        mListener = listener;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.fragment_todo, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        holder.mItem = mValues.get(position);
        holder.mContentView.setText(mValues.get(position).getNaziv());
        holder.mDateView.setText(mValues.get(position).getDatum());
        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mListener) {
                    mListener.onListFragmentInteraction(holder.mItem);
                    Intent i = new Intent(context.getApplicationContext(), PodrobnostiActivity.class);
                    i.putExtra("task_id",  mValues.get(position).getId_());
                    context.getApplicationContext().startActivity(i);
                    Toast.makeText(v.getContext(), "task - " + mValues.get(position).getId_(), Toast.LENGTH_SHORT).show();
                }
            }
        });

        holder.mView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(final View v) {
                AlertDialog.Builder adb = new AlertDialog.Builder(v.getContext());
                CharSequence meni[] = new CharSequence[] {"DOING", "FINISHED"};
                adb.setItems(meni, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if(i == 0) {
                            Bundle b = new Bundle();
                            DoingFragment d = new DoingFragment();
                            mValues.get(i).setStanje("doing");
                            b.putString("doing", mValues.get(i).getStanje());
                            d.setArguments(b);
                        } else {
                            mValues.get(i).setStanje("koncano");
                        }
                    }
                });
                AlertDialog alertDialog = adb.create();
                alertDialog.setCancelable(true);
                alertDialog.show();
                return true;
            }
        });
    }


    @Override
    public int getItemCount() {
        return mValues.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView mContentView;
        public final TextView mDateView;
        public long id;
        public Aktivnost_ mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            this.id = id;
            mDateView = (TextView) view.findViewById(R.id.Date);
            mContentView = (TextView) view.findViewById(R.id.content);
        }

        @Override
        public String toString() {
            return super.toString() + " '" + mContentView.getText() + "'";
        }


    }   

}

And i want to get the value i set in bundle in this fragment. Fragment.java

public class DoingFragment extends Fragment {

    DoingFragmentRecyclerViewAdapter mAdapter;
    private OnListFragmentInteractionListener mListener;

    public DoingFragment() {
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_doingfragment_list, container, false);

        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.list_doing);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));

        mAdapter = new DoingFragmentRecyclerViewAdapter(listAktivnosti(),mListener);
        recyclerView.setAdapter(mAdapter);


        return view;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnListFragmentInteractionListener) {
            mListener = (OnListFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnListFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }




    public interface OnListFragmentInteractionListener {
        void onListFragmentInteraction1(Aktivnost_ item);
    }

    AppDatabase db;
    public void openDB() {
        db = new AppDatabase(getContext());
        db.open();
    }
    Aktivnost_ ak;
    List<Aktivnost_> array;
    public List<Aktivnost_> listAktivnosti() {
        array  = new ArrayList<>();
        openDB();
        Bundle b = getArguments();
        Cursor cursor = db.getAllRows(b.getString("doing"));
        while(cursor.moveToNext()) {
            ak = new Aktivnost_();
            ak.setId_(cursor.getLong(cursor.getColumnIndex("_id")));
            ak.setNaziv(cursor.getString(cursor.getColumnIndex("naziv")));
            ak.setDatum(cursor.getString(cursor.getColumnIndex("datum")));
            ak.setFk_projekt(cursor.getInt(cursor.getColumnIndex("fk_projekt")));
            ak.setUdeleženci(cursor.getString(cursor.getColumnIndex("udelezenci")));
            ak.setStanje(cursor.getString(cursor.getColumnIndex("stanje")));
            array.add(ak);
        }


        return array;
    }
}
Odyssey
  • 25
  • 5
  • take a look at this: https://stackoverflow.com/questions/49574358/passing-values-from-recycler-adapter-to-fragment-in-android – Zombie Jun 07 '18 at 19:51
  • Thanks for the link it actually helped me solve another problem i was having, my question is can i do the same with OnLongClickListener? – Odyssey Jun 07 '18 at 20:30
  • you mean passing string to fragment onLongClickListener ? – Zombie Jun 07 '18 at 20:41
  • Yes, like its shown in that post but with onLongClickListener. – Odyssey Jun 07 '18 at 21:08
  • Just implement RecyclerViewAdapter.OnItemClickListener and RecyclerViewAdapter.OnItemLongClickListener in your activity or fragment, and override respective interfaces methods – Zombie Jun 07 '18 at 21:23

1 Answers1

0

From the code, I can see you are only setting the Bundle parameters in Fragment object, but not using that fragment object further. You need to display that fragment object first, then it will reflect into your target fragment.

Gaurav Bansal
  • 604
  • 5
  • 11