本文内容

  • 环境
  • 项目结构
  • 演示自定义 ListView 显示网络上 JSON 歌曲列表
  • 参考资料

本文最开始看的是一个国人翻译的文章,没有源代码可下载,根据文中提供的代码片段,自己新建的项目(比较可恶的是,没有图标图片资源,只能自己乱搞),但程序不是很稳定,有时能显示出列表中的缩略图,有时显示不出来,还在主线程访问了网络。但在文章评论中,作者给出英文原文链接,本来想这下没事了吧,结果下载源代码运行后,还是有问题~仔细看英文原文,原来他也是根据 Github 上一个项目搞的,只是添加了式样,以及显示完整的歌曲列表,包括歌曲名、歌手名、缩略图、时长。

看来只能自己研究了,式样用英文原文的,改变主要有两个,一个是英文原文的网络歌曲列表是 .xml 格式文件,本文采用 .json 格式;二是调整了网络访问。

环境


  • Windows 2008 R2 64 位
  • Eclipse ADT V22.6.2,Android 4.4.3
  • SAMSUNG GT-I9008L,Android OS 2.2.2

项目结构


1

图 1 项目结构

演示自定义 ListView 显示网络上 JSON 歌曲列表


演示运行结果如下所示,本文只给出核心代码。点击 此处 下载,自己调试一下。

device-2014-07-07-160204

图 2 主程序

自定义式样

gradient_bg.xml、gradient_bg_hover.xml、list_selector.xml 以及 image_bg.xml,定义列表背景,选中和没选中的式样,以及缩略图的背景。

main.xml 主页面及其后台

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ListView
        android:id="@+id/mylist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />
    <Button
        android:id="@+id/btn_clear"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="清除缓存, 重新加载" />
</LinearLayout>

CustomizedListView.java Request.java 访问网络

package com.example.androidhive;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONArray;
import org.json.JSONException;
public class Request {
    public Request() {
    public JSONArray getJsonFromUrl(String urlStr) {
        try {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            // Sets the flag indicating whether this URLConnection allows input.
            // conn.setDoInput(true);
            conn.setConnectTimeout(3000);
            conn.setReadTimeout(3000);
            // Flag to define whether the protocol will automatically follow
            // redirects or not.
            conn.setInstanceFollowRedirects(true);
            int response_code = conn.getResponseCode();
            if (response_code == 200) {
                InputStream in = conn.getInputStream();
                InputStreamReader inputReader = new InputStreamReader(in);
                BufferedReader buffReader = new BufferedReader(inputReader);
                String line = "", JsonStr = "";
                while ((line = buffReader.readLine()) != null) {
                    JsonStr += line;
                JSONArray jsonArray = new JSONArray(JsonStr);
                return jsonArray;
            } else {
                conn.disconnect();
                return null;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        return null;
.csharpcode .lnum { color: rgba(96, 96, 96, 1) }StreamUtils.java 缓存时的I/O操作
package com.example.androidhive;
import java.io.InputStream;
import java.io.OutputStream;
public class StreamUtils {
    public static void CopyStream(InputStream is, OutputStream os)
        final int buffer_size=1024;
            byte[] bytes=new byte[buffer_size];
            for(;;)
              int count=is.read(bytes, 0, buffer_size);
              if(count==-1)
                  break;
              os.write(bytes, 0, count);
        catch(Exception ex){}
  • androidhive android-custom-listview-with-imgae-and-text
  • androidhive android android-custom-listview-with-image-and-text 译文
  • 这三个链接的关系是,第二个链接的演示是根据第一个链接完成的,第三个链接翻译的第二个链接。但遗憾的是,英文原文是有bug的,程序运行不稳定。

  • Android ListView 和 ***Adapter 从本地/网络获取歌曲列表
  • Android LazyList 从网络获取图片并缓存
  •