0 Replies - 1170 Views - Last Post: 20 August 2013 - 04:36 PM

#1 RUAg4mer   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 30-July 11

From Activity to Fragments

Posted 20 August 2013 - 04:36 PM

Hello again. I'm back. This time with a new problem. I've gotten my JSON parsed and showing up in a Listview. Now I'm interested in having the information displayed in fragments. I want the names in one fragment and the details in a second fragment. With both fragments on the same screen. My question is HOW do I go about doing this. What I've found online so far and tried completely breaks my code..in a lot of ways. Not just one or two errors..but errors beyond the point of correcting. The code simply won't function at all after attempting to convert it to use fragments. I'll post the code again. Nevermind any errors you see in the code..It's old code from before I got everything working correctly. I do not have access to the new code so I'll just post this, and if someone could give me some advice to convert it to use fragments, that would be great.

// JSON Node names
	private static final String TAG_IDENTIFIED = "id";
	private static final String TAG_INFORMATION = "information";
	private static final String TAG_STATE = "state";
	private static final String TAG_TYPE = "type";
	private static final String TAG_ADDITIONALINFO = "additionalinfo";
	private static final String TAG_OUTDOORFUN = "outdoorfun";
	private static final String TAG_COLD = "cold";
	

	// contacts JSONArray
	JSONArray information = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		// Hashmap for ListView
		ArrayList<HashMap<String, String>> stateList = new ArrayList<HashMap<String, String>>();

		// Creating JSON Parser instance
		JSONParser jParser = new JSONParser();

		// getting JSON string from URL
		JSONObject json = jParser.getJSONFromUrl(url);
			
			try{ 
			
			
			
			for(int i = 0; i < information.length(); i++){
				information =json.getJSONArray(TAG_INFORMATION);
				
				JSONObject c = information.getJSONObject(i);
				
				// Storing each json item in variable
				String name = c.getString(TAG_STATE);
				String type = c.getString(TAG_TYPE);
				
				// Phone number is agin JSON Object
				JSONObject additionalinfo = c.getJSONObject(ADDITIONALINFO);
				String outdoorfun = c.getString(TAG_OUTDOORFUN);
				String cold = c.getString(TAG_COLD);
				
				
				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();
				
				// adding each child node to HashMap key => value
				map.put(TAG_NAME, name);
				map.put(TAG_TYPE, type);
				map.put(TAG_OUTDOORFUN, outdoorfun);
				

				
				
				informationList.add(map);
			}
			}
		 catch (JSONException e) {
			e.printStackTrace();
		}
		
		
		/**
		 * Updating parsed JSON data into ListView
		 * */
		ListAdapter adapter = new SimpleAdapter(this, informationList,
				R.layout.list_item,
				new String[] {TAG_NAME, TAG_TYPE, TAG_OUTDOORFUN }, new int[] {
						R.id.name, R.id.type, R.id.outdoorfun });

		setListAdapter(adapter);

		// selecting single ListView item
		ListView lv = getListView();

		// Launching new screen on Selecting Single ListItem
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// getting values from selected ListItem
				String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
				String type = ((TextView) view.findViewById(R.id.type)).getText().toString();
				String outdoorfun = ((TextView) view.findViewById(R.id.outdoorfun)).getText().toString();
				
				
				
				
				// Starting new intent
				Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
				in.putExtra(TAG_NAME, name);
				in.putExtra(TAG_TYPE, type);
				in.putExtra(TAG_OUTDOORFUN, outdoorfun);
				startActivity(in);

			}
		});
		
		





package com.androidhive.jsonparsing;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SingleMenuItemActivity  extends Activity {
	
	// JSON node keys
	
	private static final String TAG_ID = "id";
	private static final String TAG_INFORMATION = "information";
	private static final String TAG_STATE = "state";
	private static final String TAG_TYPE = "type";
	private static final String TAG_ADDITIONALINFO = "additionalinfo";
	private static final String TAG_OUTDOORFUN = "outdoorfun";
	private static final String TAG_COLD = "cold";
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.single_list_item);
        
        // getting intent data
        Intent in = getIntent();
        
        // Get JSON values from previous intent
        String name = in.getStringExtra(TAG_NAME);
        String type = in.getStringExtra(TAG_TYPE);
        String outdoorfun = in.getStringExtra(TAG_OUTDOORFUN);
        
        // Displaying all values on the screen
        TextView lblName = (TextView) findViewById(R.id.name_label);
        TextView lblType = (TextView) findViewById(R.id.type_label);
        TextView lblOutdoorfun = (TextView) findViewById(R.id.outdoorfun_label);
        
        lblName.setText(name);
        lblType.setText(type);
        lblOutdoorfun.setText(outdoorfun);
    }
}





Hope there's enough info here for someone to help, if not let me know what else is needed. I'm thinking there is a really easy solution to this. I've tried just adding a couple fragments through XML and using there id's instead of the listview ID. but it didn't work. Any advice?

Is This A Good Question/Topic? 0
  • +

Page 1 of 1