Question: What should i add to pass a bundle with name, height and mass to a fragment? What should the fragment class look like to receive

What should i add to pass a bundle with "name", "height" and "mass" to a fragment? What should the fragment class look like to receive that data?

Here is my MainActivity.java

public class MainActivity extends AppCompatActivity { private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView); JsonWork jsonWork = new JsonWork(); jsonWork.execute(); } private class JsonWork extends AsyncTask>{ HttpURLConnection httpURLConnection = null; BufferedReader bufferedReader = null; String FullJsonData; @Override protected List doInBackground(String... strings) { try { URL url = new URL("https://swapi.dev/api/people/?format=json"); httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.connect(); InputStream inputStream = httpURLConnection.getInputStream(); bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); StringBuffer stringBuffer= new StringBuffer(); String line=""; while ((line= bufferedReader.readLine())!= null){ stringBuffer.append(line); } FullJsonData = stringBuffer.toString(); List jsonModelList = new ArrayList<>(); JSONObject jsonStartingObject = new JSONObject(FullJsonData); JSONArray jsonSWArray = jsonStartingObject.getJSONArray("results"); for(int i=0; i jsonModels) { super.onPostExecute(jsonModels); CustomAdapter adapter = new CustomAdapter(getApplicationContext(), jsonModels); listView.setAdapter(adapter); boolean isTablet = findViewById(R.id.fragmentLocation) != null; listView.setOnItemClickListener((list, item, position, id) -> { //Add a bundle to pass data to the new fragment if(isTablet) { DetailsFragment dFragment = new DetailsFragment(); //add a DetailFragment dFragment.setArguments(??); //pass it a bundle for information getSupportFragmentManager() .beginTransaction() .replace(R.id.fragmentLocation, dFragment) //Add the fragment in FrameLayout .commit(); //actually load the fragment. } else //isPhone { Intent nextActivity = new Intent(MainActivity.this, EmptyActivity.class); nextActivity.putExtras(??); //send data to next activity startActivity(nextActivity); //make the transition } }); } } }

and my JsonModel.java

public class JsonModel { private String name; private String height; private String mass; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getHeight() { return height; } public void setHeight(String height) { this.height = height; } public String getMass() { return mass; } public void setMass(String mass) { this.mass = mass; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!