简体   繁体   English

如何将数据从主要活动发送到片段

[英]How to send data from main activity to fragment

public class MyActivity extends Activity implements ButtonFragement.OnFragmentInteractionListener, TextFragment.OnFragmentInteractionListener,Communicator {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        ButtonFragement btnfrg=new ButtonFragement();
        TextFragment txtfrg= new TextFragment();
        FragmentManager fm=getFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
        ft.add(R.id.my_activity,btnfrg,"Fragment");
        ft.add(R.id.my_activity,txtfrg,"Second Fragment");
        ft.commit();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    @Override
    public void respond(String data) {
        FragmentManager fm=getFragmentManager();
        TextFragment f1= (TextFragment) fm.findFragmentById(R.id.textfrg);
        f1.changeText(data);

    }
}

This is my main_Activity code, here i am trying to send a data over the fragment but it gives me error at f1.changeText(data).Basic structure of my project is , on main Activity , i created two fragment. 这是我的main_Activity代码,在这里我试图通过片段发送数据,但是它给我f1.changeText(data)错误。我项目的基本结构是在主Activity上创建了两个片段。 One with button and another with text. 一个带有按钮,另一个带有文本。 I want to show how many times the button was clicked on second fragment using a communicator interface. 我想显示使用通信器界面在第二个片段上单击按钮的次数。 Here in "data" counter shows a how many times button was clicked but i am not able to transfer it over second fragment. 这里在“数据”计数器中显示了单击了多少次按钮,但是我无法在第二个片段上传输它。


Complete Code for the Program--- 该程序的完整代码-

public interface Communicator {
    public void respond(String data);
}

In TextFragment class i added this method---- TextFragment类中,我添加了此方法----

  public void changeText(String data)
    {
        txt.setText(data);
    }

In ButtonFragment class i added and modified following method ButtonFragment类中,我添加并修改了以下方法

public class ButtonFragement extends Fragment implements View.OnClickListener{
    int counter=0;
    private OnFragmentInteractionListener mListener;
    Button btn;
    Communicator comm;

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        comm= (Communicator) getActivity();
        btn= (Button) getActivity().findViewById(R.id.button);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        counter++;
       // comm.respond("The button was clicked "+counter+" times");
        comm.respond("hi");
    }

Here, i just added which i added in my program. 在这里,我只是添加了我在程序中添加的内容。 My program get crash at... MainActiviy f1.changeText(data); 我的程序在...崩溃MainActiviy f1.changeText(data); But why i am not getting it.Can anyone Help me fixed this bug? 但是为什么我没有得到它?有人可以帮我解决这个错误吗?

    Bundle bundle = new Bundle();
    bundle.putString("key", value);

    // set Fragmentclass Arguments

    YourFragment ff= new YourFragment ();

    ff.setArguments(bundle);

    transaction.add(R.id.my_activity, ff);

Using Bundle 使用捆绑

From Activity: 来自活动:

Bundle bundle = new Bundle();
bundle.putString("message", "Hello!");
FragmentClass fragInfo = new FragmentClass();
fragInfo.setArguments(bundle);
transaction.replace(R.id.fragment_single, fragInfo);
transaction.commit();

Fragment: 分段:

Reading the value in the fragment 读取片段中的值

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    String myValue = this.getArguments().getString("message");
    ...
    ...
}

You have no fragment with id R.id.textfrg . 您没有ID为R.id.textfrg片段。 You are for some reason adding two fragments with id R.id.my_activity . 您出于某种原因要添加两个ID为R.id.my_activity片段。 One with tag "Fragment" and another with tag "Second Fragment" . 一个带有标签"Fragment" ,另一个带有标签"Second Fragment" So you are getting error. 因此,您遇到了错误。 Your idea is rigth. 你的想法是严厉的。 This may be helpfull 这可能会有所帮助

TextFragment f1= (TextFragment) fm.findFragmentByTag("Second Fragment");

DO this way: 这样做:

  1. On Activity Side. 在活动方面。

      Fragment fragment = new GridTemplate(); Bundle bundle = new Bundle(); bundle.putInt("index",your value); fragment.setArguments(bundle); FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.frame_container, fragment).commit(); 

here R.id.frame_container is a frame Layout or Relative Layout In which you have to add the fragment. 在这里R.id.frame_container是一个框架布局或相对布局,您必须在其中添加片段。

  1. On Fragment Side. 在片段一侧。

      int index = getArguments().getInt("index"); 

hope this wil solve your problem. 希望这能解决您的问题。 Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM