Found ---- Android Demo
It's been three months since I started my internship in a twinkling of an eye. In the past three months, I 've been growing slowly, from my initial e-commerce project to my current car sector project, while having gone through one project after another, Xiao Bian has gone through one miracle to defeat himself. Every time he encounters a new technical point, Xiao Bian starts to play tricks. It really requires courage, when the function comes out, you can get it only when you do it, rather than when you have the ability, come on, girl. Friends may have seen the discovery feature on some apps, and click the find button to automatically load the most popular information. This feature is available in the most recent project by xiaobian, after several days of experience, I finally got a fresh one. Today, this blog editor will briefly introduce how to click the Discover button to automatically load the information we need, hope to help the friends who need it, and ask the friends for more advice '(* Your _ friends *)′.
First, we need to use two entities to compile our entity layer. The first content is as follows:
Package com. jczb. car. bean; import java. util. date; import java. util. list; import android. r. integer; import android. r. integer;/*** Description: Content entity of a section * Author: Ding Guohua * Time: 10:41:43, December 3, September 10, 2015 */@ SuppressWarnings (serial) public class Content extends Entity {/** Content table ID */private int uid;/** display title */private String title; /** video or image path */private String path;/** details */private String details;/** channel type */private String channelType; /** channel name */private String channelName;/** author */private String author;/** recommended? */private String isRecommend; /** read or play count */private int browseNumber;/** comment count */private int commentbrowseNumber;/** likes */private int praiseNumber; /** step count */private int treadNumber;/** release time */private String issueTime;/** add to favorites */private int isCollect; /** associate video */private List
Content; public String getTitle () {return title;} public void setTitle (String title) {this. title = title;} public String getPath () {return path;} public void setPath (String path) {this. path = path;} public String getDetails () {return details;} public void setDetails (String details) {this. details = details;} public String getChannelType () {return channelType;} public void setChannelType (String channelType) {this. channelType = channelType;} public String getAuthor () {return author;} public void setAuthor (String author) {this. author = author;} public String getIsRecommend () {return isRecommend;} public void setIsRecommend (String isRecommend) {this. isRecommend = isRecommend;} public int getBrowseNumber () {return browseNumber;} public void setBrowseNumber (int browseNumber) {this. browseNumber = browseNumber;} public int getCommentbrowseNumber () {return commentbrowseNumber;} public void setCommentbrowseNumber (int commentbrowseNumber) {this. commentbrowseNumber = commentbrowseNumber;} public int getPraiseNumber () {return praiseNumber;} public void setPraiseNumber (int praiseNumber) {this. praiseNumber = praiseNumber;} public int getTreadNumber () {return treadNumber;} public void setTreadNumber (int treadNumber) {this. treadNumber = treadNumber;} public String getIssueTime () {return issueTime;} public void setIssueTime (String issueTime) {this. issueTime = issueTime;} public String getChannelName () {return channelName;} public void setChannelName (String channelName) {this. channelName = channelName;} public int getIsCollect () {return isCollect;} public void setIsCollect (int isCollect) {this. isCollect = isCollect;} public List
GetContent () {return Content;} public void setContent (List
Content) {Content = content;} public int getUid () {return uid;} public void setUid (int uid) {this. uid = uid ;}}
The second object contentVo, the specific code is as follows:
Package com. jczb. car. bean; import java. util. list;/*** Description: content set for parsing Json strings * Author: Ding Guohua * Time: 10:38:38, January 1, September 10, 2015 */@ SuppressWarnings (serial) public class ContentVo extends Entity {/** server return result: true or false */private String result;/** the Json String returned by the server is parsed to List and combined with */private List
Content; public String getResult () {return result;} public void setResult (String result) {this. result = result;} public List
GetContent () {return Content;} public void setContent (List
Content) {Content = content ;}}
Next, compile the interface method that needs to be called. The Code is as follows:
/*** Note: @ user Ding Guohua * @ date 2:10:59 */public ContentVo discover (boolean isFirst, int contentVoId) throws AppException {ContentVo contentVo = null; // construct the keyString key of the cache file = contentVo _ + contentVoId; // 1. if the network is connected, obtain data from the server first. if (isNetworkConnected () & (isReadDataCache (key) | isFirst) {try {// obtain the Content set contentVo = ApiClient from the server. discover (this); // if you can obtain data from the server, save it to the local cache. Only when the data is not empty and the obtained result is t If (contentVo! = Null & contentVo. getResult (). equals (false) {contentVo. setCacheKey (key); saveObject (contentVo, key) ;}} catch (AppException e) {// If a network disconnection occurs during access, obtain the data in the local cache contentVo = (ContentVo) readObject (key); // if the data in the local cache is null, an exception is thrown if (contentVo = null) {throw e ;}}// 2. if not, else {contentVo = (ContentVo) readObject (key); if (contentVo = null) {contentVo = new ContentVo ();} return contentVo ;}Step 3: write our xml layout. The specific code is as follows:
Step 4: Compile the Code required in the java class as follows:
Package com. jczb. car. ui; import java. util. arrayList; import java. util. hashMap; import java. util. list; import java. util. map; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. view; import android. view. view. onClickListener; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. editText; import android. widget. imageView; import android. widget. listView; import android. widget. relativeLayout; import android. widget. simpleAdapter; import android. widget. textView; import android. widget. toast; import com. jczb. car. appContext; import com. jczb. car. appException; import com. jczb. car. r; import com. jczb. car. bean. content; import com. jczb. car. bean. contentVo;/*** Note: Discovery Function page Author: Ding Guohua * Time: 2:02:58 */public class DiscoveryActivity extends Activity implements OnClickListener, OnItemClickListener {// RelativeLayout rlFoundSearch; // ImageView ivTuBiaoSearch; // declare several controls used: private TextView etSearch; private ImageView ivSearch; private ListView lvHotChannel; private RelativeLayout rlFoundSearchEmpty; // private ImageView ivTuBiao; /** resolution finds the object class used by the interface */private ContentVo contentVo = null;/** fill in the List of ListView */private List
> Data = new ArrayList
> ();/** Used to obtain the server interface's discovery content set */private List
DiscoverContent; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. discovery); findViewById (); // initialize the page control initView ();/* jump to the blank page of the Search Keyword */rlFoundSearchEmpty. setOnClickListener (this); // ivTuBiao. setOnClickListener (this); // start thread to obtain server data discoverThread. start ();} private void findViewById () {// TODO Auto-generated method stubrlFoundSearchEmpty = (RelativeLayout) findViewById (R. id. rl_search_id); // ivTuBiao = (ImageView) findViewById (R. id. iv_search_tubiao_id);} @ Overridepublic void onClick (View v) {switch (v. getId () {/* case R. id. iv_search_tubiao_id: break; */case R. id. rl_search_id: Intent intentFoundSerch = new Intent (this, SearchResourceActivity. class); startActivity (intentFoundSerch); break; default: break;}/*** Note: Control initialization and listening events * Author: Ding Guohua * Time: 2:12:49 */public void initView () {// initialize the control etSearch = (TextView) findViewById (R. id. et_seachkey_id); ivSearch = (ImageView) findViewById (R. id. iv_search_id); lvHotChannel = (ListView) findViewById (R. id. lv_hotchannel_id); // rlFoundSearch = (RelativeLayout) findViewById (R. id. rl_search_id); // initialize the listening event ivSearch. setOnClickListener (this); // to implement click events in ListView, this Activity must implement the OnItemClickListener interface lvHotChannel. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView
Arg0, View arg1, int position, long arg3) {// after clicking the item, you can obtain the parameters to be carried Based on the clicked position to jump to the Intent intent = new Intent (DiscoveryActivity. this, VedioInfoActivity. class); intent. putExtra (channelType, discoverContent. get (position ). getChannelType (); intent. putExtra (Id, discoverContent. get (position ). getId (); startActivity (intent) ;}}) ;};/*** Note: Get and load data from the server to ListView * Author: Ding Guohua * Time: 2:54:27 */public void loadData () {}/*** defines a thread wait */Handler handler = new Handler () {@ Overridepublic void handleMessage (Message msg) {super. handleMessage (msg); // you need to judge based on the return value of what in the msg passed in the thread. // the value of what in the message is 1, and the value of what is-1 is blank, the exception is 0 switch (msg. what) {case 1: // obtain the server data contentVo = (ContentVo) msg obtained by the thread. obj; discoverContent = contentVo. getContent (); // assign data to List
> Set for (int I = 0; I <discoverContent. size (); I ++) {Map
Map = new HashMap
(); // Map. put (Id, discoverContent. get (I ). getId (); // map. put (channelType, discoverContent. get (I ). getChannelType (); map. put (title, discoverContent. get (I ). getTitle (); map. put (channelName, discoverContent. get (I ). getChannelName (); data. add (map );} // define the Adapter that fills the ListView and fill it in the ListView // String [] {} and the name entered in the map must be consistent with SimpleAdapter contentAdapter = new SimpleAdapter (DiscoveryActivity. this, data, R. layout. discovery_ I Tem, new String [] {title, channelName}, new int [] {R. id. TV _channelTopNews_id, R. id. TV _channelName_id}); lvHotChannel. setAdapter (contentAdapter); break; case-1: Toast. makeText (DiscoveryActivity. this, data is empty !, Toast. LENGTH_LONG). show (); break; case 0: Toast. makeText (DiscoveryActivity. this, data acquisition failed !, Toast. LENGTH_LONG ). show (); break; default: break ;}};/*** enable a subthread for discovery */Thread discoverThread = new Thread (new Runnable () {@ Overridepublic void run () {Message msg = new Message (); // obtain the Global Object ApplicationAppContext appContext = (AppContext) getApplication (); try {// get server data contentVo = appContext. discover (true, 1); // If contentVo is returned, the value of what is in the message is 1, the value of what is null is-1, and the exception is 0if (contentVo! = Null) {msg. what = 1; msg. obj = contentVo;} else {msg. what =-1 ;}} catch (AppException e) {msg. what = 0; e. printStackTrace ();} handler. sendMessage (msg) ;}});/*** Note: different Toast prompts * Author: Ding Guohua * Time: 3:53:30 */public void showToast (int what) {switch (what) {case-1: Toast. makeText (DiscoveryActivity. this, data is empty !, Toast. LENGTH_LONG). show (); break; case 0: Toast. makeText (DiscoveryActivity. this, data acquisition failed !, Toast. LENGTH_LONG). show (); default: break; }}@ Overridepublic void onItemClick (AdapterView
Arg0, View arg1, int arg2, long arg3) {// TODO Auto-generated method stub }}
At this point, we found a line of this function. Finally, let's take a look at our running effect, as shown below:
Note:The implementation function of the discovery is briefly introduced here, and the discovery is implemented. However, the small compilation is still in the fog, but it doesn't matter. The smaller the small compilation will be, the more brave it is, this is the meaning of life. For small editors, it is both a challenge and an opportunity, because knowledge is the same. Furthermore, in the mini-editor's program life, it leaves the most precious memories. Although later mini series may not be engaged in the android industry, there are many kinds of things in the Code world, some are sweet, some are warm, some are easy to sing, and some are endless, in these stories, the only thing we have in common is that a year, a month, and a period of time, I love you very much! Love you-during this internship, Android has brought various surprises to xiaobian. In the next blog, I will share it with my friends. Please wait for '(* Your _ blank *)' To learn how to enter keywords and search for corresponding content *)′!