在片段内的AsyncTask中获取EditText值

时间:2014-09-01 01:08:46

标签: android android-fragments android-asynctask

我有一个包含EditText字段的片段,用于用户的地址。我试图通过AsyncTask将它们发送到我的网络服务器,但还没弄明白如何获取值。没有碎片,它工作正常。我试图删除所有不必要的代码。感谢。

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

        }
    }

2 个答案:

答案 0 :(得分:3)

您应该将edittext值作为参数传递给AsyncTask

如果在asynctask仍在运行时片段或活动被销毁,则应避免将视图传递给asynctask。它还通过尽可能地解耦逻辑使得asynctask更容易测试。

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

    }

}

答案 1 :(得分:0)

当您在this.email2班级中引用saveInfo等时,this是对该类的引用,该类没有您在实例化中EditText个对象的概念。片段。

访问实例变量的一种方法是创建saveInfo构造函数并从片段中发送view对象。

在您的片段中,或在您调用saveInfo任务的任何地方:

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

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