Go on with your study yesterday.
Yesterday we created a new view based on the weather data obtained to display the contents, so today we will show the data!!!
Here we have to link the data and the view, then we use the adapter-adapter,android to provide us with a lot of Adapter, here we use the baseadapter.
Baseadapter (1)
Right click on Src/com.demo.weather, select New > Class, follow the fill:
After selecting [Finish], we create a new subclass of Baseadapter, open the Weatheradapter.java file to explain the code,
public class Weatheradapter extends baseadapter{ @Override public int GetCount () { //TODO auto-generated method stub return 0; } @Override public Object getItem (int arg0) { //TODO auto-generated method stub return null; } @Override public long getitemid (int arg0) { //TODO auto-generated method stub return 0;
} @Override public View getView (int arg0, view arg1, ViewGroup arg2) { //TODO auto-generated Meth OD stub return null;} }
public int GetCount ()
This method returns a ListView with several data,
Public Object getItem (int arg0)
This method returns the data for the current row,
public long getitemid (int arg0)
This method returns the ID of the current row,
Public View getView (int arg0, view arg1, ViewGroup arg2)
This method returns the view of the current row,
So how do you relate the data to the view?
First, we need to convert the obtained data into a form where the code can be easily manipulated. To convert the JSON data to JavaBean, we need to use a Gson library, which can be downloaded here. http://code.google.com/p/google-gson/
Put the downloaded Gson-v2.jar into the Libs folder, and start our hard work, it will be a large piece of code.
Gson
Create a new package, named Com.demo.weather.bean, to add 4 classes under this package, Baidudata, Resultsbean, Indexbean, Weatherdatabean, respectively:
public class baidudata{ private int error; Private String status; private String date; Private list<resultsbean> results; public int GetError () { return error; } public void SetError (int error) { this.error = error; } Public String getStatus () { return status; } public void SetStatus (String status) { this.status = status; } Public String getDate () { return date; } public void SetDate (String date) { this.date = date; } Public list<resultsbean> GetResults () { return results; } public void SetResults (list<resultsbean> results) { this.results = results; }}
public class resultsbean{ private String currentcity; Private String PM25; Private list<indexbean> index; Private list<weatherdatabean> Weather_data; Public String getcurrentcity () { return currentcity; } public void setcurrentcity (String currentcity) { this.currentcity = currentcity; } Public String GETPM25 () { return pm25; } public void SetPm25 (String pm25) { this.pm25 = pm25; } Public list<indexbean> GetIndex () { return index; } public void Setindex (list<indexbean> index) { this.index = index; } Public list<weatherdatabean> Getweather_data () { return weather_data; } public void Setweather_data (list<weatherdatabean> weather_data) { this.weather_data = Weather_data; }}
public class indexbean{ private String title; Private String ZS; Private String Tipt; Private String des; Public String GetTitle () { return title; } public void Settitle (String title) { this.title = title; } Public String Getzs () { return ZS; } public void Setzs (String zs) { This.zs = ZS; } Public String Gettipt () { return tipt; } public void Settipt (String tipt) { this.tipt = tipt; } Public String getdes () { return des; } public void Setdes (String des) { this.des = des; }}
public class weatherdatabean{private String date; Private String Daypictureurl; Private String Nightpictureurl; Private String weather; Private String Wind; private String temperature; Public String GetDate () {return date; public void SetDate (String date) {this.date = date; } public String Getdaypictureurl () {return daypictureurl; } public void Setdaypictureurl (String daypictureurl) {this.daypictureurl = Daypictureurl; } public String Getnightpictureurl () {return nightpictureurl; } public void Setnightpictureurl (String nightpictureurl) {this.nightpictureurl = Nightpictureurl; } public String GetWeather () {return weather; } public void Setweather (String weather) {This.weather = weather; } public String Getwind () {return wind; } public void Setwind (String wind) {this.wind = wind; } publiC String Gettemperature () {return temperature; } public void Settemperature (String temperature) {this.temperature = temperature; }}
These 4 JavaBean host string-converted objects in JSON format.
Then, open the Mainactivity.java file, this file also has a small red fork, which is the result of the deletion of a text before we did not modify the code in a timely manner. Don't hesitate to erase it.
Modify the Http.send method to the following:
Http.send (Httpmethod.get, "Http://api.map.baidu.com/telematics/v3/weather", params, new requestcallback<string > () { @Override public void onsuccess (responseinfo<string> responseinfo) { String Weather = Responseinfo.result; Gson Gson = new Gson (); Baidudata data = Gson.fromjson (weather, baidudata.class); LOG.V ("OnFailure", data.tostring ()); } @Override public void OnFailure (HttpException arg0, String arg1) { log.v ("OnFailure", arg1); } } );
Here, let's start with a paragraph to explain so many of the code.
Although the code is many, but are very simple, baidudata, Resultsbean, Indexbean, Weatherdatabean these 4 classes are according to Gson requirements, combined with the Baidu Weather API return data made standard JavaBean.
Gson Gson = new Gson (); Baidudata data = Gson.fromjson (weather, baidudata.class);
These two lines are the core function that converts the JSON-formatted string into the baidudata of the JavaBean we made.
Baseadapter (2)
Now that the data has been formatted as JavaBean, the rest of the work is much simpler.
Modify Weatheradapter to associate the data with the view,
public class Weatheradapter extends baseadapter{private list<weatherdatabean> weathers; Private Layoutinflater Inflater; Private Bitmaputils bitmaputils; Public Weatheradapter (context context, list<weatherdatabean> weathers) {this.weathers = weathers; Inflater = Layoutinflater.from (context); Bitmaputils = new Bitmaputils (context); } @Override public int getcount () {return weathers.size (); } @Override public Object getItem (int position) {return weathers.get (position); } @Override public long getitemid (int position) {return position; } @Override public View getView (int position, view Convertview, ViewGroup parent) {Convertview = Inflat Er.inflate (r.layout.item_weather, NULL); TextView txtdate = (TextView) Convertview.findviewbyid (r.id.item_date); TextView Txtweather = (TextView) Convertview.findviewbyid (R.id.item_weather); TextView Txtwind = (TextView) Convertview.findviewbyid (R.id.item_wind); TextView txttemperature = (TextView) Convertview.findviewbyid (r.id.item_temperature); ImageView imgtemperature = (ImageView) Convertview.findviewbyid (r.id.item_picture); Weatherdatabean bean = (weatherdatabean) getItem (position); Txtdate.settext (Bean.getdate ()); Txtweather.settext (Bean.getweather ()); Txtwind.settext (Bean.getwind ()); Txttemperature.settext (Bean.gettemperature ()); Bitmaputils.display (Imgtemperature, Bean.getdaypictureurl ()); return convertview; }}
Here we use the Layoutinflater to load the Item_weather interface made yesterday, using the Bitmaputils to load the Web images. Although it is a large piece of code, but the reason is very simple, the corresponding data display to the corresponding project.
Then, we add a reference to the ListView and the Weatheradapter class we built earlier in Mainactivity.java, and associate the ListView, Weatheradapter, Baidudata three objects.
public class Weatheradapter extends baseadapter{private list<weatherdatabean> weathers; Private Layoutinflater Inflater; Private Bitmaputils bitmaputils; Public Weatheradapter (context context, list<weatherdatabean> weathers) {this.weathers = weathers; Inflater = Layoutinflater.from (context); Bitmaputils = new Bitmaputils (context); } @Override public int getcount () {return weathers.size (); } @Override public Object getItem (int position) {return weathers.get (position); } @Override public long getitemid (int position) {return position; } @Override public View getView (int position, view Convertview, ViewGroup parent) {Convertview = Inflat Er.inflate (r.layout.item_weather, NULL); TextView txtdate = (TextView) Convertview.findviewbyid (r.id.item_date); TextView Txtweather = (TextView) Convertview.findviewbyid (R.id.item_weather); TextView Txtwind = (TextView) Convertview.findviewbyid (R.id.item_wind); TextView txttemperature = (TextView) Convertview.findviewbyid (r.id.item_temperature); ImageView imgtemperature = (ImageView) Convertview.findviewbyid (r.id.item_picture); Weatherdatabean bean = (weatherdatabean) getItem (position); Txtdate.settext (Bean.getdate ()); Txtweather.settext (Bean.getweather ()); Txtwind.settext (Bean.getwind ()); Txttemperature.settext (Bean.gettemperature ()); Bitmaputils.display (Imgtemperature, Bean.getdaypictureurl ()); return convertview; }}
Finish the call.
The final effect is as follows:
Comrades, there is a sense of accomplishment!!!
Do not be complacent, this is only the data simple display, and can only be the Royal Park Beijing data, if you are not in Beijing, this is not useful to you at all.
Ah, well, today is here, already enough, studious you must have been looking for more about adapter, the ListView knowledge, here I do not introduce, suggest that we often Baidu.
Relax and talk nonsense.
I have been engaged in the IT industry for more than more than 10 years, beginning with the novice programmer, step by step, see a variety of programmers, from a personal point of view the programmer into such a few levels:
1. No brain
This refers to a person who is confronted with a problem and asks a comrade, he does not think, also aren't eager, mixed day count day. This type is very rare, I have just heard, are the legendary characters.
2. Rookie
This type of programmer is very numerous, usually a friend who has not been in contact with the technology for a long time. This part is usually no network can not work, often in the forum to find information, download others code to use, the keyboard ctrl, C, v keys are polished.
3. Calf
This type is more in the technical leader, the technical director of such positions, to achieve the calf this step, need to have a deep understanding of technology, can be designed from scratch. In the open source community, this part of the people are usually more active, the novice most use the calf's labor results.
4. Daniel
This type of people have been very few, I have only seen so few, are the major companies, such as the CTO. One of them, he has designed the company's cloud computing solutions from scratch, from the hardware infrastructure to the software level, to the user level, the Daniel has a deep grasp.
5. Legends
I can only see it in a variety of news and books. Personally think that this part of the people's technology is not what we can judge, their thoughts, influence is not what I can reach at all.
What level are you, dear?
The attachment is the project document, click Download.
This series of articles is my original, if need to reprint, please specify the source www.liuzhibang.cn
10 days to learn Android-fourth day