将视图传递给AsyncTask以访问findViewById [英] Passing view to AsyncTask to access findViewById

查看:83
本文介绍了将视图传递给AsyncTask以访问findViewById的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android编程的新手,并且有一个问题.

I am new to Android programming, and have one question.

我正在尝试在我的AsyncTask中访问 findViewById ,但是显然,默认情况下它将不可用,因为我没有对View对象执行任何操作.

I am trying to access findViewById in my AsyncTask, but obviously, by default this will not be available, because I am not performing any actions against a View object.

我发现有几篇文章解释了如何解决此问题,但是它们已经过时了,已有5年以上的历史,并且想知道这是否仍然是正确的方法?我正在使用android的数据绑定方法,应该替换 findViewById 调用,但是在这种情况下,我不知道怎么做?

I have found, a few articles, explaining how to solve this, but they are old, 5 years and up, and would like to know if this is still the correct approach? I am using android's data binding methodology, and this is supposed to replace findViewById calls, but I don't see how, in this scenario?

解决方案仍然有效吗?

Is this way of solving still valid?

这是我的代码,以防万一.我正在尝试从 AsyncTask

Here, is my code, in case there is a better solution. I am trying to access the progressbar in this view from within the AsyncTask

我的个人资料视图:

<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
    <variable name="user" type="Models.User" />
    <variable name="viewActions" type="ViewModel.ProfileViewModel" />
</data>

<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal|top">

    <ProgressBar
        android:id="@+id/progressPostUser"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:visibility="gone"/>

    <include layout="@layout/main_toolbar"/>

    <ImageView
        android:id="@+id/imagePlaceHolder"
        android:layout_width="250dp"
        android:layout_height="150dp"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="5dp"
        android:src="@mipmap/ic_account"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp">

        <ImageButton
            android:id="@+id/btnOpenCamera"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginRight="10dp"
            android:src="@mipmap/ic_account"
            android:onClick="btnOpenCamper_OnClick"/>

        <ImageButton
            android:id="@+id/btnChooseImage"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@mipmap/ic_view_list"/>

    </LinearLayout>

    <EditText
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:hint="Name"
        android:text="@={user._name}"/>

    <EditText
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:hint="Surname"
        android:text="@={user._surname}"/>

    <EditText
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:hint="Email"
        android:text="@={user._email}"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save"
        android:onClick="@{() -> viewActions.onSaveClicked(user)}"/>

</LinearLayout>

我的活动课:

public class ProfileActivity extends MenuActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActivityProfileBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_profile);
    binding.setUser(new User());
    binding.setViewActions(new ProfileViewModel(this));

    //get the toolbar
    Toolbar tb = (Toolbar)findViewById(R.id.toolbarMain);
    setSupportActionBar(tb);
}
}

还有处理来自View的事件的'ViewModel'.

And the 'ViewModel' which handles events from the View.

public class ProfileViewModel {

private User mUser;
private Context mContext;

public ProfileViewModel(Context context){
    mContext = context;
}

public void onSaveClicked(User user) {
    String nameTest = user.get_name();
    String surnameTest = user.get_surname();

    Toast.makeText(mContext, user.get_name(), Toast.LENGTH_SHORT).show();
}
}

这是我的User类.

public class User extends BaseObservable {

public User() {

}

private String _name;
@Bindable
public String get_name() {
    return _name;
}
public void set_name(String _name) {
    this._name = _name;
    notifyPropertyChanged(BR._name);
}

private String _surname;
@Bindable
public String get_surname() {
    return _surname;
}
public void set_surname(String _surname) {

    this._surname = _surname;
    notifyPropertyChanged(BR._surname);
}

private String _email;
@Bindable
public String get_email() {
    return _email;
}
public void set_email(String _email) {

    this._email = _email;
    notifyPropertyChanged(BR._email);
}

private Bitmap _profileImage;
public Bitmap get_profileImage() {
    return _profileImage;
}
public void set_profileImage(Bitmap _profileImage) {
    this._profileImage = _profileImage;
}

public String toJsonString(){
    try{
        JSONObject jObject = new JSONObject();
        jObject.put("Name", get_name());
        jObject.put("Surname", get_surname());
        jObject.put("Email", get_email());
        jObject.put("ProfileImage", Base64.encodeToString(convertBitmapToBytes(), Base64.DEFAULT));
    } catch (Exception ex){
        Log.d("Error", "User.toJson");
    }
    return "";
}

@Override
public String toString() {
    return super.toString();
}

private byte[] convertBitmapToBytes(){
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    get_profileImage().compress(Bitmap.CompressFormat.PNG, 100, stream);
    return stream.toByteArray();
}

}

推荐答案

我真的不喜欢将上下文和视图传递给不同对象的想法,并且不喜欢将视图特定的功能保留在活动类本身内,所以我实现了一个我经过的接口,如下所示:

I really did not like the idea of passing context, and the View around to different objects, and like to keep the view specific functionality within the activity class itself, so I implemented an interface, that I passed around, as follow:

这是我的界面:

public interface IProfiler {
    void ShowProgressbar();
    void HideProgressbar();
    void MakeToast();
}

我的活动类实现此接口,如下所示:

My activity class implements this interface, as follow:

public class ProfileActivity extends MenuActivity implements IProfiler {

private ProgressBar mProgressar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActivityProfileBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_profile);
    binding.setUser(new User());
    binding.setViewActions(new ProfileViewModel(this));


    //get the toolbar
    Toolbar tb = (Toolbar)findViewById(R.id.toolbarMain);
    setSupportActionBar(tb);

    //set the Progressbar
    mProgressar = (ProgressBar)findViewById(R.id.progressPostUser);
}

@Override
public void ShowProgressbar() {
    mProgressar.setVisibility(View.VISIBLE);
}

@Override
public void HideProgressbar() {
    mProgressar.setVisibility(View.GONE);
}

@Override
public void MakeToast() {
    Toast.makeText(this, "Some toast", Toast.LENGTH_SHORT);
}
}

我的ProfileViewModel,除了将接口作为参数:

My ProfileViewModel, which excepts the interface as parameter:

    public class ProfileViewModel {

    private User mUser;
    private IProfiler mProfiler;

    public ProfileViewModel(IProfiler profiler){
        mProfiler = profiler;
    }

    public void onSaveClicked(User user) {
        try {
            String nameTest = user.get_name();
            String surnameTest = user.get_surname();

            new AsyncTaskPost(mProfiler).execute(new URL("http://www.Trackme.com"));
        }
        catch (Exception ex) {

        }
    }
}

最后是我的AsyncTaskPost.

And then finally, my AsyncTaskPost.

public class AsyncTaskPost extends AsyncTask<URL, Void, Void> {

private IProfiler mProfilerActions;

public AsyncTaskPost(IProfiler profilerActions){
    mProfilerActions = profilerActions;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    mProfilerActions.ShowProgressbar();
}

@Override
protected Void doInBackground(URL... urls) {
    try{
        Thread.sleep(5000);
        return null;
    }
    catch (Exception ex) {
        return null;
    }
}

@Override
protected void onPostExecute(Void aVoid) {
    mProfilerActions.HideProgressbar();
    mProfilerActions.MakeToast();
}

@Override
protected void onCancelled() {

    super.onCancelled();
    mProfilerActions.HideProgressbar();
}
}

这篇关于将视图传递给AsyncTask以访问findViewById的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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