===============================listview展示======================================================== public class MainActivity extends AppCompatActivity { private ListView listView; private String dataUrl = "http://www.xieast.com/api/news/news.php"; public static final int CG = 1001; public static final int SB = 1000; private List<News.DataBean> a = new ArrayList<>(); private MyAdapter myAdapter; private final Handler handler=new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case CG: String data = (String) msg.obj; Gson gson = new Gson(); News bean = gson.fromJson(data, News.class); a = bean.getData(); show(a.size() + ""); myAdapter.notifyDataSetChanged(); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.view); myAdapter = new MyAdapter(); listView.setAdapter(myAdapter); getData(); } public void getData() { new Thread() { @Override public void run() { super.run(); try { URL url = new URL(dataUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(5000); connection.connect(); int i = connection.getResponseCode(); if (i == HttpURLConnection.HTTP_OK) { Message message = Message.obtain(); message.what = CG; InputStream is = connection.getInputStream(); String data = conver2sString(is); message.obj = data; handler.sendMessage(message); } else{ Message message = Message.obtain(); message.what = SB; handler.sendMessage(message); } } catch (Exception e) { e.printStackTrace(); } } }.start(); } private String conver2sString(InputStream is) throws IOException { ByteArrayOutputStream stream = new ByteArrayOutputStream(); int len = -1; byte[] buffer = new byte[512]; while ((len = is.read(buffer)) != -1) { stream.write(buffer, 0, len); } return new String(stream.toByteArray()); } private void show(String msg) { Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show(); } private class MyAdapter extends BaseAdapter { @Override public int getCount() { return a.size(); } @Override public Object getItem(int i) { return i; } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { View v; ViewHier viewHier; if (view == null) { viewHier = new ViewHier(); v = View.inflate(MainActivity.this, R.layout.lay, null); viewHier.img = v.findViewById(R.id.img); viewHier.tv = v.findViewById(R.id.tv); viewHier.tv1 = v.findViewById(R.id.tv1); v.setTag(viewHier); } else { v = view; viewHier = (ViewHier) v.getTag(); } News.DataBean bean = (News.DataBean) a.get(i); viewHier.tv1.setText(bean.getAuthor_name()); viewHier.tv1.setText(bean.getDate()); viewHier.tv1.setText(bean.getTitle()); Picasso.with(MainActivity.this).load("https:"+bean.getThumbnail_pic_s()).fit().into(viewHier.img); return v; } } class ViewHier { ImageView img; TextView tv; TextView tv1; } }
==============================listview展示布局=================================================
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="欢迎来主页面" /> <ListView android:id="@+id/view" android:layout_width="wrap_content" android:layout_height="wrap_content"></ListView> </LinearLayout> <LinearLayout android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff0" android:text="这里是侧滑" /> </LinearLayout> </android.support.v4.widget.DrawerLayout>