简体   繁体   English

在android mvp中传递对象?

[英]Pass an object in android mvp?

recently I been reading about the mvp pattern on Internet and to be honest, it has been a little bit confusing. 最近,我一直在阅读有关Internet上的mvp模式的信息,说实话,这有点令人困惑。

I'm trying to re-write an app using the mvp pattern but I reached a point using AsyncTask where I can't figure out how to pass the Object in the Interactor back to the View. 我正在尝试使用mvp模式重新编写应用程序,但是使用AsyncTask达到了无法解决如何将Interactor中的对象传递回View的地步。

Here'e the code: 这是代码:

View: 视图:

public class DetailsActivity extends BaseActivity {

private DetailsPresenter presenter;
private Pet pet;
private TextView name, description, breed, lostAt, age;
private int idList;

@Override
protected void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    setContentView(R.layout.details_layout);

    DetailsPresenter presenter = new DetailsPresenter();
    Typeface font0 = Typeface.createFromAsset(getAssets(), "fonts/CaviarDreams.ttf");
    Typeface font1 = Typeface.createFromAsset(getAssets(), "fonts/CaviarDreams_Bold.ttf");
    TextView txtDescription = findViewById(R.id.textView8);
    TextView txt1 = findViewById(R.id.textView6); //Age, Type
    TextView txt2 = findViewById(R.id.textView9); //LostAt
    Button btnContact = findViewById(R.id.button6);
    description = findViewById(R.id.details);
    menuRes = R.menu.details_menu;
    name = findViewById(R.id.textView10);
    breed = findViewById(R.id.breed);
    lostAt = findViewById(R.id.lostAt);
    age = findViewById(R.id.age);

    name.setTypeface(font1);
    breed.setTypeface(font0);
    description.setTypeface(font0);
    lostAt.setTypeface(font0);
    txtDescription.setTypeface(font1);
    txt2.setTypeface(font1);
    txt1.setTypeface(font1);
    btnContact.setTypeface(font0);

    idList = getIntent().getExtras().getInt("TAG_LIST");
    id = getIntent().getExtras().getInt("TAG_ID");

    if(idList == 0){
        txt1.setText(R.string.type);
        txt2.setText(R.string.txt2);
    } else{
        txt1.setText(R.string.txt0);
        txt2.setText(null);
    }

    btnContact.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getApplicationContext(), ChatFragment.class);
            startActivity(intent);
        }
    });

//        Get pet details from database on background
    pet = presenter.getPet(idList, id);
}

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

    if(idList == 0){
        name.setText(pet.getName());
        breed.setText("(" + pet.getBreed() + ")");
        description.setText(pet.getDescription());
        lostAt.setText(pet.getLocation());
    }else{
        name.setText(pet.getBreed());
        description.setText(pet.getDescription());
        lostAt.setText(pet.getGender());
        age.setText(pet.getAge());
    }
}

} }

Presenter: 主持人:

public class DetailsPresenter {

private DetailsInteractor interactor;

public Pet getPet(int idList, int id) {

    interactor = new DetailsInteractor(idList, id);
    interactor.execute();

    return interactor.pet;
}

} }

Interactor: 交互器:

public class DetailsInteractor extends AsyncTask<String, String, Pet> {

public Pet pet;
private int idList;
private int id;
private DBAction database;

public DetailsInteractor (int idList, int id) {

    this.idList = idList;
    this.id = id;

    database = new DBAction();
}

@Override
protected Pet doInBackground(String... strings) {

    pet = database.requestItem(idList, id);
    return pet;
}

I need that after getting the data from the database, it updates the View, using object Pet. 我需要从数据库中获取数据后,使用对象Pet更新视图。

Any answers and suggestions will be welcomed, Thanks! 任何答案和建议都将受到欢迎,谢谢!

When You create/initialize a presenter in Activity(This is your view) you should pass the view to the presenter. 当您在Activity(这是您的视图)中创建/初始化演示者时,应将视图传递给演示者。 Something like this. 这样的事情。

 DetailsPresenter presenter = new DetailsPresenter(View view);

View can be any object with which you can update the UI or can call the methods in the activity. 视图可以是可以用来更新UI或可以在活动中调用方法的任何对象。

Moreover, you have to go through few more good site to learn about MVP. 此外,您还需要浏览一些更好的站点来了解MVP。

http://valokafor.com/learn-android-mvp-pattern-example/ this is really nice one. http://valokafor.com/learn-android-mvp-pattern-example/这是非常好的一个。

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

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