This question has already been answered:
It is necessary to transfer the text from the activity to the fragment.
Under this call method
public void NewsClick(View view) { getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, UniversalListFragment.newInstance("notes.json")).commit(); } did so, but nothing happens.
Here is the fragment code
private static final String TAG_FRAGMENT = "notes.json"; public static UniversalListFragment newInstance(String name) { UniversalListFragment fragment = new UniversalListFragment(); Bundle args = new Bundle(); args.putString(TAG_FRAGMENT, name); fragment.setArguments(args); return fragment; } Explain how to transfer using this method. How to transfer a value from an activation to a fragment , instead of fragment.setArguments (bundle); write UniversalListFragment.setArguments (bundle); . And leave NewsClick as it is? and Bundle bundle = new Bundle (); in oncreate shove
Fragment code
public class UniversalListFragment extends Fragment { private static final String NOTE_TITLE = "title" ; private static final String NOTE_CONTENT = "content" ; private JSONAdapter noteAdapter; private RecyclerView recyclerView; private LinearLayoutManager mLLM; private JSON note; private JSONObject jsonObject; private JSONArray jsonArray; private static final String TAG_FRAGMENT = "notes.json"; public static UniversalListFragment newInstance(String name) { UniversalListFragment fragment = new UniversalListFragment(); Bundle args = new Bundle(); args.putString(TAG_FRAGMENT, name); fragment.setArguments(args); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment final View rv = inflater.inflate( R.layout.fragment_list, container, false); //recyclerview/// noteAdapter = new JSONAdapter(); recyclerView = (RecyclerView) rv.findViewById(R.id.recyclerview); recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST)); mLLM = new LinearLayoutManager(recyclerView.getContext()); recyclerView.setLayoutManager(mLLM); recyclerView.setAdapter(noteAdapter); try { loadNotes(); } catch (JSONException e) { e.printStackTrace(); } AppCompatActivity activity = (AppCompatActivity) getActivity(); ActionBar actionBar = activity.getSupportActionBar(); if(actionBar!= null) { actionBar.setTitle(R.string.start_service); } return rv; } private void loadNotes() throws JSONException { jsonArray = new JSONArray(getJSONString()); for(int i=0; i < jsonArray.length(); i++){ jsonObject = jsonArray.getJSONObject(i); note = new JSON(jsonObject.getString(NOTE_TITLE), jsonObject.getString(NOTE_CONTENT)); noteAdapter.addNote(note); } noteAdapter.notifyDataSetChanged(); } private String getJSONString(){ String json = null; try { InputStream is = getContext().getAssets().open(TAG_FRAGMENT); // go to main/asset directory to see notes.json int size = is.available(); byte[] buffer = new byte[size]; is.read(buffer); is.close(); json = new String(buffer, "UTF-8"); } catch (IOException ex) { ex.printStackTrace(); return null; } return json; } }
String txt = getArguments().getString(TAG_FRAGMENT);- Jarvis_JString strFromArgs = getArguments().getString("notes.json", "defaultValue");- Yuriy SPb ♦