Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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;
            }
    }

  • #2
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why is it called a string variable?


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •