我有两门课。第一个是活动,第二个是片段,其中有一些EditText。在活动中,我有一个带有async-task的子类,在方法doInBackground中,我得到了一些结果,我保存到变量中。我怎么能把这个变量从子类“我的活动”到这个片段?


当前回答

在片段和活动之间传递数据的最聪明的方法是创建一个变量,例如:

class StorageUtil {
  public static ArrayList<Employee> employees;
}

然后通过onActivityCreated方法将数据从fragment传递给activity:

//a field created in the sending fragment
ArrayList<Employee> employees;

@Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
         employees=new ArrayList();

       //java 7 and above syntax for arraylist else use employees=new ArrayList<Employee>() for java 6 and below

     //Adding first employee
        Employee employee=new Employee("1","Andrew","Sam","1984-04-10","Male","Ghanaian");
        employees.add(employee);

      //Adding second employee
       Employee employee=new Employee("1","Akuah","Morrison","1984-02-04","Female","Ghanaian");
         employees.add(employee);

        StorageUtil.employees=employees;
    }

现在您可以获得StorageUtil的值。来自各地的员工。 古德勒克!

其他回答

如果你将片段(具体子类)的引用传递给异步任务,你就可以直接访问片段。

将片段引用传递给异步任务的一些方法:

如果你的异步任务是一个完全成熟的类(类FooTask扩展AsyncTask),那么将你的片段传递到构造函数中。 如果您的异步任务是一个内部类,只需在异步任务定义的范围内声明一个最终的Fragment变量,或者作为外部类的字段。您将能够从内部类访问它。

有时你可以在你的活动中接收Intent,你需要将信息传递给你的工作片段。 给出的答案是OK,如果你需要启动片段,但如果它仍在工作,setArguments()不是很有用。 如果传递的信息将导致与UI交互,则会出现另一个问题。在这种情况下,你不能调用像myfragment.passData()这样的东西,因为android会迅速告诉只有创建视图的线程可以与之交互。

所以我的建议是使用接收器。这样,您可以从任何地方发送数据,包括活动,但是工作将在片段的上下文中完成。

在你的片段的onCreate():

protected DataReceiver dataReceiver;
public static final String REC_DATA = "REC_DATA";

@Override
public void onCreate(Bundle savedInstanceState) {


    data Receiver = new DataReceiver();
    intentFilter = new IntentFilter(REC_DATA);

    getActivity().registerReceiver(dataReceiver, intentFilter);
}

private class DataReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        int data= intent.getIntExtra("data", -1);

        // Do anything including interact with your UI
    }
}

在你的活动:

// somewhere
Intent retIntent = new Intent(RE_DATA);
retIntent.putExtra("data", myData);
sendBroadcast(retIntent);

使用以下接口在活动和片段之间进行通信

public interface BundleListener {
    void update(Bundle bundle);
    Bundle getBundle();
}

或者使用下面这个通用监听器进行双向通信使用接口

 /**
 * Created by Qamar4P on 10/11/2017.
 */
public interface GenericConnector<T,E> {
    T getData();
    void updateData(E data);
    void connect(GenericConnector<T,E> connector);
}

片段显示法

public static void show(AppCompatActivity activity) {
        CustomValueDialogFragment dialog = new CustomValueDialogFragment();
        dialog.connector = (GenericConnector) activity;
        dialog.show(activity.getSupportFragmentManager(),"CustomValueDialogFragment");
    }

你也可以在onAttach(context)中将你的上下文转换为GenericConnector

在你的活动中

CustomValueDialogFragment.show(this);

在你的片段中

...
@Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        connector.connect(new GenericConnector() {
            @Override
            public Object getData() {
                return null;
            }

            @Override
            public void updateData(Object data) {

            }

            @Override
            public void connect(GenericConnector connector) {

            }
        });
    }
...
    public static void show(AppCompatActivity activity, GenericConnector connector) {
            CustomValueDialogFragment dialog = new CustomValueDialogFragment();
            dialog.connector = connector;
            dialog.show(activity.getSupportFragmentManager(),"CustomValueDialogFragment");
        }

注意:千万不要像"".toString().toString().toString();道路

在你的活动中声明静态变量

public static HashMap<String,ContactsModal> contactItems=new HashMap<String, ContactsModal>();

然后在你的片段中像这样做

ActivityName.contactItems.put(Number,contactsModal);

在片段和活动之间传递数据的最聪明的方法是创建一个变量,例如:

class StorageUtil {
  public static ArrayList<Employee> employees;
}

然后通过onActivityCreated方法将数据从fragment传递给activity:

//a field created in the sending fragment
ArrayList<Employee> employees;

@Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
         employees=new ArrayList();

       //java 7 and above syntax for arraylist else use employees=new ArrayList<Employee>() for java 6 and below

     //Adding first employee
        Employee employee=new Employee("1","Andrew","Sam","1984-04-10","Male","Ghanaian");
        employees.add(employee);

      //Adding second employee
       Employee employee=new Employee("1","Akuah","Morrison","1984-02-04","Female","Ghanaian");
         employees.add(employee);

        StorageUtil.employees=employees;
    }

现在您可以获得StorageUtil的值。来自各地的员工。 古德勒克!