在这两个片段之间传递数据是不可能的

时间:2013-09-17 14:54:31

标签: android string interface android-fragments

我疯了! 我无法将数据从片段(A)传递到另一个片段(B)。 我读到了使用公共接口...它似乎工作,但我不明白如何使用这种方法。

片段(A)

package it.anddev.pagertabs;
public class Page1Fragment extends Fragment {

String Str;
OnDataPass dataPasser;

Class Senddata extends AsyncTask<String, String, String> {

   @Override
   protected void onPreExecute() {

                            // do something [...]

   }

    @Override
    protected String doInBackground(String... args) {

                             // do something [...]

           dataPasser.onDataPass(result_array.toString());

    }

      return null;

    }

    protected void onPostExecute(String file_url) {

                             // do something [...]

    }};



@Override
public void onAttach(Activity activity) {
  super.onAttach(activity);
  dataPasser = (OnDataPass) activity;

}

public interface OnDataPass {
    public void onDataPass(String data);
}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

                       // do something [...]

  Button Avanti = (Button) view.findViewById(R.id.sendbutton);
  Avanti.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View view) {
               //mViewPager.setCurrentItem(1, true);
               new Senddata().execute();
           }
            });

FragmentB

public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {


  if (container == null) {
     return null;
  }

  View view = (LinearLayout)inflater.inflate(R.layout.page2,container,false);

  Button mostra = (Button) view.findViewById(R.id.mostrabutton);

           // String str = i need to get string from "public void onDataPass"

  mostra.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View view) {
           //mViewPager.setCurrentItem(1, true);
           Log.d("IT WORKS", str);
       }
        });


  return view;
}

@Override
public void onDataPass(final String data) {
// TODO Auto-generated method stub
Log.d("okkkkkk", "" + data);

}

最后,我如何从fragmentB中的public void获取字符串? 感谢

4 个答案:

答案 0 :(得分:2)

考虑我的2个片段A和B,并假设我需要将数据从B传递给A.

然后在B中创建一个接口,并将数据传递给Main Activity。创建另一个接口并将数据传递给片段A.

分享一个小例子:

片段A看起来像

public class FragmentA extends Fragment implements InterfaceDataCommunicatorFromActivity {
    public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity;
    String data;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void updateData(String data) {
        this.data = data;
        //data is updated here which is from fragment B
    }

    @Override
    public void onAttach(Activity activity) {
    super.onAttach(activity);
        try {
            interfaceDataCommunicatorFromActivity = (InterfaceDataCommunicatorFromActivity) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement InterfaceDataCommunicatorFromActivity");
        }
    }

}

FragmentB看起来像

class FragmentB extends Fragment {
    public InterfaceDataCommunicator interfaceDataCommunicator;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // call this inorder to send Data to interface
        interfaceDataCommunicator.updateData("data");
    }

    public interface InterfaceDataCommunicator {
        public void updateData(String data);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            interfaceDataCommunicator = (InterfaceDataCommunicator) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement InterfaceDataCommunicator");
        }
    }

}

主要活动是

public class MainActivity extends Activity implements InterfaceDataCommunicator {
    public InterfaceDataCommunicatorFromActivity interfaceDataCommunicatorFromActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

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

    @Override
    public void updateData(String data) {
        interfaceDataCommunicatorFromActivity.updateData(data);
    }

    public interface InterfaceDataCommunicatorFromActivity {
        public void updateData(String data);
    }

}

答案 1 :(得分:1)

最简单的方法是在第一个片段中创建一个静态变量,并在第二个片段中使用此变量。

答案 2 :(得分:0)

使用公共接口通过包含片段的Activity传递String。 Here是一个类似的问题和解决方案,更详细。

答案 3 :(得分:0)

考虑2个片段A和B,并假设我需要将数据从B传递给A.

然后在B中创建一个接口,并将数据传递给Activity。在那里,您可以拨打A getActivity()来获取信息。

示例:

活动是:

public class MainActivity extends Activity implements InterfaceDataCommunicator {
    public static String data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

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

    @Override
    public void updateData(String data) {
        this.data = data;
    }

}

片段A:

public class FragmentA extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        getActivity().data;//receive data

        return super.onCreateView(inflater, container, savedInstanceState);
    }

}

片段B:

class FragmentB extends Fragment {
    public InterfaceDataCommunicator interfaceDataCommunicator;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        interfaceDataCommunicator.updateData(/*the String to send*/); //send data
    }

    public interface InterfaceDataCommunicator {
        public void updateData(String data);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            interfaceDataCommunicator = (InterfaceDataCommunicator) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement InterfaceDataCommunicator");
        }
    }

}