是否有可能通过一个片段的构造? [英] Is it possible to pass a fragment in a constructor?

查看:106
本文介绍了是否有可能通过一个片段的构造?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的片段传递到AsyncTask的类,这样我可以在一次碎片任务完成更新或两个部件。下面是我处理:

I am trying to pass my Fragment to an ASyncTask class so that I can update a widget or two in the fragment once the task completes. Here's what I'm dealing with:

public class LoginFragment extends Fragment {

    Button loginButton;
    TextView loginErrorMsg;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.loginfragment, container, false);
    }

    public OnClickListener loginListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.v("LoginF", "onclick");
            ProgressDialog progressDialog = new ProgressDialog(getActivity());
            progressDialog.setMessage("Logging in...");
            LoginTask loginTask = new LoginTask((Polling) getActivity(), progressDialog);
            loginTask.execute();
        }
    };

而LoginTask:

And the LoginTask:

public class LoginTask extends AsyncTask<String, Void, Integer> {

private ProgressDialog progressDialog;
private Polling activity;
private int id = -1;
private JSONParser jsonParser;
private static String loginURL = "http://davidjkelley.net/android_api/";
private static String registerURL = "http://davidjkelley.net/android_api/";
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";
TextView loginErrorMsg = (EditText)activity.findViewById(R.id.loginErrorMsg);
EditText userName = (EditText)activity.findViewById(R.id.emailEditText);
EditText passwordEdit = (EditText)activity.findViewById(R.id.passEditText);

public LoginTask(Polling activity, ProgressDialog progressDialog)
{
    this.activity = activity;
    this.progressDialog = progressDialog;
}

所以我想第三个参数添加到LoginTask的构造,基本上我LoginFragment的一个实例。我的目标是更新或者一个TextView或提出了一个面包在屏幕上澄清登录是否成功还是失败:因为现在的用户没有告诉登录如何进行的方式。想法?

So I would like to add a third parameter to the constructor of LoginTask, essentially an instance of my LoginFragment. My goal is to update either a TextView or put up a Toast on the screen to clarify whether login succeeds or fails: as right now, the user has no way of telling how the login proceeded. Ideas?

推荐答案

由于好奇的说,你不想被周围路过片段(他们有一个链接到这是一个方面的活动,并通过上下文是baaad)

As curious says you don't want to be passing Fragments around (they have a 'link' to the activity which is a context and passing contexts is baaad)

您想通过一个小的物体,可以帮助你叫从任务返回到您的片段。

You want to pass a small object that can help you call back from your Task to your Fragment.

我也将使用的接口。这里是我的例子:

I would also use an interface. Here's my example:

片段:

public class LoginFragment extends Fragment implements OnClickListener, OnLoginListener{

    Button loginButton;
    TextView loginErrorMsg;
    private ProgressDialog progressDialog;

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

        progressDialog = new ProgressDialog(activity);
        progressDialog.setMessage("Logging in...");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_login, container, false);

        loginButton = v.findViewById(R.id.button);
        loginButton.setOnClickListener(this);

        return v;

    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.button:
            Log.v("LoginF", "onclick");
            progressDialog.show();
            LoginTask loginTask = new LoginTask(this);
            loginTask.execute();
            break;
        default:
            break;
        }
    }

    @Override
    public void onLoginSuccess() {
        progressDialog.dismiss();
        // Yayy
    }

    @Override
    public void onLoginFailure() {
        progressDialog.dismiss();
        // Boo
    }
}

在AsyncTask的:

The ASyncTask:

public class LoginTask extends AsyncTask<String, Void, Integer> {

    private final OnLoginListener listener;

    public interface OnLoginListener{
        public void onLoginSuccess();
        public void onLoginFailure();
    }

    public LoginTask(OnLoginListener listener) {
        this.listener = listener;
    }

    @Override
    protected Integer doInBackground(String... params) {
        try{
            // Something
        } catch (SomeException e){
            listener.onLoginFailure();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
        listener.onLoginSuccess();
    }

}

如果你得到你的头周围的接口你的世界将开辟和你的​​code看起来不那么像亚马逊丛林,而更像一个组织良好的花园; - )

If you get your head around interfaces your world will open up and your code will look less like the amazon jungle and more like a well organised garden ;-)

这篇关于是否有可能通过一个片段的构造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆