Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android mvp中传递对象?_Android_Design Patterns_Mvp - Fatal编程技术网

在android mvp中传递对象?

在android mvp中传递对象?,android,design-patterns,mvp,Android,Design Patterns,Mvp,最近我在网上读到了关于mvp模式的文章,说实话,这有点让人困惑 我试图使用mvp模式重新编写一个应用程序,但我使用AsyncTask时遇到了一个问题,即我不知道如何将Interactor中的对象传递回视图 代码如下: 查看: public class DetailsActivity extends BaseActivity { private DetailsPresenter presenter; private Pet pet; private TextView name, descript

最近我在网上读到了关于mvp模式的文章,说实话,这有点让人困惑

我试图使用mvp模式重新编写一个应用程序,但我使用AsyncTask时遇到了一个问题,即我不知道如何将Interactor中的对象传递回视图

代码如下:

查看:

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());
    }
}
public class DetailsPresenter {

private DetailsInteractor interactor;

public Pet getPet(int idList, int id) {

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

    return interactor.pet;
}
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;
}
}

演示者:

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());
    }
}
public class DetailsPresenter {

private DetailsInteractor interactor;

public Pet getPet(int idList, int id) {

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

    return interactor.pet;
}
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;
}
}

交互者:

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());
    }
}
public class DetailsPresenter {

private DetailsInteractor interactor;

public Pet getPet(int idList, int id) {

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

    return interactor.pet;
}
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;
}
公共类详细信息插入器扩展异步任务{
公众宠物;
私人游手好闲;
私有int-id;
专用数据库;
public DetailsInteractor(int idList,int id){
this.idList=idList;
this.id=id;
database=newdbaction();
}
@凌驾
受保护的宠物doInBackground(字符串…字符串){
pet=database.requestItem(idList,id);
归还宠物;
}
我需要在从数据库获取数据后,它使用对象Pet更新视图


欢迎您提供任何答案和建议,谢谢!

当您在“活动”中创建/初始化演示者时(这是您的视图),您应该将视图传递给演示者。类似于以下内容

 DetailsPresenter presenter = new DetailsPresenter(View view);
视图可以是任何对象,您可以使用它更新UI或调用活动中的方法

此外,你必须通过一些更好的网站来了解MVP


这是一个非常好的链接。

谢谢@lib4我读了你给我的链接,它非常有用。我还有一个问题,因为我正在使用一个AsyncTask,当AsyncTask完成后,我该如何执行该方法?否则,每当我尝试更新TextView时,我都会得到一个NullPointer。请您投票支持我的答案并接受。这样它将帮助其他人同样。谢谢。我在错误地完成之前发布了评论,我更新了ittry以在onPostExecute示例中执行它,如下所示受保护的void onPostExecute(长结果){update UI/}请投票并接受答案,因为它将帮助其他人。谢谢