简体   繁体   English

MVP方法-Android

[英]MVP approach - Android

I am trying to implement MVP architecture in a sample android project . 我正在尝试在示例android项目中实现MVP架构。

Could you recommend how I can break this code down for a better approach on MVP. 您能否建议我如何分解此代码,以获得对MVP更好的方法。

I can create an Interface to have setContactInfo() method but could not think of any other approach. 我可以创建一个具有setContactInfo()方法的接口,但是没有想到任何其他方法。

This is detailView of the List item from the list view. 这是列表视图中列表项的detailView。 I am getting the data in a Bundle from another activity. 我正在从另一个活动中获取捆绑包中的数据。

Thank you for your advice. 感谢您的意见。

  package com.salesi.coding;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ContactDetails extends AppCompatActivity {

    @Bind(R.id.tvTitle) protected TextView mTitle;
    @Bind(R.id.tvFirstName) protected TextView mFirstName;
    @Bind(R.id.tvLastName) protected TextView mLastName;
    @Bind(R.id.tvJobTitle) protected TextView mJobTitle;
    @Bind(R.id.tvPhoneNUmber) protected TextView mPhoneNUmber;
    @Bind(R.id.tvEmail) protected TextView mEmail;
    @Bind(R.id.tvHobbies) protected TextView mHobbies;
    @Bind(R.id.tvAddressLine1) protected TextView mAddressLine1;

    Intent contactIntent;
    Bundle contactBundle;
    String title, firstName, lastName,jobTitle, phoneNumber, email, address, hobbies;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_details);

        ButterKnife.bind(this);

        //Get the Bundle data.
        contactIntent = getIntent();
        contactBundle = contactIntent.getExtras();

        hobbies = contactBundle.getString("hobbies");
        title = contactBundle.getString("title");
        firstName = contactBundle.getString("firstName");
        lastName = contactBundle.getString("lastName");
        jobTitle = contactBundle.getString("jobTitle");
        phoneNumber = contactBundle.getString("phoneNumber");
        email = contactBundle.getString("email");
        address = contactBundle.getString("address");
        setContactInfo();

    }

    private void setContactInfo(){
        mTitle.setText(title);
        mFirstName.setText(firstName);
        mLastName.setText(lastName);
        mJobTitle.setText(jobTitle);
        mPhoneNUmber.setText(phoneNumber);
        mEmail.setText(email);
        mHobbies.setText(hobbies.toString());
        mAddressLine1.setText(address.toString());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.exitApp:
                finish();
                break;

            default:
        }
        return true;
    }
}

So, before MVP became really popular, Android projects were written in a manner in which the activity was too heavy(doing too much work). 因此,在MVP真正流行之前,Android项目是以活动过于繁重(做太多工作)的方式编写的。 Activities would be responsible for rendering UI, handling touch events, making API calls, having callbacks for said API calls etc. 活动将负责呈现UI,处理触摸事件,进行API调用,为所述API调用提供回调等。

One of the reasons why MVP became popular was because it introduced separation of concerns into Android code. MVP受欢迎的原因之一是因为它将关注点分离引入了Android代码。 Basically, in MVP the activity/fragment(the View) is incharge of only the view related aspects, while things like making API calls and handling the callbacks were removed and moved to the presenter. 基本上,在MVP中,活动/片段(视图)仅负责与视图相关的方面,而诸如进行API调用和处理回调的操作则已删除并移至演示者。

The code you provided above is actually fine as it is and doesn't require a presenter. 您上面提供的代码实际上是可以的,不需要演示者。

Here's a scenario of when you would use MVP: 这是您何时使用MVP的方案:

The contact details are coming from your backend server. 联系人详细信息来自您的后端服务器。 ContactDetailsActivity would inform ContactDetailsPresenter that the details are required. ContactDetailsActivity会通知ContactDetailsPresenter需要详细信息。 ContactDetailsPresenter would make a GET request to the backend server. ContactDetailsPresenter会向后端服务器发出GET请求。 Once ContactDetailsPresenter gets a successful response from the server, it would inform the ContactDetailsActivity that the details have been fetched and would provide the necessary details to ContactDetailsActivity. 一旦ContactDetailsPresenter从服务器成功获得响应,它将通知ContactDetailsActivity该详细信息已被获取,并将向ContactDetailsActivity提供必要的详细信息。 ContactDetailsActivity would update the UI with the details. ContactDetailsActivity将使用详细信息更新UI。

For an MVP (model-view-presenter) architectural pattern you should have : A model , A VIEW , A presenter 对于MVP(模型视图演示者)架构模式,您应该具有:模型,视图,演示者

first of all you have the model part and you have this correctly done which is the xml files , 首先,您具有模型部分,并且已正确完成了XML文件,

second a view and it's the place to bind the attributes to the model parts which you are doing in the code above 第二个视图,这是将属性绑定到您在上面的代码中所做的模型零件的地方

Third the pattern which is the place you should retrieve info , so what you can do is do a separate class in which you put the methods above import it in your class and call the methods 第三种模式是您应该检索信息的位置,因此您可以做的是做一个单独的类,在该类中,您将上面的方法导入到您的类中并调用这些方法

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

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