2013-05-11 40 views
0

我在SOF上看到過幾個問題,但都沒有幫助。如何更改其中一個Activity的ListView中的數據其他活動

在我的應用程序中,我有一個用戶列表,可以通過單擊用戶的朋友進行訪問。流程是:

  1. 轉到我的個人資料

  2. 點擊我的朋友去到具有用戶列表(我的朋友)

  3. 點擊任何一個活動listView項目需要該用戶的配置文件

  4. 從該配置文件中,我可以看到用戶的好友列表與我的列表相同。

是所有這些的ListView項目有一個按鈕,add as friend的問題,這使得我在該列表中用戶的朋友(喜歡跟着改變在嘰嘰喳喳下面),現在我回來了通過堆棧中,和前一個listView中某個用戶所在的按鈕所在的位置add as friend

如何更改所有ListView中該用戶的按鈕(我的適配器數據中的標誌)?

+0

嘗試定義您的項目靜態全局和您的列表視圖。或者使用Bundle和putextras – 2013-05-11 11:27:23

+0

我無法將數據發回到距離後臺僅5步的活動。其實我甚至不知道要更改哪個ListView。如果全局變量是不可能的,那麼它不適合使用,但是在這些活動中使用onresume事件只是從數據庫或任何你想要的地方重新創建列表,所以也有靜態數據不會工作,因爲有活動的多個實例有列表 – user1537779 2013-05-11 11:28:58

+0

。 – 2013-05-11 11:36:56

回答

1

使用Interface可將事件發送回活動並在收到活動時更新列表或數據庫。

接口是將消息傳遞給「外部世界」的方式。看看簡單的button onClickListener。您基本上在按鈕上調用setOnClickListener(this)並實現onClickListener,這裏是interface。無論何時點擊按鈕,您都會收到onClick的事件。它是通過活動之間的消息,而無需意圖的最安全的方式(根據我這是一個巨大的痛苦中......)下面是一個例子:

例子:

class A extends Activity implements EventInterface{ 

    public A(){ 

     //set a listener. (Do not forget it!!!) 
     //You can call it wherever you want; 
     //just make sure that it is called before you need something out of it. 
     //safest place is onCreate. 
     setEventInterfaceListener(A.this); 

    }  

    //this method will be automatically added once you implement EventInterface. 
    void eventFromClassB(int event){ 

     //you receive events here. 
     //Check the "event" variable to see which event it is. 

    }   

} 


class B{ 

    //Interface logic 
    public interface EventInterface{ 
     public static int BUTTON_CLICKED = 1; 

     void eventFromClassB(int event); 
    } 
    static EventInterface events; 

    public static void setEventInterfaceListener(EventInterface listener) { 
     events = listener; 
    } 

    private void dispatchEvent(int trigger) { 
     if (events != null) { 
      events.eventFromClassB(trigger); 
     } 
    } 

    //Interface ends 

    void yourMethod(){ 

     //Call this whenever you want to send an event. 
     dispatchEvent(BUTTON_CLICKED); 

    } 

} 
+0

你能否提供一些代碼或鏈接給你推薦的。 – user1537779 2013-07-24 06:32:28

+0

當然!我對我的帖子進行了編輯。 – 2013-07-26 16:12:02

相關問題