SolinariWu
4/29/2017 - 5:09 PM

GetTaipeiMetroInfo

GetTaipeiMetroInfo

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="8dp"
    card_view:cardBackgroundColor="@color/colorAccent"
    card_view:cardCornerRadius="4dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/station"
                android:textColor="@color/white"/>

            <TextView xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/tvStation"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:textColor="@color/white" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/destination"
                android:textColor="@color/white"/>

            <TextView xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/tvDestination"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:textColor="@color/white" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/update_time"
                android:textColor="@color/white"/>

            <TextView xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/tvTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:textColor="@color/white" />
        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
   android:gravity="center">

    <ProgressBar
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_gravity="center_vertical" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:visibility="gone"/>
</LinearLayout>
package com.example.solinari.GetTaipeiMetroInfo;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * Created by Solinari on 2017/1/21.
 */

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
    private String[] MetroInfo;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tvStation;
        public TextView tvDestination;
        public TextView tvTime;
        public ViewHolder(View v) {
            super(v);
            tvStation =(TextView) v.findViewById(R.id.tvStation);
            tvDestination =(TextView) v.findViewById(R.id.tvDestination);
            tvTime =(TextView) v.findViewById(R.id.tvTime);
        }

    }
    public RecyclerViewAdapter(String[] info) {
        MetroInfo = info;
    }

    @Override
    public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {

        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recycler_metroinfo, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //MetroInfo[position] 士林站_大安站_20:59:27 ,用"_"來區分各個欄位
        String[] tmp  = MetroInfo[position].split("_");
        holder.tvStation.setText(tmp[0]);
        holder.tvDestination.setText(tmp[1]);
        holder.tvTime.setText(tmp[2]);
    }
    @Override
    public int getItemCount() {
        return MetroInfo.length;
    }
}
package com.example.solinari.GetTaipeiMetroInfo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Solinari on 2016/12/31.
 */

public class FragmentList_One extends Fragment{
    private RecyclerView recyclerView;
    private ProgressBar progressBar;
    private RecyclerView.Adapter recyclerAdapter;
    private String[] MetroInfo;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragmentlist_one, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
        getMetroInfo();//取得台北捷運列車到站資訊
        return view;
    }
    public void setRecyclerView(){
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerAdapter = new RecyclerViewAdapter(MetroInfo);//設置RecyclerView.Adapter
        recyclerView.setAdapter(recyclerAdapter);
        progressBar.setVisibility(View.GONE);//Loading圖隱藏
        recyclerView.setVisibility(View.VISIBLE);//資料呈現
    }
    public void getMetroInfo(){
        RequestQueue queue = Volley.newRequestQueue(getActivity());//創建一個RequestQueue
        //台北捷運到站資料網址,按終點站排序
        String url = "http://data.taipei/opendata/datalist/apiAccess?scope=resourceAquire&rid=55ec6d6e-dc5c-4268-a725-d04cc262172b&sort=Destination";
        //對URL發出Request取得JsonObject的Response
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            //Response的JSONObject中,我們要的資訊是results(JSONArray)
                            //{"result":{"offset":0,"limit":10000,"count":25,"sort":"Destination","results":[{"_id":"1","Station":"士林站","Destination":"大安站","UpdateTime":"2017-04-10T20:59:27.577"},...}]}}
                            JSONArray info = response.getJSONObject("result").getJSONArray("results");
                            MetroInfo = new String[info.length()];
                            for(int i = 0; i < info.length();i++){
                                JSONObject jsonObject = info.getJSONObject(i);
                                //{"_id":"1","Station":"士林站","Destination":"大安站","UpdateTime":"2017-04-10T20:59:27.577"}
                                String time = jsonObject.getString("UpdateTime").substring(11,19);
                                MetroInfo[i] = jsonObject.getString("Station") + "_" + jsonObject.getString("Destination") + "_" + time;
                            }
                            setRecyclerView();//設置RecyclerView
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //當發生錯誤時,提示使用者發生錯誤
                MetroInfo = new String[1];
                MetroInfo[0] = "Error_Error_Error";
                setRecyclerView();
                Toast.makeText(getActivity(),"Get data error !",Toast.LENGTH_LONG).show();
            }
        }
        );
        //將Request加入RequestQueue
        queue.add(jsonObjectRequest);
    }
}