2014-02-05 84 views
3

我想傳遞一個字符串從片段活動(主)的所有片段(孩子),可能是該圖片可以解釋正是我想要做的通字符串片段活動viewpager

https://dl.dropboxusercontent.com/u/57465028/SC20140205-163325.png什麼

所以,從上面的圖片...我想通過按一個按鈕來viewpager中的所有活動從edittext傳遞一個字符串....我怎麼能這樣做?

我試圖按照此代碼https://stackoverflow.com/a/12739968/2003393但它無法解決我的問題..

請幫我...我卡

預先感謝。

這裏是片段的活性(MainActivity)我的代碼

公共類Swipe_Menu擴展FragmentActivity {

//String KeyWord; 


//private static final String KEYWORD = "keyword"; 

private ViewPager _mViewPager; 
private ViewPagerAdapter _adapter; 
/** Called when the activity is first created. */ 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.swipe_menu_image); 


    Button Back = (Button)findViewById(R.id.account); 
    ImageButton Search = (ImageButton)findViewById(R.id.search); 
    EditText Keyword = (EditText)findViewById(R.id.keyword); 

    final String KeyWord = Keyword.getText().toString(); 


    /** 
    * Back button click event 
    * */ 
    Back.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      finish(); 
     } 
    }); 



    setUpView(); 
    setTab(); 
} 

protected void sendValueToFragments(String value) { 
    // it has to be the same name as in the fragment 
    Intent intent = new Intent("my_package.action.UI_UPDATE"); 
    intent.putExtra("UI_KEY", KeyWord ); 
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent); 
} 

,這裏是我的片段(兒童活動)

public class Store_Swipe extends Fragment { 

public static final String ACTION_INTENT = "my_package.action.UI_UPDATE"; 

String KeyWord; 
private TextView kata_keyword; 

protected BroadcastReceiver receiver = new BroadcastReceiver() { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if(ACTION_INTENT.equals(intent.getAction())) { 
      String value = intent.getStringExtra("UI_KEY"); 
      updateUIOnReceiverValue(value); 
     } 
    } 
}; 

private void updateUIOnReceiverValue(String value) { 
    // you probably want this: 
    KeyWord = value; 
} 

public static Fragment newInstance(Context context) { 
    Store_Swipe f = new Store_Swipe(); 
    return f; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    IntentFilter filter = new IntentFilter(ACTION_INTENT); 
    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, filter); 
} 

@Override 
public void onDestroy() { 
    LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver); 
    super.onDestroy(); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    /*Bundle bundle = this.getArguments(); 
    KeyWord = bundle.getString("keyword");*/ 
    View view = inflater.inflate(R.layout.store_swipe, container, false); 
    init(view); 

    return view; 

} 

void init(View view) { 
    kata_keyword = (TextView) view.findViewById(R.id.keyword); 
    //ImageView image = (ImageView) view.findViewById(R.id.image_error); 
    kata_keyword.setText(KeyWord); 
} 

}

回答

4

您無法直接訪問位於我的片段n ViewPager所以你不能直接引用它們。

我在這些情況下做的事情是從活動發送廣播消息到片段。由於這個原因,在片段中註冊一個BroadcatReceiver(在onCreateonCreateView-您的決定)m,爲該接收器設置一個自定義動作(例如"my_package.actions.internal.BROADCAST_ACTION"),不要忘記從補充方法註銷接收器。

當您想要從活動發送消息時,請使用上述操作創建一個意圖,添加intent extra中的字符串併發送廣播。

在你的接收器的onReceive方法(在片段中),從意圖參數獲取字符串,並在那裏你有字符串。

有意義嗎?

編輯:提供一些代碼,下面是我會做的片段中的變化:

public class Store_Swipe extends Fragment { 

    public static final String ACTION_INTENT = "my_package.action.UI_UPDATE"; 

    protected BroadcastReceiver receiver = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      if(ACTION_INTENT.equals(intent.getAction())) { 
       String value = intent.getStringExtra("UI_KEY"); 
       updateUIOnReceiverValue(value); 
      } 
     } 
    }; 

    private void updateUIOnReceiverValue(String value) { 
     // you probably want this: 
     kata_keyword.setText(value); 
    } 

    String KeyWord; 
    private TextView kata_keyword; 

    public static Fragment newInstance(Context context) { 
     Store_Swipe f = new Store_Swipe(); 
     return f; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     IntentFilter filter = new IntentFilter(ACTION_INTENT); 
     LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, filter); 
    } 

    @Override 
    public void onDestroy() { 
     LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver); 
     super.onDestroy(); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     Bundle bundle = this.getArguments(); 
     KeyWord = bundle.getString("keyword"); 
     View view = inflater.inflate(R.layout.store_swipe, container, false); 
     init(view); 

     return view; 

    } 

    void init(View view) { 
     kata_keyword = (TextView) view.findViewById(R.id.keyword); 
     ImageView image = (ImageView) view.findViewById(R.id.image_error); 
     kata_keyword.setText(KeyWord); 
    } 
} 

而且這個代碼,我將不得不從一個活動,該參數是從EditText上的值:

protected void sendValueToFragments(String value) { 
    // it has to be the same name as in the fragment 
    Intent intent = new Intent("my_package.action.UI_UPDATE"); 
    intent.putExtra("UI_KEY", value); 
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent); 
} 

你會從點擊監聽器,你會在onCreate設置稱之爲:

findViewById(R.id.button_id).setOnClickListener(new OnClickListener() {    
    @Override 
    public void onClick(View v) { 
     String valueThatYouWantToSend = null; /// just the value 
     sendValueToFragments(valueThatYouWantToSend);  
    } 
}); 
+0

感謝反正@gunar 但是,我在Android的新手...:d 你能告訴我一些代碼,在那裏我有將** BroadcatReceiver **放入onCreate或OnCreateView? – Arsyah

+0

AndroidManifest文件如何?我應該爲Broadcastreceiver配置它嗎? – Arsyah

+0

你不需要;因爲你正在將應用程序內的消息發送到其組件。 – gunar

0

//我覺得這個解決方案將解決您的問題

// In Main activity put your code ----------------------------------- 
public void onPageSelected(int position) 
    { 

     System.out.println("nilesh"); 

     PageOneFragment f = new PageOneFragment(); 
    f.getText(); 

    PageTwoFragment ff = new PageTwoFragment(); 
    ff.setText(); 

    } 

    //in General Class ------------------------------------------------ 
    public class General 
    { 
    public static String name=""; 
    } 

    // first Fragment --------------------------------------------- 
    public void getText() 
    { 
    General.name = edittext.getText().toString(); 
    } 

    // second Fragment ---------------------------------------------- 
    public void setText() 
    { 
     System.out.println("name**" + General.name); 
     tv.setText(General.name); 
    } 
+0

謝謝反正,但我想要做的** BroadcastReceiver **方法就像我編輯的代碼... – Arsyah