如何将来自recyclerview适配器的字符串值传递给片段

时间:2018-06-07 19:13:12

标签: java android

我想将一个字符串值从我的适配器类传递给我的片段。我尝试将字符串存储在一个包中。要检索我使用Bundle b = getArguments(); b.getString("key")的值,问题是我得到一个空指针异常。下面是将字符串保存在一个包中的代码。所以我的问题是我如何将一个字符串值从adapterA传递给fragmentB。

提前致谢。

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() + "'";
        }


    }   

}

我希望在此片段中获取我在bundle中设置的值。 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;
    }
}

1 个答案:

答案 0 :(得分:0)

从代码中,我可以看到你只在Fragment对象中设置Bundle参数,但没有进一步使用该片段对象。 您需要首先显示该片段对象,然后它将反映到您的目标片段中。