简体   繁体   English

Android - 将View从Activity传递到AsyncTask类

[英]Android - Passing View from Activity to AsyncTask class

MainActivity.java: MainActivity.java:

private class NetCheck extends AsyncTask<String, Void, Boolean> {
    @Override
    protected Boolean doInBackground(String... args) {
        return cd.isConnectingToInternet();
    }

    protected void onPostExecute(Boolean th) {
        if (th == true) {
            new RemoteLoader(MainActivity.this).execute();
        }
    }
}

RemoteLoader.java: RemoteLoader.java:

public class RemoteLoader extends AsyncTask<Void, Void, Void>{
    private View view;

    public RemoteLoader(View context){
        this.view = view;
    }

    @Override
    protected Void doInBackground(Void... Pages) {
             // do in bg
    }

    @Override
    protected void onPostExecute(Void result) {
        // Set title into TextView
        TextView txttitle = (TextView) view.findViewById(R.id.txtProtip);
        txttitle.setText(protip);
    }

}

I'm trying to execute RemoteLoader class from MainActivity . 我正在尝试从MainActivity执行RemoteLoader类。 But in RemoteLoader contains something that needs to inflate the Main layout. 但是在RemoteLoader中包含需要膨胀Main布局的东西。

How do I pass the Layout from MainActivity to RemoteLoader in this case? 在这种情况下,如何将布局从MainActivity传递到RemoteLoader?

The problem with this is that in case of eg an orientation change during the loading process the activity is recreated and the view will no longer be valid. 这样做的问题是,例如在加载过程中方向改变的情况下,将重新创建活动,并且视图将不再有效。 You risk leaking the activity. 您有泄漏活动的风险。 Also, the view that you try to write to will no longer be the one visible. 此外,您尝试写入的视图将不再是可见的。

Take a look at the Robospice documentation for understanding the basic problem: https://github.com/octo-online/robospice/wiki/Understand-the-basics-of-RoboSpice-in-30-seconds This is one of the trickier parts about Android. 看一下Robospice文档,了解基本问题: https//github.com/octo-online/robospice/wiki/Understand-the-basics-of-RoboSpice-in-30-seconds这是一个比较棘手的问题有关Android的部分。

pass an Activity to the RemoteLoader instead of View as follows 将一个Activity传递给RemoteLoader而不是View,如下所示

private WeakReference<Activity> activity;

public RemoteLoader(Activity context){
    this.activity = new WeakReference<Activity>(context);
}

Update: Using weak reference to prevent possible memory leaks. 更新:使用弱引用来防止可能的内存泄漏。

Use this code for RemoteLoader:-- 将此代码用于RemoteLoader: -

public class RemoteLoader extends AsyncTask<Void, Void, Void>{

LayoutInflater inflater;
int layoutResId;

public RemoteLoader(View context,int layoutResId){
    this.view = view;
    inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.layoutResId = layoutResId;
}

@Override
protected Void doInBackground(Void... Pages) {
         // do in bg
}

@Override
protected void onPostExecute(Void result) {
    // Set title into TextView
    View yourLayout = inflater.inflate(layoutResId, null);
    TextView txttitle = (TextView) yourLayout.findViewById(R.id.txtProtip);
    txttitle.setText(protip);
}

}

you can pass the TextView reference to the async task class through constructor 您可以通过构造函数将TextView引用传递给异步任务类

public class RemoteLoader extends AsyncTask<Void, Void, Void>{

    TextView txtTitle;

    public RemoteLoader(TextView txtTitle){
        this.txtTitle = txtTitle;
    }

    @Override
    protected Void doInBackground(Void... Pages) {
             // do in bg
    }

    @Override
    protected void onPostExecute(Void result) {
        // Set title into TextView
        txttitle.setText(protip);
    }

}

in your main activity execute the async task as follwos 在您的主要活动中以follwos的身份执行异步任务

TextView txttitle = (TextView) view.findViewById(R.id.txtProtip);
new RemoteLoader(txttitle).execute();

if you want to execute this async task from another async task, then pass the textView reference through that from the MainActivity 如果要从另一个异步任务执行此异步任务,则通过MainActivity传递textView引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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