"Listviewjson" "mainactivity functional analysis, does not discuss the implementation of the specific tool class" "Android Parse JSON data (including asynchronous cache processing of picture data) and load it into the ListView"

Source: Internet
Author: User

Recently encountered a fairly good, beginner-friendly project that describes how to parse JSON data and load it into a ListView.

But personally, this tool can be used in all future implementations of similar functions.

Projects can also use this architecture.

This project is dealing with only one imgurl per news, and should be considered when the number of Imgurl is uncertain how to fix it.

First, the project source code structure is as follows:

Project DOWNLOAD Link: http://download.csdn.net/download/y562810463/8004245

The Com.demo.app.common package in this project can be used in other projects, and the Com.demo.api,com.demo.app package is also very valuable for reference.

Run the result diagram first:

Let's start by explaining the running logic of the program:

Inheriting the appcontext of the application class is the application of the real portal, but the Appcontext OnCreate method calls Super.oncreate (),

So enter from the mainactivity of Com.demo.app.ui.

Mainactivity.java code as well as logical analysis:

 PackageCom.demo.app.ui;Importjava.util.List;Importandroid.app.Activity;ImportAndroid.app.ProgressDialog;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportCom.demo.app.AppContext;Importcom.demo.app.AppException;ImportCOM.DEMO.APP.R;ImportCom.demo.app.adapter.MainListViewAdapter;Importcom.demo.app.bean.News;Importcom.demo.app.bean.NewsList;ImportCom.demo.app.common.UIHelper;ImportCom.demo.app.widget.MyListView; Public classMainactivityextendsActivity {PrivateMylistview ListView; PrivateList<news>newslist; PrivateAppContext AppContext;//Global Context    PrivateMainlistviewadapter Listviewadapter; PrivateProgressDialog Selectordialog; PrivateButton bt_news, Bt_forum, Bt_cartype, Bt_favour, Bt_more; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub        Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); AppContext=(AppContext) getapplication (); //Network connection Judgment        if(!appcontext.isnetworkconnected ()) Uihelper.toastmessage ( This, r.string.network_not_connected);        Init ();    InitData (); }    Private voidinit () {bt_news=(Button) Findviewbyid (r.id.bt_news); Bt_forum=(Button) Findviewbyid (R.id.bt_forum); Bt_cartype=(Button) Findviewbyid (R.id.bt_cartype); Bt_favour=(Button) Findviewbyid (R.id.bt_favour); Bt_more=(Button) Findviewbyid (R.id.bt_more); Bt_news.setselected (true);        Bt_news.setonclicklistener (OnClick (bt_news));        Bt_forum.setonclicklistener (OnClick (Bt_forum));        Bt_cartype.setonclicklistener (OnClick (Bt_cartype));        Bt_favour.setonclicklistener (OnClick (Bt_favour));        Bt_more.setonclicklistener (OnClick (Bt_more)); ListView=(Mylistview) Findviewbyid (R.id.news_listview); Selectordialog= Progressdialog.show ( This,NULL, "Loading, please wait ...",true,                false); }    PrivateView.onclicklistener OnClick (FinalButton btn) {        return NewView.onclicklistener () { Public voidOnClick (View v) {bt_news.setselected (false); Bt_forum.setselected (false); Bt_cartype.setselected (false); Bt_favour.setselected (false); Bt_more.setselected (false); if(BTN = =bt_news) {bt_news.setselected (true); } Else if(BTN = =bt_forum) {bt_forum.setselected (true); } Else if(BTN = =Bt_cartype) {bt_cartype.setselected (true); } Else if(BTN = =bt_favour) {bt_favour.setselected (true); } Else if(BTN = =Bt_more) {bt_more.setselected (true);    }            }        }; } Handler Mhandler=NewHandler () { Public voidhandlemessage (Message msg) {selectordialog.cancel (); if(Msg.what = = 1) {newslist= (list<news>) Msg.obj; Listviewadapter=NewMainlistviewadapter (mainactivity. This, newslist);            Listview.setadapter (Listviewadapter); } Else if(Msg.what = =-1) {uihelper.toastmessage (mainactivity. This, "No Data"); } Else if(Msg.what = =-2) {uihelper.toastmessage (mainactivity. This, r.string.xml_parser_failed);    }        }    }; Private voidInitData () {selectordialog.show (); NewThread () { Public voidrun () {Message msg=NewMessage (); BooleanIsrefresh =false; Try{newslist list=appcontext.getnewslist (); if(List.getnewscount () > 0) {Msg.what= 1; Msg.obj=list.getnewslist (); Appcontext.saveobject (list,"Newslist_"); } Else{msg.what=-1; }                } Catch(appexception e) {e.printstacktrace (); Msg.what=-2; Msg.obj=e;            } mhandler.sendmessage (msg);    }}.start (); }}

From the Mainactivity function OnCreate start analysis:

1. First generate Appcontext, then use the various possible methods defined in Appcontext.

AppContext = (appContext) getapplication ();

2, the network judgment, wherein Uihelper is a tool class

if (! appcontext.isnetworkconnected ())            Uihelper.toastmessage (this, r.string.network_not_connected);

3, the implementation of the Init, InitData method, the Init method is relatively simple, the key is the InitData method.

Init (); InitData ();

4, Next analysis InitData method. Because InitData methods are related to handler in mainactivity, they are analyzed together to analyze their effects.

Handler Mhandler =NewHandler () { Public voidhandlemessage (Message msg) {selectordialog.cancel (); if(Msg.what = = 1) {newslist= (list<news>) Msg.obj; Listviewadapter=NewMainlistviewadapter (mainactivity. This, newslist);            Listview.setadapter (Listviewadapter); } Else if(Msg.what = =-1) {uihelper.toastmessage (mainactivity. This, "No Data"); } Else if(Msg.what = =-2) {uihelper.toastmessage (mainactivity. This, r.string.xml_parser_failed);    }        }    }; Private voidInitData () {selectordialog.show (); NewThread () { Public voidrun () {Message msg=NewMessage (); BooleanIsrefresh =false; Try{newslist list=appcontext.getnewslist (); if(List.getnewscount () > 0) {Msg.what= 1; Msg.obj=list.getnewslist (); Appcontext.saveobject (list,"Newslist_"); } Else{msg.what=-1; }                } Catch(appexception e) {e.printstacktrace (); Msg.what=-2; Msg.obj=e;            } mhandler.sendmessage (msg);    }}.start (); }

Use the InitData method to start a thread that gets the list<news> data to load by using the Appcontext.getnewslist () method.

While Appcontext.getnewslist () uses the Apiclient.getnewslist () method, the Apiclient.getnewslist () method involves getting a JSON string over the network and converting the JSON to a list <News>.

If the list<news> succeeds, it is placed in the domain obj of message msg and the MSG domain, what is set to 1. returned to handler.

Note the News picture in:list<news> is a URL.

When handler obtains a LIST<NEWS>, it establishes the Mainlistviewadapter adapter, which parses the data into the ListView. One thing to note: This adapter uses Bitmapmanager to convert the URL of a picture to a picture.

It is also important to note that the application will have the cached data available to it. and adapter will load in the GetView.

The next blog specifically discusses the role and implementation of its tool classes.

"Listviewjson" "mainactivity functional analysis, does not discuss the implementation of the specific tool class" "Android Parse JSON data (including asynchronous cache processing of picture data) and load it into the ListView"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.