2012-06-19 93 views
0

我是android的新手,在我的android應用中,主要的Activity類,其中包含TextView用於顯示來自其他類的各種狀態消息。我想更新主ActivityTextView與其他類的狀態值。主要活動類和其他類沒有直接聯繫。在android中可能嗎?如果是的話,我不知道要這樣做。請提供解決方案做如何在主活動中的TextView上顯示狀態消息?

代碼片段

//main activity 
public class MainMenu extends Activity { 

    static String status = "Hello Friends"; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    TextView tv = (TextView)findViewById(R.id.mytext); 
    tv.setText(status); 

     MyOtherClass myclass = new MyOtherClass(); 
     myclass.connect(); 
} 

其他類不是一個活動類

// Other class 
public class MyOtherClass { 
    public MyOtherClass(){ 
    } 

    public void connect(){ 
    String strIP = Mtx.confRead("IPAddress"); 
    String strPort = Mtx.confRead("Port"); 

      String msg = "Connecting..."; 
      // i want to show value of msg varible in Textview of main activity from here 
    } 

感謝您

+0

需要刷新的TextView以反映更改後的值。 –

+0

如何刷新? – Riskhan

+1

有很多方法可以做到這一點。你可以使用TimerTask。你也可以嘗試Handler類來刷新UI。 –

回答

1

做一個狀態實例字段在您的主要活動

public static status = "initial status"; 

將其設置爲TextView的

TextView tv = (TextView) findViewById(R.id.youtTextViewId); 

tv.setText(status); 

和當它們被調用時使用其他活動中的值更新它。

+0

如何從其他類中刷新TextView? – Riskhan

+0

tv.setText(newText)會做但你必須把電視公共靜態,調用此當您更改一些其它活動的狀態,但如果你是改變一些其他的活動狀態,並呼籲主要活動再次你不需要做到這一點,因爲你有更新的狀態文字 –

+0

我只想更新的TextView不充分主要活動 – Riskhan

1

是的,它是可能的,你需要傳遞的狀態值來自其他類,然後使用

  textView.setText(your_status); 

值可以通過意圖傳遞通過putExtra()和getExtra()

+0

請看我修改的問題 – Riskhan

+0

非常感謝 – Riskhan

1
在這樣

s=new Intent(this, nextClassName.class); 
        d=new Bundle(); 
        d.putString("status", status); 
        s.putExtras(d); 
        startActivity(s); 

一流的發送狀態

然後在newClassNameü可以通過這個代碼

Intent t=getIntent(); 
    k=t.getExtras(); 
    status=k.getString("status"); 

的U可以設置的TextView的文本狀態得到它

textview.setText(status); 

試試這個

+0

ü可以檢查代碼的第一個片段,有間d和s – Riskhan

+0

對不起,我忘了把putExtras沒有關係..現在我有補充說.. :) –

+0

好的。我想發送狀態到主類 – Riskhan

1

,如果其它類是由烏爾活動開始活動,然後使用類似這樣

主要活動

private void some_function() { 
    startActivityForResult(intent_to_pass, SOME_REQUEST_CODE); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if((requestCode == SOME_REQUEST_CODE) && (resultCode == RESULT_OK)) { 
     // extract status from data and use setText() to set the new status 
    } 
} 

其他活動

// Prepare an intent, say result, with the status to be sent to main activity and use this to send back the new status 
setResult(RESULT_OK, result); 

如果其他類服務和/或活動是獨立的,然後在主要活動中使用

@Override 
protected void onNewIntent(Intent intent) { 
    // Extract new status from intent now and use it 
} 

In其他類別只需以包含新狀態的意向啓動主要活動即可。這樣可以確保在主要的活動已經在運行,只需使用在收到

編輯新意圖的數據(見發佈後烏爾更新):

如果其他類既不活動,也不是服務,則u可以這樣做:

當您創建此類時,將父類(可以是服務或活動)的上下文傳遞給它,並使用此上下文創建與startActivity()一起使用的意圖。或者,只需使用BroadcastListeners進行通信。但是我不知道這是做

+0

非常感謝你 – Riskhan

+0

烏爾歡迎:) .. – skaur

1

最好的辦法ü可以通過在你的代碼

public String connect(){ 
String strIP = Mtx.confRead("IPAddress"); 
String strPort = Mtx.confRead("Port"); 

     String msg = "Connecting..."; 
     return msg; 

     // i want to show value of msg varible in Textview of main activity from here 

}

做了這些改變,並在主類做到這一點

String status=myclass.connect(); 
textview.setText(status); 

試試這個

+0

非常感謝你 – Riskhan

+0

最歡迎... :) –

1

可能是這樣可以工作.......

//main activity 
public class MainMenu extends Activity { 

static String status = "Hello Friends"; 
static TextView tv; 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 

tv = (TextView)findViewById(R.id.mytext); 
tv.setText(status); 

    MyOtherClass myclass = new MyOtherClass(); 
    myclass.connect(); 
} 

在其他類:

// Other class 
    public class MyOtherClass { 
    public MyOtherClass(){ 
    } 

     public void connect(){ 
     String strIP = Mtx.confRead("IPAddress"); 
     String strPort = Mtx.confRead("Port"); 

     String msg = "Connecting..."; 
      MainMenu.tv.setText(msg); 
      } 
+0

感謝您的解決方案 – Riskhan

相關問題