-1

我正在開發一個應用程序,我有一個共同的活動,即BaseActivity,我將它擴展到我的應用程序中的所有其他活動。 我在BaseActivity中運行一些AsyncTask以反映所有其他活動。我的查詢是我需要從我的活動之一將值傳遞給此BaseActivity,說MainActivity如何將值從一個活動發送到另一個在android中的擴展活動?

我試過類,接口的概念,但沒有爲我工作。建議我,如何實現這一點。如果我的任何參考代碼需要評論,我會發布它。

EDITED

我需要這個countvalue傳遞給BaseActivity。我不能使用意圖,因爲我導航到另一個活動說MainActivity2

MainActivity 這是我的AsyncTask代碼

public class AsyncHttpTask extends AsyncTask<String, Void, Integer> { 

    @Override 

    protected Integer doInBackground(String... params) { 

     Integer result = 0; 

     try { 

      HttpClient httpclient = new DefaultHttpClient(); 
      HttpResponse httpResponse = httpclient.execute(new HttpGet(params[0])); 
      int statusCode = httpResponse.getStatusLine().getStatusCode(); 
      if (statusCode == 200) { 
       String response = streamToStringCount(httpResponse.getEntity().getContent()); 
       parseResultCount(response); 
       result = 1; 
      } else { 
       result = 0; 
      } 
     } catch (Exception e) { 

      e.printStackTrace(); 
     } 

     return result; 
    } 

    @Override 

    protected void onPostExecute(Integer result) { 

     // isRefreshing = false; 


     //layout.setRefreshing(false); 

     super.onPostExecute(result); 

    } 
} 


String streamToStringCount(InputStream stream) throws IOException { 

    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream)); 
    String line; 
    String result = ""; 
    while ((line = bufferedReader.readLine()) != null) { 

     result += line; 
    } 
    if (null != stream) { 

     stream.close(); 
    } 
    return result; 
} 


private void parseResultCount(String result) { 
    Detail_Item item; 
    try { 
     JSONObject posts = new JSONObject(result); 


     //rstatus1=posts.optString("count"); 
     countValue=posts.optInt("count"); 
     countValue1=countValue; 
     Log.i("countvalue", String.valueOf(countValue)); 

    } 

    catch (JSONException e) { 

     e.printStackTrace(); 

    } 
} 
+0

在基本活動中需要哪種類型的值? –

+0

你可以顯示你的代碼你試過了嗎? – Masum

+0

我從** MainActivity **獲取幾個整數值,並需要將這些整數值傳遞給** BaseActivity ** –

回答

0

按我的理解,你想通過從MainActivity一些值(將BaseActivity擴展到BaseActivity)。

在BaseActivity像剛纔創建的方法,

protected void setYourValue(Object obj){ 
    .......... 
} 

從您的MainActivity只是調用該方法,並設置任何你想要的。

setYourValue("This is my value"); 

如果在MainAcitivity不是從BaseActivity擴展,

public static void setYourValue(Object obj){ 
    .......... 
} 

在MainActivity,

BaseActivity.setYourValue("This is my value"); 

請不要在實施sharedpreferrence一些研究。

+0

我沒有在MainActivity中擴展BaseActivity,所以我不能直接訪問'setYourValue',對嗎? –

+0

好吧,如果是這樣,那麼沒有其他辦法我猜,你需要在BaseActivity或SharedPreference中創建一個靜態方法。 – Srinivasan

+0

這很好。我猜SharedPreference會做... –

1

將值存儲在SharedPreferences中,並從應用程序中的任何活動中檢索它們。在新的活動

Intent i = new Intent(CurrentActivity.this, NewActivity.class); 
i.putExtra("new_variable_name","value"); 
startActivity(i); 

然後檢索這些值:

+0

好的建議。我會嘗試並讓你知道。 –

0

在你當前活動,創建一個新的意向

Bundle extras = getIntent().getExtras(); 
if (extras != null) { 
    String value = extras.getString("new_variable_name"); 
} 

Refer Link

0

,你可以做到這一點通過定義公共靜態的,因爲所有的這個或者子類的實例將會共享它。這是我不推薦的非常難看的解決方案。它更好地使用Extras

Intent intent = new Intent(context,SecondActivity.class); 意圖。putExtra(「Key」,Value); startActivity(意向)

更多信息請諮詢文件https://developer.android.com/guide/components/intents-filters.html

1

在你的AsyncTask 定義

public interface ResultListener { 
    void onResultReady(Integer result); 
} 

創建構造函數接受ResultListener參數

private ResultListener mRl = null; 
public AsyncHttpTask (ResultListener rl) { 
    mRl=rl; 
} 

在postExecute(它運行在主線程,你知道)來電聽衆

protected void onPostExecute(Integer result) { 
     super.onPostExecute(result); 
     if (mRl!=null) 
      mRl.onResultReady(result); 
} 

現在,如果你有一些活動,它實現ResultListener 你需要將結果傳遞一個參考,你開始任務是這樣的:

AsyncHttpTask.ResultListener myActivity ; 
... 
(new AsyncHttpTask(myActivity)).execute(myParameters); 

你現在的問題歸結爲如何你的活動/片段開始和協調,以便您可以在適當的地方得到適當的參考。

相關問題