标签:
以前大家开发android应用用的都是MVC,但是会感觉到M和V在Activity中的频繁交互使得代码变得臃肿,代码行数
很容易达到上千行。不仅如此,MVC的使用使得代码得可阅读性和可维护性已经可扩展性非常低,一些小功能的增加、
删除、修改和查询非常艰难,尤其是代码转接给别人维护时。
MVP模式的出现很好地解决了这些问题,你会发现用了MVP模式以后,再也不想用MVC模式了。MVP模式的代码包结
构如图:
M层的接口写未实现的逻辑方法,用M层的实现类来实现所有的逻辑方法;V层的接口写未实现的界面交互的方
法,用V层的实现类即Activity或者Fragment来实现所有的界面交互的方法;P层的类中通过构造方法来实现M和V的沟
通和交互,P其实就是中介者的身份。这样使得M和V完全分开,各司其职。
SharedPreferences存储数据需要SharedPreferences对象和Editor对象,Editor对象存储数据,
SharedPreferences对象读取数据。
程序运行效果如图:
代码如下:
1.界面布局activity_main:
<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn_jsonArray"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="生成JSONArray" />
<TextView
android:id="@+id/tv_jsonArrayContent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:gravity="center"
android:text="@string/hello_world"
android:textSize="20sp" />
<Button
android:id="@+id/btn_jsonArrayParse"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="解析JSONArray" />
<TextView
android:id="@+id/tv_jsonArrayParseContent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:gravity="center"
android:text="@string/hello_world"
android:textSize="20sp" />
</LinearLayout></span>
<span style="font-size:18px;">package com.msd.spsavejsonarray.model.interfaces;
/*
* 主界面模型接口
*/
public interface MainModelInterface {
/*
* 生成JSONArray
*/
String generateSpSaveJSONArray();
/*
* 解析JSONArray
*/
String parseSpSaveJSONArray(String jsonArrayContent);
}
</span>
<span style="font-size:18px;">package com.msd.spsavejsonarray.model.classes;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.msd.spsavejsonarray.model.interfaces.MainModelInterface;
/*
* 主界面模型接口的实现类
*/
public class MainModelClass implements MainModelInterface {
/*
* (non-Javadoc)生成JSONArray的实现
*
* @see com.msd.spsavejsonarray.model.interfaces.MainModelInterface#
* generateSpSaveJSONArray()
*/
@Override
public String generateSpSaveJSONArray() {
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject;
for (int i = 0; i < 5; i++) {
jsonObject = new JSONObject();
try {
jsonObject.put("id", "id_" + i);
jsonObject.put("name", "name_" + i);
jsonArray.put(jsonObject);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return jsonArray.toString();
}
/*
* (non-Javadoc)解析JSONArray的实现,返回字符串
*
* @see com.msd.spsavejsonarray.model.interfaces.MainModelInterface#
* parseSpSaveJSONArray(java.lang.String)
*/
@Override
public String parseSpSaveJSONArray(String jsonArrayContent) {
StringBuilder stringBuilder = new StringBuilder();
try {
JSONArray jsonArray = new JSONArray(jsonArrayContent);
JSONObject jsonObject;
for (int i = 0; i < jsonArray.length(); i++) {
jsonObject = (JSONObject) jsonArray.get(i);
stringBuilder.append("第" + i + "项:" + "id为"
+ jsonObject.getString("id") + " name为"
+ jsonObject.getString("name") + "\n");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stringBuilder.toString();
}
}
</span>
<span style="font-size:18px;">package com.msd.spsavejsonarray.view.interfaces;
/*
* 主界面view接口
*/
public interface MainViewInterface {
/*
* 显示生成的JSONArray
*/
void showGenerateJSONArray(String jsonArrayString);
/*
* 显示解析的JSONArray
*/
void showParseJSONArray(String jsonArrayParseString);
}
</span>
<span style="font-size:18px;">package com.msd.spsavejsonarray.view.classes;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import com.msd.spsavejsonarray.R;
import com.msd.spsavejsonarray.presenter.MainPresenter;
import com.msd.spsavejsonarray.view.interfaces.MainViewInterface;
/*
* 主界面View接口实现类
*/
public class MainActivity extends Activity implements OnClickListener,
MainViewInterface {
private SharedPreferences sharedPreferences;// sharedPreferences对象
private static Editor editor;// Editor对象
private String spName = "SPSAVEJSONARRAY";// SP存储的文件名称
private MainPresenter mainPresenter;// 主界面presenter对象
private TextView tv_jsonArrayContent;// 显示生成的JSONArray的TextView
private TextView tv_jsonArrayParseContent;// 显示解析的JSONArray的TextView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainPresenter = new MainPresenter(this);
sharedPreferences = getSharedPreferences(spName, MODE_PRIVATE);// 私有模式
editor = sharedPreferences.edit();
tv_jsonArrayContent = (TextView) findViewById(R.id.tv_jsonArrayContent);
tv_jsonArrayParseContent = (TextView) findViewById(R.id.tv_jsonArrayParseContent);
findViewById(R.id.btn_jsonArray).setOnClickListener(this);
findViewById(R.id.btn_jsonArrayParse).setOnClickListener(this);
}
/*
* 如果在Activity中引用presenter执行耗时操作,而Activity被销毁时,presenter对象会一直持有Activity对象,
* 有可能引起内存泄漏,所以执行保险操作,在onDestroy()中清空Activity对presenter对象的引用,
* 或者清空prensenter对象对Acitvity对象的引用(non-Javadoc)
*
* @see android.app.Activity#onDestroy()
*/
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mainPresenter = null;
}
/*
* 点击事件
*
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_jsonArray:// 生成JSONArray
mainPresenter.generateSpSaveJSONArray();
break;
case R.id.btn_jsonArrayParse:// 解析JSONArray
mainPresenter.parseSpSaveJSONArray();
break;
default:
break;
}
}
/*
* 显示生成的JSONArray(non-Javadoc)
*
* @see
* com.msd.spsavejsonarray.view.MainViewInterface#showGenerateJSONArray(
* java.lang.String)
*/
@Override
public void showGenerateJSONArray(String jsonArrayString) {
editor.putString(spName, jsonArrayString).commit();// 将生成的JSONArray存储起来
tv_jsonArrayContent.setText(jsonArrayString);
// tv_jsonArrayParseContent.setText(sharedPreferences.getString(spName,
// "[]"));//通过sp获取生成的JSONArray
}
/*
* 显示解析的JSONArray(non-Javadoc)
*
* @see
* com.msd.spsavejsonarray.view.MainViewInterface#showParseJSONArray(java
* .lang.String)
*/
@Override
public void showParseJSONArray(String jsonArrayParseString) {
tv_jsonArrayParseContent.setText(jsonArrayParseString);
}
}
</span>
<span style="font-size:18px;">package com.msd.spsavejsonarray.presenter;
import com.msd.spsavejsonarray.model.classes.MainModelClass;
import com.msd.spsavejsonarray.model.interfaces.MainModelInterface;
import com.msd.spsavejsonarray.view.interfaces.MainViewInterface;
/*
* 主界面presenter
*/
public class MainPresenter {
private MainModelInterface mainModelInterface;// 主界面Model接口实现类的对象
private MainViewInterface mainViewInterface;// 主界面View接口实现类的对象,即MainActivity
/*
* 构造方法实现M和V的交互
*/
public MainPresenter(MainViewInterface mainViewInterface) {
mainModelInterface = new MainModelClass();
this.mainViewInterface = mainViewInterface;
}
/*
* 生成JSONArray
*/
public void generateSpSaveJSONArray() {
mainViewInterface.showGenerateJSONArray(mainModelInterface
.generateSpSaveJSONArray());
}
/*
* 解析JSONArray
*/
public void parseSpSaveJSONArray() {
mainViewInterface.showParseJSONArray(mainModelInterface
.parseSpSaveJSONArray(mainModelInterface
.generateSpSaveJSONArray()));
}
}
</span>总结:以上代码看起来非常小题大做,但是到了真正做项目的时候,如果界面比较复杂, 界面交互要素较多的时候,MVP的优点就完全展示出来了,Activity中上千行的代码变得条理清楚,非常精简,非常容易维护和修改。
代码下载:
小白学习Android MVP和SharedPreferences存储JSONArray
标签:
原文地址:http://blog.csdn.net/confusing_awakening/article/details/51361660