2012-04-05 60 views
0

我想弄清楚如何從活動類中的方法獲取值到該活動的片段中。 我已經設置了三個文件:AsyncTaskActivity.java,EventsList.java和ListFragment.java。 AsyncTaskActivity.java具有返回字符串值的方法getOtakuEvents()。返回從活動到一個片段的字符串值

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 implements EventsList{ 

     LocationManager locationManager; 

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

     @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) { 

      x =result; 


       Log.v("values",x); 

      } 

     } 


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

EventsList.java是一個接口,幫助類知道getOtakuEvents()值是否可用。

package com.leobee; 

public interface EventsList { 

    String getOtakuEvents(); 

} 

最後,所述片段具有來自getOtakuEvents得到值()的方法稱爲getStringfromActivity()。

package com.leobee; 

    import android.content.Intent; 
    import android.content.res.Configuration; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.view.View; 
    import android.widget.ArrayAdapter; 
    import android.widget.ListView; 

    // shows list view of items if fragment is not null this class will also show the item selected form Detailfragment class 
    public class ListFragment extends android.app.ListFragment{ 
    String events; 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      String events =getStringfromActivity(); 

     } 
public String getStringfromActivity() { 
     String i; 
     i=EventsList.getOtakuEvents(); 
      return i; 
     } 



    /* public String getStringfromActivity() { 

     return getActivity().getOtakuEvents(); 

     }*/ 


     @Override 
     public void onActivityCreated(Bundle savedInstanceState){ 

      super.onActivityCreated(savedInstanceState); 


     String[] values = new String[] { "Android", "iPhone", "WindowsMobile", 
       "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", 
       "Linux", "OS/2" }; 

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,values); 
      setListAdapter(adapter); 

     } 

     @Override 
     public void onListItemClick(ListView l, View v, int position, long id){ 

      String item =(String)getListAdapter().getItem(position); 
      DetailFragment fragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.detailFragment); 


      if (fragment != null && fragment.isInLayout()){ 

      fragment.setText(item); 
      }else{Intent intent = new Intent(getActivity().getApplicationContext(), 
        DetailActivity.class); 
      intent.putExtra("value", item); 
      startActivity(intent); 

      } 


     } 
     @Override 
     public void onConfigurationChanged(Configuration newConfig) { 
      super.onConfigurationChanged(newConfig); 
      Log.e("text","config change detail fragment"); 
      // Checks the orientation of the screen 

     } 




    } 

我收到錯誤:無法對類型爲EventsList的非靜態方法getOtakuEvents()進行靜態引用。 這很令人困惑,因爲我沒有在getOtakuEvents()或片段中聲明靜態類型。

可替換地,我也嘗試該型式的方法中的片段:

public String getStringfromActivity() { 

     return getActivity().getOtakuEvents(); 

     } 

我正在一個錯誤:該方法getOtakuEvents()是未定義的類型的活動。這對我來說是莫名其妙的,因爲方法是在父活動中定義的

具體而言,我需要能夠將字符串值從活動發送到片段。我正在嘗試使用接口或getActivity方法。你可以看看我的代碼,讓我知道我要去哪裏錯了,以及如何解決它?我已經在2天的大部分時間裏看到了這一點,似乎無法解決這個問題。任何幫助表示讚賞。

回答

0

您可以從SharedPreferences中存儲和檢索它

+0

謝謝!我沒有想到這個方向,但它的工作。 – Leoa 2012-04-06 11:34:02

相關問題