package com.bawie.com.test1.fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
import com.bawie.com.test1.JsonListBean;
import com.bawie.com.test1.R;
import com.bawie.com.test1.adapter.MyAdapter;
import com.bawie.com.test1.http.Http;
import com.google.gson.Gson;
import java.util.ArrayList;
/**
-
@Auther: 樊腾亚
-
@Date: 2018/12/29 14:27:02
-
@Description:
*/
public class List extends Fragment {private ListView listView;
@Nullable
@Overridepublic View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.listview,container,false); listView = view.findViewById(R.id.listview); new Thread(){ @Override public void run() { super.run(); String s = Http.get("https://api.apiopen.top/musicRankings"); Gson gson = new Gson(); JsonListBean listBean = gson.fromJson(s, JsonListBean.class); java.util.List<JsonListBean.ResultBean> list = listBean.getResult(); Message message = new Message(); message.what = 0; message.obj = list; handler.sendMessage(message); } }.start(); return view; }
Handler handler = new Handler(){
public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case 0: java.util.List<JsonListBean.ResultBean> list = (java.util.List<JsonListBean.ResultBean>) msg.obj; listView.setAdapter(new MyAdapter((ArrayList<JsonListBean.ResultBean>) list,getActivity())); break; case 1: Toast.makeText(getActivity(),"没网络",Toast.LENGTH_LONG).show(); break; } }
};
}