将片段主要活动中的字符串传递给viewpager中的片段活动

时间:2014-02-05 09:53:37

标签: android string android-fragments fragment

我想从片段活动(主要)传递一个字符串到所有片段(子),可能这张图片可以解释我想要做什么

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

所以,从上面的图片...我想通过按一个按钮来传递一个来自edittext的字符串到viewpager中的所有活动....我怎么能这样做?

我试图遵循此代码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);
}

}

2 个答案:

答案 0 :(得分:4)

您无法直接访问位于ViewPager的片段,因此您无法直接引用这些片段。

在这些情况下我正在做的是从Activity发送广播消息到Fragments。因此,在片段中注册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);
    }
}

这个代码我将来自activity,参数是来自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);       
    }
});

答案 1 :(得分: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);
  }