1

我正在按照此代码创建自定义列表视图。

它使用XML 文件来解析对象并将它们显示在Listview

我想知道我可以使用相同的示例来显示 Youtube 播放列表的视频标题和持续时间。

我已经浏览了 Youtube API 和 GDATA,但我不知道如何获取可以与上述示例代码一起使用的原始 xml 链接

有帮助吗?

4

1 回答 1

2

这是我在以前的一个项目中使用过的一个类。这会将第一个视频作为“主要”视频,显示时间和标题,然后将所有其余视频添加到布局中。

public class Videos extends Activity {
    ImageView mainThumb;
    TextView mainTitle;
    TextView mainTime;
    LinearLayout videos;
    ArrayList<String> links;

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.videos);

        new ParseVideoDataTask().execute();
        mainThumb = (ImageView) findViewById(R.id.mainThumb);
        mainTitle = (TextView) findViewById(R.id.mainTitle);
        mainTime = (TextView) findViewById(R.id.mainTime);
        videos = (LinearLayout) findViewById(R.id.videos);

    }

    private class ParseVideoDataTask extends AsyncTask<String, String, String> {
        int count = 0;

        @Override
        protected String doInBackground(String... params) {
            URL jsonURL;
            URLConnection jc;
            links = new ArrayList<String>();
            try {
                jsonURL = new URL("http://gdata.youtube.com/feeds/api/playlists/" +
                    YOUR PLAYLIST ID +  
                    "?v=2&alt=jsonc");
                 jc = jsonURL.openConnection(); 
                 InputStream is = jc.getInputStream(); 
                 String jsonTxt = IOUtils.toString(is); 
                 JSONObject jj = new JSONObject(jsonTxt); 
                 JSONObject jdata = jj.getJSONObject("data"); 
                 JSONArray aitems = jdata.getJSONArray("items"); 
                 for (int i=0;i<aitems.length();i++) {
                     JSONObject item = aitems.getJSONObject(i); 
                     JSONObject video = item.getJSONObject("video"); 
                     String title = video.getString("title");
                     JSONObject player = video.getJSONObject("player");
                     String link = player.getString("default");
                     String length = video.getString("duration");
                     JSONObject thumbnail = video.getJSONObject("thumbnail"); 
                     String thumbnailUrl = thumbnail.getString("hqDefault")
                     String[] deets = new String[4];
                     deets[0] = title;
                     deets[1] = thumbnailUrl;
                     deets[2] = length;
                     links.add(link);
                     publishProgress(deets);
                 }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }       
            return null;
        }       

        @Override
        protected void onProgressUpdate(final String... deets) {
            count++;
            if (count == 1) {
                MainActivity.setImageFromUrl(deets[1], mainThumb, Videos.this);
                mainTitle.setText(deets[0]);
                mainTime.setText(formatLength(deets[2]));
                mainThumb.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(links.get(1)));
                        startActivity(i);
                    }
                });
            } else {
                LayoutInflater layoutInflater = (LayoutInflater)Videos.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View video = layoutInflater.inflate(R.layout.video, null);
                ImageView thumb = (ImageView) video.findViewById(R.id.thumb);
                TextView title = (TextView) video.findViewById(R.id.title);
                TextView time = (TextView) video.findViewById(R.id.time);
                MainActivity.setImageFromUrl(deets[1], thumb, Videos.this);
                title.setText(deets[0]);
                time.setText(formatLength(deets[2]));
                video.setPadding(20, 20, 20, 20);
                videos.addView(video);
                video.setId(count-1);
                video.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(links.get(v.getId())));
                        startActivity(i);
                    }
                });
            }
        }
    }

    private CharSequence formatLength(String secs) {
        int secsIn = Integer.parseInt(secs);
        int hours = secsIn / 3600,
                remainder = secsIn % 3600,
                minutes = remainder / 60,
                seconds = remainder % 60;

                return ((minutes < 10 ? "0" : "") + minutes
                + ":" + (seconds< 10 ? "0" : "") + seconds );
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        for (int i=0;i<videos.getChildCount();i++) {
            View v = videos.getChildAt(i);
            if (v instanceof ImageView) {
                ImageView iv = (ImageView) v;           
                ((BitmapDrawable)iv.getDrawable()).getBitmap().recycle();   
            }
        }
    }
}

视频.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

        <ImageView
            android:id="@+id/thumb"
            android:layout_width="240dp"
            android:layout_height="180dp"
            android:layout_centerHorizontal="true"
            android:scaleType="fitStart" />

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/thumb"
            android:layout_below="@+id/thumb"
            android:layout_toLeftOf="@+id/time"
            android:maxLines="1"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/thumb"
            android:layout_below="@+id/thumb"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/black" />              


</RelativeLayout>

视频.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >
    <RelativeLayout
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >    

        <ImageView
            android:id="@+id/mainThumb"
            android:layout_width="240dp"
            android:layout_height="180dp"
            android:layout_centerHorizontal="true"
            android:scaleType="fitXY"/>

        <TextView
            android:id="@+id/mainTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/mainThumb"
            android:layout_below="@+id/mainThumb"
            android:layout_toLeftOf="@+id/mainTime"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/mainTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/mainThumb"
            android:layout_below="@+id/mainThumb"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@android:color/black" />              
    </RelativeLayout>

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="none"
        android:layout_alignParentBottom="true" >

        <LinearLayout
            android:id="@+id/videos"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:gravity="center"
            android:orientation="horizontal" />

    </HorizontalScrollView>
</RelativeLayout>
于 2012-08-26T10:43:19.393 回答