CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Building for mobile devices (http://www.codingforums.com/forumdisplay.php?f=70)
-   -   Sending a string variable from an Activity to a Fragment of that Activity (http://www.codingforums.com/showthread.php?t=256148)

Leoa 04-05-2012 12:09 PM

Sending a string variable from an Activity to a Fragment of that Activity
 
I'm new at Android and I'm having some trouble with sending a string variable from an Activity to a Fragment of that Activity.

I've set it up using intents, but I get an error that says the Fragment can not be cast to type Activity. So I researched and found some information on FragmentManager. The examples I found showed Fragments communication with other Fragments. This is not the solution I'm looking for. I need to send a string variable to a Fragment. In the onPostExecute() method I want to sent the result to a list fragment. Do you know how to achieve this or any example that I can follow? Please help. Thanks. My code is below:
Code:


package com.leobee;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.android.maps.MapActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class AsyncTasksActivity extends MapActivity {

        LocationManager locationManager;

          String stxtLat ;
          String stxtLong;
          double pLong;
          double pLat;

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

        // Acquire a reference to the system Location Manager
          locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

          // Define a listener that responds to location updates
          LocationListener locationListener = new LocationListener() {
              public void onLocationChanged(Location location) {
                  pLong=location.getLongitude();
                  pLat=location.getLatitude();
                  stxtLat=Double.toString(pLat);
                  stxtLong=Double.toString(pLong);
                  Toast.makeText(AsyncTasksActivity.this, stxtLong, Toast.LENGTH_SHORT).show();
                  Toast.makeText(AsyncTasksActivity.this, stxtLat, Toast.LENGTH_SHORT).show();


              }

              public void onStatusChanged(String provider, int status, Bundle extras) {}

              public void onProviderEnabled(String provider) {}

              public void onProviderDisabled(String provider) {}

            };


          // Register the listener with the Location Manager to receive location updates
          locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
          DownloadWebPageTask task = new DownloadWebPageTask();

          double numRand = Math.floor(Math.random()*1000);
            String userLat= stxtLat;
            String userLong= stxtLong;
            task.execute(new String[] { "http://www.leobee.com/otakufinder/scripts/geoloco.php?userLat="+userLat+"&userLong="+userLong+"&randNum="+numRand });

        }

        private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... urls) {
                String response = "";
                for (String url : urls) {
                    DefaultHttpClient client = new DefaultHttpClient();
                    //HttpGet myGet = new HttpGet("http://foo.com/someservlet?param1=foo&param2=bar");

                    HttpGet httpGet = new HttpGet(url);
                    try {
                        HttpResponse execute = client.execute(httpGet);
                        InputStream content = execute.getEntity().getContent();

                        BufferedReader buffer = new BufferedReader(
                                new InputStreamReader(content));
                        String s = "";
                        while ((s = buffer.readLine()) != null) {
                            response += s;
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                return response;
            }


        @Override
        protected void onPostExecute(String result) {

                String x =result;
                    // send vars thru bundle (basket)
                Bundle basket = new Bundle();
                basket.putString("key", x);
                Intent openClass = new Intent(AsyncTasksActivity.this, ListFragment.class);
                openClass.putExtras(basket);
                startActivity(openClass);

                Log.v("values",x);

            }

        }


        @Override
        protected boolean isRouteDisplayed() {
            // TODO Auto-generated method stub
            return false;
        }
}


averycoolperson 08-13-2012 06:20 PM

Why is it called a string variable?


All times are GMT +1. The time now is 01:16 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.