M
M
MaximZeroeff2015-07-01 11:27:46
Android
MaximZeroeff, 2015-07-01 11:27:46

How to put the results of the parser into fragments?

Hello Toster community!
Please don't hit me hard. I recently started learning Android programming and ran into this problem.
I can’t figure out how to put the results of the parser into the fragments I need.
I have this json parser:

import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.widget.ListView;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    
    
    public class MainActivity extends Activity {
    	// Declare Variables
    	JSONObject jsonobject;
    	JSONArray jsonarray;
    	ListView listview;
    	ListViewAdapter adapter;
    	ProgressDialog mProgressDialog;
    	ArrayList<HashMap<String, String>> arraylist;
    	static String TITLE = "title";
    	static String SHORT_STORY = "short_story";
    	static String CENA = "cena";
    	static String SKIDKA = "skidka";
    	static String CATEGORY = "category";
    	static String IMAGE = "image";
    
    
    
    
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		// Get the view from listview_main.xml
    		setContentView(R.layout.listview_main);
    		// Execute DownloadJSON AsyncTask
    		new DownloadJSON().execute();
    	}
    
    
    
    
    	// DownloadJSON AsyncTask
    	private class DownloadJSON extends AsyncTask<Void, Void, Void> {
    
    		@Override
    		protected void onPreExecute() {
    			super.onPreExecute();
    			// Create a progressdialog
    			mProgressDialog = new ProgressDialog(MainActivity.this);
    			// Set progressdialog title
    			mProgressDialog.setTitle("Android JSON Parse Tutorial");
    			// Set progressdialog message
    			mProgressDialog.setMessage("Loading...");
    			mProgressDialog.setIndeterminate(false);
    			// Show progressdialog
    			mProgressDialog.show();
    		}
    
    		@Override
    		protected Void doInBackground(Void... params) {
    			// Create an array
    			arraylist = new ArrayList<HashMap<String, String>>();
    			// Retrieve JSON Objects from the given URL address
    			jsonobject = JSONfunctions.getJSONfromURL("http://raccoon-media.ru/json.json");
    
    			try {
    				// Locate the array name in JSON
    				jsonarray = jsonobject.getJSONArray("prod");
    
    				for (int i = 0; i < jsonarray.length(); i++) {
    					HashMap<String, String> map = new HashMap<String, String>();
    					jsonobject = jsonarray.getJSONObject(i);
    					// Retrive JSON Objects
    					map.put("title", jsonobject.getString("title"));
    					map.put("short_story", jsonobject.getString("short_story"));
    					map.put("cena", jsonobject.getString("cena"));
    					map.put("skidka", jsonobject.getString("skidka"));
    					map.put("image", jsonobject.getString("image"));
    					map.put("category", jsonobject.getString("category"));
    					// Set the JSON Objects into the array
    					arraylist.add(map);
    				}
    			} catch (JSONException e) {
    				Log.e("Error", e.getMessage());
    				e.printStackTrace();
    			}
    			return null;
    		}
    
    		@Override
    		protected void onPostExecute(Void args) {
    			// Locate the listview in listview_main.xml
    			listview = (ListView) findViewById(R.id.listview);
    			// Pass the results into ListViewAdapter.java
    			adapter = new ListViewAdapter(MainActivity.this, arraylist);
    			// Set the adapter to the ListView
    			listview.setAdapter(adapter);
    			// Close the progressdialog
    			mProgressDialog.dismiss();
    		}
    	}
    
    
    	@Override
    	public boolean onCreateOptionsMenu(Menu menu) {
    		// Inflate the menu;
    		MenuInflater inflater = getMenuInflater();
    		inflater.inflate(R.menu.activity_main, menu);
    		return super.onCreateOptionsMenu(menu);
    	}
    }

And there is a main activity (made as described here habrahabr.ru/post/236567/) with tabs through SlidingTabLayout and SlidingTabStrip, where each tab is a fragment.
How to push the results of this parser into one of the tabs, another parser into another tab, etc. ?
Thank you in advance!
PS
Answer found, see comments.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaximZeroeff, 2015-07-01
@MaximZeroeff

It turns out that this is very easy to do.
In the fragment, inside onViewCreated:
just execute the desired function like this: new DownloadJSON().execute();
Before that, place the entire code of this function together with the fragment code, but change
MainActivity.this (in 2 places) to getActivity().
But there was another problem:
How to make new DownloadJSON().execute() run only once?
Now it is executed every time the fragment is called.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question