-3

我要疯了!!我无法将数据从片段(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();
           }
            });

片段B

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中的公共void获取字符串?谢谢

4

4 回答 4

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

}

片段B看起来像

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

}
于 2013-10-30T09:48:44.853 回答
1

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

于 2013-09-17T15:05:38.997 回答
0

使用公共接口通过包含片段的 Activity 传递字符串。 是更详细的类似问题和解决方案。

于 2013-09-17T20:34:36.543 回答
0

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

然后在B中创建一个接口,并将数据传递给Activity。从那里您可以致电 AgetActivity()以获取信息。

例子:

活动是:

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

}
于 2015-07-08T00:25:15.697 回答