你的位置:首页 > 软件开发 > 操作系统 > [android] 天气app布局练习(四)

[android] 天气app布局练习(四)

发布时间:2016-07-17 18:00:05
主要练习一下获取网络数据和解析MainActivity.javapackage com.example.weatherreport;import java.io.UnsupportedEncodingException;import java.net.URLEncoder;imp ...

主要练习一下获取网络数据和解析

MainActivity.java

package com.example.weatherreport;import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.widget.GridView;import android.widget.SimpleAdapter;import android.widget.TextView;import com.example.weatherreport.contants.ApiContants;import com.example.weatherreport.domain.Weather;import com.example.weatherreport.net.HttpListener;import com.example.weatherreport.net.HttpUtil;public class MainActivity extends Activity {  private GridView gv_airs;  private TextView tv_city;  private TextView tv_number;  private TextView tv_rain;  private TextView tv_cloth;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    gv_airs = (GridView) findViewById(R.id.gv_airs);    tv_city=(TextView) findViewById(R.id.tv_city);    tv_number=(TextView) findViewById(R.id.tv_number);    tv_rain=(TextView) findViewById(R.id.tv_rain);    tv_cloth=(TextView) findViewById(R.id.tv_cloth);    setViewData();    makeGridView();  }  /**   * 设置界面数据   */  private void setViewData() {    String city = null;    try {      city = URLEncoder.encode("北京", "gb2312");    } catch (UnsupportedEncodingException e) {      e.printStackTrace();    }    HttpUtil.get(ApiContants.WEATHER_URL+"?city="+city+"&password=DJOYnieT8234jlsK&day=0", new HttpListener() {      @Override      public void onSuccess(String result) {        Weather weather=(Weather) HttpUtil.public void onError(String result) {        System.out.println(result);      }    });  }  protected void setViewWeather(Weather weather) {    tv_city.setText(weather.getCity());    tv_number.setText(weather.getHot());    tv_rain.setText(weather.getRain());    tv_cloth.setText(weather.getCloth());    System.out.println(weather.toString());  }  /**   * 组装GridView   */  private void makeGridView() {    List<Map<String, String>> data = new ArrayList<>();    Map<String, String> item = null;    item = new HashMap<>();    item.put("title", "83");    item.put("desc", "湿度(%)");    data.add(item);    item = new HashMap<>();    item.put("title", "11.3");    item.put("desc", "可见度(km)");    data.add(item);    item = new HashMap<>();    item.put("title", "2级");    item.put("desc", "东南风");    data.add(item);    item = new HashMap<>();    item.put("title", "最弱");    item.put("desc", "紫外线");    data.add(item);    item = new HashMap<>();    item.put("title", "1005.5");    item.put("desc", "气压(mb)");    data.add(item);    item = new HashMap<>();    item.put("title", "22.4");    item.put("desc", "体感");    data.add(item);    SimpleAdapter adapter = new SimpleAdapter(this, data,        R.layout.main_grid_item, new String[] { "title", "desc" },        new int[] { R.id.tv_title, R.id.tv_desc });    gv_airs.setAdapter(adapter);  }}

原标题:[android] 天气app布局练习(四)

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录