簡體   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);

    }
}

這是我的main_Activity代碼,在這里我試圖通過片段發送數據,但是它給我f1.changeText(data)錯誤。我項目的基本結構是在主Activity上創建了兩個片段。 一個帶有按鈕,另一個帶有文本。 我想顯示使用通信器界面在第二個片段上單擊按鈕的次數。 這里在“數據”計數器中顯示了單擊了多少次按鈕,但是我無法在第二個片段上傳輸它。


該程序的完整代碼-

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

TextFragment類中,我添加了此方法----

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

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");
    }

在這里,我只是添加了我在程序中添加的內容。 我的程序在...崩潰MainActiviy f1.changeText(data); 但是為什么我沒有得到它?有人可以幫我解決這個錯誤嗎?

    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);

使用捆綁

來自活動:

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

分段:

讀取片段中的值

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

您沒有ID為R.id.textfrg片段。 您出於某種原因要添加兩個ID為R.id.my_activity片段。 一個帶有標簽"Fragment" ,另一個帶有標簽"Second Fragment" 因此,您遇到了錯誤。 你的想法是嚴厲的。 這可能會有所幫助

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

這樣做:

  1. 在活動方面。

      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(); 

在這里R.id.frame_container是一個框架布局或相對布局,您必須在其中添加片段。

  1. 在片段一側。

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

希望這能解決您的問題。 謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM