1. 程式人生 > >練手小項目(2)-生活小助手--星座運勢查詢

練手小項目(2)-生活小助手--星座運勢查詢

avi 聚合數據 當前 value book tell 身邊 req pin

上一篇內容

練手小項目(2)-生活小助手

今天星期一。趁著中午的歇息時間把 第二個寫出來 星座運勢,近期看看極客學院 用聚合數據做了天氣預報的視頻教程,不好評價他。看他在後面的代碼變更那麽大,我就知道,後面肯定做不下去,於是。就改代碼了。代碼變更那麽大,有幾個人會去理解,還不如我自己寫................

先看布局 技術分享

點擊去就是一個spinner 用幾個textview顯示查詢內容 布局有點醜,主要是給別人做功能,UI我就不考慮

技術分享

關於UI 我還是要貼下代碼。假設你有想法就把他美化下唄

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Spinner
        android:id="@+id/constellation_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_marginTop="20dp"
        android:layout_below="@id/constellation_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
 		<TextView
          android:id="@+id/datetime"
          style="@style/TextStyle"
          android:text="時間" />
 		<TextView
          android:id="@+id/name"
          style="@style/TextStyle"
          android:text="星座" />
        <TextView
          android:id="@+id/OFriend"
          style="@style/TextStyle"
          android:text="最佳配偶星座" />
        <TextView
          android:id="@+id/all"
          style="@style/TextStyle"
          android:text="幸運度" />
        <TextView
          android:id="@+id/color"
          style="@style/TextStyle"
          android:text="幸運顏色" />
       
        <TextView
          android:id="@+id/health"
          style="@style/TextStyle"
          android:text="健康運勢" />
        <TextView
          android:id="@+id/love"
          style="@style/TextStyle"
          android:text="愛情運勢" />
        <TextView
          android:id="@+id/money"
          style="@style/TextStyle"
          android:text="金錢運勢" />
        <TextView
          android:id="@+id/work"
          style="@style/TextStyle"
          android:text="工作運勢" />
        
        <TextView
          android:id="@+id/number"
          style="@style/TextStyle"
          android:text="幸運數字" />
        <TextView
          android:id="@+id/summary"
          style="@style/TextStyle"
          android:text="建議:" />

    </LinearLayout>

</RelativeLayout>

然後看 java代碼吧


1.實例化全部控件

	private void initview() {
		spinner = (Spinner) findViewById(R.id.constellation_name);
		datetime=(TextView) findViewById(R.id.datetime);
		name=(TextView) findViewById(R.id.name);
		OFriend=(TextView) findViewById(R.id.OFriend);
		all=(TextView) findViewById(R.id.all);
		color=(TextView) findViewById(R.id.color);
		health=(TextView) findViewById(R.id.health);
		love=(TextView) findViewById(R.id.love);
		money=(TextView) findViewById(R.id.money);
		work=(TextView) findViewById(R.id.work);
		number=(TextView) findViewById(R.id.number);
		summary=(TextView) findViewById(R.id.summary);
	}

2.建立數據元 給spinner 進行填充,


// 建立數據源
String[] mItems = getResources().getStringArray(R.array.book);

數據源在I res/values/arrays 就是12星座

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="book">
        <item >當前沒有選擇星座</item>
        <item >白羊座</item>
        <item >金牛座</item>
        <item >雙子座</item>
        <item >巨蟹座</item>
        <item >獅子座</item>
        <item >處女座</item>
        <item >天枰座</item>
        <item >天蠍座</item>
        <item >射手座</item>
        <item >魔蠍座</item>
        <item >水瓶座</item>
        <item >雙魚座</item>
    </string-array>
    
</resources>

3.數據源有了,就要定義 適配器了 spainner 的使用方法就是一個mvc 記住就可以

由於數據不多 所以就用arrayadapter

// 建立Adapter而且綁定數據源  
        ArrayAdapter<String> _Adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, mItems);
        //綁定 Adapter到控件  
        spinner.setAdapter(_Adapter);  

4.對於spinnner點擊事件處理 依據點擊的條目 進行聯網操作。json解析 然後展示 看所有代碼吧 我在代碼中解釋


package com.example.helper;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.helper.utils.HttpUtils;
import com.example.helper.utils.HttpUtils.OnNetWorkResponse;
import com.example.helper.utils.UrlApi;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ConstellationSearch extends Activity {
	private Spinner spinner;
	private TextView datetime,name,OFriend,all,color,health,love,money,work,number,summary;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.constellation_search);
		initview();
		// 建立數據源  
		String[] mItems = getResources().getStringArray(R.array.book);  
		// 建立Adapter而且綁定數據源  
		ArrayAdapter<String> _Adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, mItems); 
		//綁定 Adapter到控件  
		spinner.setAdapter(_Adapter);  
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			private String encode;
			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
			}
			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				 String str=parent.getItemAtPosition(position).toString();  
				    Toast.makeText(ConstellationSearch.this, "你點擊的是:"+str, 2000).show();  
				    try {
				    	//此數據要求url編碼所以我們轉一下
						encode = URLEncoder.encode(str,"utf-8");
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
				    	//拼接字符串 成url進行聯網
				    String path=UrlApi.TWO+encode+UrlApi.TWO_ITEM;
				    HttpUtils.RequestNetWork(path, new OnNetWorkResponse() {
						
						@Override
						public void ok(String response) {
							try {
								JSONObject o1=new JSONObject(response);
								int error_code = o1.getInt("error_code");
								System.out.println(error_code);
								if (error_code==0) {
									/*json格式 
									 * {
										    "error_code": 0,
										    "reason": "Success",
										    "result": {
										        "OFriend": "獅子座",
										        "all": "61%",
										        "color": "橙",
										        "date": "1416199893",
										        "datetime": "2014-11-17",
										        "health": "82%",
										        "love": "59%",
										        "money": "67%",
										        "name": "雙子座",
										        "number": "2",
										        "summary": "今天。工作上,你的任務要是想要提前完畢的話。得須要身邊的同事多多配合才行;感情上。不要吝嗇於表達自己的感情。要讓對方體驗到你的真心才行。健康上,狀態不錯;財運上,下降。

", "work": "55%" } } * * */ //json解析很easy 我簡單 講一下 第一個是大括號 SO jsonobject 然後看下我們要的數據在哪,result // 後面也是括號 getStringObject JSONObject o2 = o1.getJSONObject("result"); datetime.setText("當前時間: "+o2.getString("datetime")); name.setText("你選擇星座是: "+o2.getString("name")); OFriend.setText("最佳配偶星座: "+o2.getString("OFriend")); all.setText("幸運度: "+o2.getString("all")); color.setText("幸運色"+o2.getString("color")); health.setText("健康指數: "+o2.getString("health")); love.setText("愛情指數: "+o2.getString("love")); money.setText("金錢指數: "+o2.getString("money")); work.setText("升職指數: "+o2.getString("work")); number.setText("幸運數字: "+o2.getString("number")); summary.setText("建議: "+o2.getString("summary")); } } catch (JSONException e) { e.printStackTrace(); } } @Override public void error(String error) { } }); } }); } private void initview() { spinner = (Spinner) findViewById(R.id.constellation_name); datetime=(TextView) findViewById(R.id.datetime); name=(TextView) findViewById(R.id.name); OFriend=(TextView) findViewById(R.id.OFriend); all=(TextView) findViewById(R.id.all); color=(TextView) findViewById(R.id.color); health=(TextView) findViewById(R.id.health); love=(TextView) findViewById(R.id.love); money=(TextView) findViewById(R.id.money); work=(TextView) findViewById(R.id.work); number=(TextView) findViewById(R.id.number); summary=(TextView) findViewById(R.id.summary); } }

點擊下載源代碼


練手小項目(2)-生活小助手--星座運勢查詢