MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。MVC被独特的发展起来用于映射传统的输入、处理和输出功能在一个逻辑的图形化用户界面的结构中。
下面是我个人写的MVC
整体的项目架构
mvc.png
FoodBean 食物实体类
FoodListener 逻辑处理完成 通知View层
FoodModule 这个接口是C层和M层用来交互的
FoodModuleImpl M层用来进行网络请求的是FoodMdule的实现类
以下是代码
- 布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/foodListView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
-
MainActivity类
import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import androidx.appcompat.app.AppCompatActivity; import com.example.h0802.module.FoodListener; import com.example.h0802.module.FoodModule; import com.example.h0802.module.FoodModuleImpl; import java.util.List; public class MainActivity extends AppCompatActivity implements FoodListener { private ListView foodListView; private FoodModule foodModule; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); foodListView = findViewById(R.id.foodListView); foodModule = new FoodModuleImpl(); foodModule.getString("http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1", this); } @Override public void getList(final List<String> list) { runOnUiThread(new Runnable() { @Override public void run() { if (list == null) { return; } foodListView.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, list)); } }); } }
-
FoodModule类
public interface FoodModule { void getString(String address, FoodListener foodListener); }
-
FoodModuleImpl
import com.alibaba.fastjson.JSON; import com.example.h0802.bean.FoodBean; import java.io.IOException; import java.util.ArrayList; import java.util.concurrent.TimeUnit; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class FoodModuleImpl implements FoodModule { @Override public void getString(String address, final FoodListener foodListener) { OkHttpClient client = new OkHttpClient.Builder() .callTimeout(5, TimeUnit.SECONDS) .readTimeout(5, TimeUnit.SECONDS) .build(); Request request = new Request.Builder() .url(address) .get() .build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { ArrayList<String> datas = new ArrayList<>(); FoodBean foodBean = JSON.parseObject(response.body().string(), FoodBean.class); for (FoodBean.DataBean food : foodBean.getData()) { datas.add(food.getTitle()); } foodListener.getList(datas); } }); } }
-
FoodListener
import java.util.List; public interface FoodListener { void getList(List<String> list); }
-
效果图
MVC效果图.png