Android uses MVP mode to save and display webview history records. androidwebview
MVP is a common design mode on android. In Launhcer, it is often seen in Mobile browsers. I have observed what other great gods wrote. Now, I will write a demo with a help.
If you have any questions, please submit them.
We will discuss it together.
Demo; http://download.csdn.net/detail/xufeifandj/8267619
Demo is mainly a webview on the home page, which allows you to add and display historical records. The example is relatively simple.
The structure of the entire project is as follows:
HistoryPresenter. java mainly controls the view and model, which is equivalent to the control layer in MVC.
The specific implementation of adding and removing historical records and other business logic is implemented in HistoryModel. UI. java is an interface that submits data to the UI through business logic analysis to notify MainAcitivy to update the View.
Package com. ferris. ferrismvp. presenter; import java. util. list; import com. ferris. ferrismvp. UI; import com. ferris. ferrismvp. beam. history; import com. ferris. ferrismvp. model. historyModel; import com. ferris. ferrismvp. model. IHistoryModel; public class HistoryPresenter {private UI ui; private IHistoryModel iHistoryModel; public HistoryPresenter (UI ui) {// TODO Auto-generated constructor stubthis. ui = ui; this. iHistoryModel = new HistoryModel ();}/*** add History record * @ param hisString */public void AddHistory (History hisString) {iHistoryModel. addHistory (hisString);}/*** Delete History * @ param hisString */public void removeHistory (History hisString) {iHistoryModel. removeHistory (hisString);}/*** display all History records */public void showHistorys () {List <History> histories = iHistoryModel. showAllHistory (); ui. showAllHistory (histories );}}
The business logic interface of the historical record Model. Three methods are added, deleted, and queried.
package com.ferris.ferrismvp.model;import java.util.List;import com.ferris.ferrismvp.beam.History;public interface IHistoryModel {public void addHistory(History history);public void removeHistory(History history);public List<History> showAllHistory();}
Implementation Logic
package com.ferris.ferrismvp.model;import java.util.ArrayList;import java.util.List;import com.ferris.ferrismvp.beam.History;public class HistoryModel implements IHistoryModel{private List<History> historys=new ArrayList<History>();public HistoryModel() {// TODO Auto-generated constructor stub}@Overridepublic void addHistory(History history) {// TODO Auto-generated method stubhistorys.add(history);}@Overridepublic void removeHistory(History history) {// TODO Auto-generated method stubint removeId=-1;for(int i=0;i
History Bean object
package com.ferris.ferrismvp.beam;public class History {private String title;private String url;public History() {// TODO Auto-generated constructor stub}public History(String title,String url) {// TODO Auto-generated constructor stubthis.title=title;this.url=url;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}}
UI Interface
package com.ferris.ferrismvp;import java.util.List;import com.ferris.ferrismvp.beam.History;public interface UI {public void showAllHistory(List<History> historys);}
Package com. ferris. ferrismvp; import java. util. list; import com. ferris. ferrismvp. beam. history; import com. ferris. ferrismvp. presenter. historyPresenter; import android. OS. bundle; import android. support. v7.app. actionBarActivity; import android. view. view; import android. view. view. onClickListener; import android. webkit. webView; import android. widget. button; import android. widget. toast; public class MainActivity extends ActionBarActivity implements OnClickListener, UI {private WebView webView1; private Button addHistory, showHistory; private synchronized; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); findView (); initWebView (); historyPresenter = new HistoryPresenter (this);} private void initWebView () {// TODO Auto-generated method stubwebView1.getSettings (). setJavaScriptEnabled (true); webView1.loadUrl ("http://www.baidu.com");} private void findView () {// TODO Auto-generated method stubwebView1 = (WebView) findViewById (R. id. webView1); addHistory = (Button) findViewById (R. id. button1); showHistory = (Button) findViewById (R. id. button2); addHistory. setOnClickListener (this); showHistory. setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubswitch (v. getId () {case R. id. button1: // Add historyPresenter. addHistory (new History (webView1.getTitle (), webView1.getUrl (); break; case R. id. button2: // display the historyPresenter history. showHistorys (); break; default: break; }}@ Overridepublic void showAllHistory (List <History> historys) {// TODO Auto-generated method stubToast. makeText (MainActivity. this, historys. toString (), Toast. LENGTH_SHORT ). show ();}}