转换GDATA饲料XML - 的Youtube播放列表到Android列表视图 [英] Convert gdata feed to xml - Youtube Playlist to ANdroid Listview

查看:128
本文介绍了转换GDATA饲料XML - 的Youtube播放列表到Android列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面这$ C $ Ç创建一个自定义列表视图。

I am following this code to create a custom list view.

它使用一个XML文件来分析对象,并显示在列表视图

It uses an XML file to parse objects and show them in Listview

我想知道我可以用同样的例子来说明视频标题和一个YouTube播放列表的时间。

I was wondering can I use the same example to show Videos Titles and Duration of a Youtube Playlist.

我已经不过的Youtube API和GDATA,但我不能找出如何让原始XML的链接,我可以用上面的例子中code

I have gone though Youtube API and GDATA but I can't find out how to get raw xml link which I can use with above example code

任何帮助球员?

推荐答案

下面有我在我的previous项目之一的一类。这使得第一视频作为主之一,显示时间和标题,然后添加的影片所有其余的布局。

Heres a class I've used in one of my previous projects. This puts the first video as the "main" one, displays the time and title, and then adds all the rest of the videos to a layout.

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();   
            }
        }
    }
}

video.xml

video.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>

videos.xml

videos.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>

这篇关于转换GDATA饲料XML - 的Youtube播放列表到Android列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆