Search code examples
androidandroid-fragmentsarraylistandroid-recyclerviewandroid-bundle

How to Pass String Array from RecyclerView Adapter to Fragment in Android


I am retrieving values from Adapter and storing Values in ArrayList<String>.I am converting ArrayList to String Array and passing the Array from RecyclerView Adapter to Other Fragment, but I am getting null values in other Fragment.
This is code for converting ArrayList<String> into String Array and send to Other Fragment

String []tablesId=new String[tableId.size()];
           tablesId=tableId.toArray(tablesId);
        TableAssignConfirm confirm=new TableAssignConfirm();
           Bundle bundle=new Bundle();
               bundle.putStringArray("tablesIds",tablesId);
               confirm.setArguments(bundle);  

This is code to retrieve the Values from Bundle in Other Fragment

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v=inflater.inflate(R.layout.table_assign_confirm, container, false);
       // Bundle bundle=getArguments().getStringArray("tableIds");
        String []getTables=getArguments().getStringArray("tableIds");
        Log.i("ids", Arrays.toString(getTables));
        return v;
    }  

How to resolve this and get the Values ?


Solution

  • Try this

    Bundle bundle=getArguments();
    if(bundle!=null){
        String []getTables=bundle.getStringArray("tableIds");
     }
    

    in your code

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View v=inflater.inflate(R.layout.table_assign_confirm, container, false);
           Bundle bundle=getArguments();
           if(bundle!=null){
              String []getTables=bundle.getStringArray("tableIds");
            }
            Log.i("ids", Arrays.toString(getTables));
            return v;
        }