简体   繁体   中英

Get EditText value in AsyncTask inside fragment

I have a fragment with EditText fields for the user's address. I am trying to send them to my webserver through AsyncTask but have not figured out how to get the values. It works fine without fragments. I tried to remove all unnecessary code. Thanks.

public class FragmentProfileMain extends Fragment implements OnClickListener {

    EditText email2, email3, mail1, mail2, cityET, stateET, zipET;

}

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

    view = inflater.inflate(R.layout.profile_fragment_main, container,
            false);

    email1 = (TextView) view.findViewById(R.id.profileEmail1);
    email2 = (EditText) view.findViewById(R.id.profileEmail2);
    email3 = (EditText) view.findViewById(R.id.profileEmail3);
    mail1 = (EditText) view.findViewById(R.id.profileAddress1);
    mail2 = (EditText) view.findViewById(R.id.profileAddress2);
    cityET = (EditText) view.findViewById(R.id.profileCity);
    stateET = (EditText) view.findViewById(R.id.profileState);
    zipET = (EditText) view.findViewById(R.id.profileZip);

return view;
}

class saveInfo extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        // Check for success tag
        int success;

        try {
            // Building Parameters
            String secondemail = ((EditText) this.email2).getText().toString();
            String thirdemail = ((EditText) this.email3).getText().toString();
            String secaddress = ((EditText) this.mail1).getText().toString();
            String secaddresstwo = ((EditText) this.mail2).getText().toString();
            String secCity = ((EditText) this.cityET).getText().toString();
            String secState = ((EditText) this.stateET).getText().toString();
            String secZip = ((EditText) this.zipET).getText().toString();

        }
    }

You should pass the edittext values to the AsyncTask as arguments

You should avoid passing views to an asynctask, in case the fragment or activity gets destroyed while an asynctask is still running. It also makes the asynctask more testable by decoupling the logic as much as possible.

public void startSaveInfo(){

    String firstEmail = email1.getText().toString();
    String secondEmail = email2.getText().toString();
    String thirdEmail = email3.getText().toString();
    String secaddress = mail1.getText().toString();
    String secaddresstwo = mail2.getText().toString();
    String secCity = cityEt.getText().toString();
    String secState = stateEt.getText().toString();
    String secZip = zipEt.getText().toString();

    new saveInfo().execute(secondemail,thirdEmail,secaddress,secaddresstwo,secCity,secState,secZip)

}

class saveInfo extends AsyncTask<String, String, String> {

    @Override
    protected String doInBackground(String... args) {
            // Building Parameters
            String secondemail = args[0];
            String thirdemail = args[1];
            String secaddress = args[2];
            String secaddresstwo = args[3];
            String secCity = args[4];
            String secState = args[5];
            String secZip = args[6];

            //do your work

    }

}

When you reference this.email2 etc. in your saveInfo class, this is a reference to the class which has no concept of the EditText objects you have instantiated in your Fragment.

One way of accessing the instance variables is to create a saveInfo constructor and sending it your view object from the fragment.

In your fragment, or wherever you are calling your saveInfo task:

SaveInfo saveInfo = new SaveInfo(view) // 'saveInfo' should be capitalised

In saveInfo :

...
View view;    
public SaveInfo (View view) {
    this.view = view;
}

@Override
protected String doInBackground(String... args) {
    // TODO Auto-generated method stub
    // Check for success tag
    int success;

    try {
        // Building Parameters
        String secondemail = view.email2.getText().toString();
....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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