本文介绍了为了实现这些ListActivity显示在视频网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现这些ListActivity.java

I need to implement these ListActivity.java

ListActivity.java:

package com.sit.fth.activity;

public class ListActivity extends Activity {
    private ListView lv;
    private String[] groupArray = {"Category1", "Category2", "Category3"};
    private String[][] childArray = {{"Test Greater glory Part-3","Greater glory Part-1"},
        {"What does","SundayService ( 19_01_14 )"}, {"Greater glory Part-3", "SundayService ( 19_01_14 )Tamil"}};

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.list1);
        lv = (ListView) findViewById(R.id.listview);

        String[] data = getIntent().getStringArrayExtra("strArray");
        AdapterView.OnItemClickListener clickListener = null;

        // If no data received means this is the first activity
        if (data == null) {
            data = groupArray;
            clickListener = new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Intent intent = new Intent(ListActivity.this, ListActivity.class);
                    intent.putExtra("strArray", childArray[position]);
                    startActivity(intent);
                }
            };
        }

        ArrayAdapter adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, data);

        lv.setAdapter(adapter);
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(clickListener);

     }

}  

list1.java:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

如果我点击组别中显示,ListView.Another的ListView。

If I click the Category1 in that ListView.Another ListView shown.

我的问题是,如果我点击 TestGreater荣耀部分-3 ,视频必须从URL.I显示不知道如何实施 ListActivity Class,以下面的编码。

My problem is,If I click the TestGreater Glory Part-3, the video have to display from URL.I doesn't know how to implement that ListActivity Class to the below codings.

的strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string name="str_video">VIDEO</string>
<string name="api_host">URL Here</string>

</resources> 

YouTubePlayActivity.java:

package com.sit.fth.activity;

public class YoutubePlayActivity extends YouTubeFailureRecoveryActivity {
    public static final String DEVELOPER_KEY = "DEVELOPER_KEY_HERE";
    private String videoId;
    private YouTubePlayerView youTubeView;
    private ActionBar actionabar;
    private int position;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);

        setContentView(R.layout.youtube_play);

        position = getIntent().getExtras().getInt("position");

        Bundle bundle = getIntent().getExtras();
        videoId = bundle.getString("videoid");

        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.youtube_view);
        youTubeView = new YouTubePlayerView(this);
        frameLayout.addView(youTubeView);
        youTubeView.initialize(DEVELOPER_KEY, this);

        ((TextView) findViewById(R.id.header_title)).setText(bundle
                .getString("title"));

        actionabar = getActionBar();
        //actionabar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionabar.setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider,
            YouTubePlayer player, boolean wasRestored) {
        if (!wasRestored) {
            try {
                if (videoId != null) {
                    // 2GPfZYwYZoQ
                    player.cueVideo(videoId);
                } else {
                    Toast.makeText(getApplicationContext(),
                            "Not a Valid Youtube Video", Toast.LENGTH_SHORT)
                            .show();
                }
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(),
                        "Not a Valid Youtube Video", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        return youTubeView;
    }
}

我需要实现 ListActivity.java 类来获得在url中显示的视频。

I need to implement that ListActivity.java Class to get the videos shown in the url.

推荐答案

下面是列出类别的应用程序(的您提供的的)。点击一些类别后,它的视频将被从 JSON 文件加载和显示另一个的ListView

Here's an app that lists categories (that you provide). After clicking some category, it's videos will be loaded from the JSON file and displayed as another ListView.

当你点击一个视频,它的 URL和标题发送到 YoutubeActivity 。 YoutubeActivity然后开始与视频的播放器。

When you click on a video, it's url and title is sent to the YoutubeActivity. YoutubeActivity then starts a player with that video.

我相信你想更复杂的东西,但你可以添加其他功能(如从JSON获得额外的字段)很容易。如果你想也进口类别,那么你需要的另一个JSON结构的如类阵列 - > 视频阵列

I believe you wanted something more complex but you can add the other functionality (such as getting additional fields from the json) quite easily. If you would want to import categories also, then you'd need another json structure such as array of categories->array of videos.

ListActivity:

package com.example.youtubecatplayer;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by Simon on 2014 Jul 08.
 */
public class ListActivity extends Activity {
    // Categories must be pre-set
    private String[] data = {"Category1", "Category2", "Category3"};
    private final String JSONUrl = "http://tfhapp.fathershouse.in/api/video.php";
    private final String TAG_VIDEOS = "videos";
    private final String TAG_CAT = "video_category";
    private final String TAG_URL = "video_url";
    private final String TAG_TITLE = "video_title";

    private List<String> videoTitles = new ArrayList<String>();
    private List<String> videoURLs = new ArrayList<String>();
    private ArrayAdapter adapter;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.list1);
        ListView listView = (ListView) findViewById(R.id.listview);

        Bundle extras = getIntent().getExtras();
        AdapterView.OnItemClickListener clickListener = null;

        // Category view:
        if (extras == null) {
            clickListener = new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Intent intent = new Intent(ListActivity.this, ListActivity.class);
                    intent.putExtra("categoryName", data[position]);
                    startActivity(intent);
                }
            };
            adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, data);
        }
        else { // Child view
            // Get the category of this child
            String categoryName = extras.getString("categoryName");
            if (categoryName == null)
                finish();

            // Populate list with videos of "categoryName", by looping JSON data
            new JSONParse(categoryName).execute();

            clickListener = new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Intent intent = new Intent(ListActivity.this, YoutubeActivity.class);
                    // Send video url and title to YoutubeActivity
                    intent.putExtra("videoUrl", videoURLs.get(position));
                    intent.putExtra("videoTitle", videoTitles.get(position));
                    startActivity(intent);
                }
            };
            adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, videoTitles);
        }

        listView.setAdapter(adapter);
        listView.setTextFilterEnabled(true);
        listView.setOnItemClickListener(clickListener);
    }

    private class JSONParse extends AsyncTask<String, String, JSONObject> {
        private ProgressDialog pDialog;
        private String categoryName;

        // Constructor // Get the categoryName of which videos will be found
        public JSONParse(String category) {
            this.categoryName = category;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Create a loading dialog when getting the videos
            pDialog = new ProgressDialog(ListActivity.this);
            pDialog.setMessage("Getting Videos...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }
        @Override
        protected JSONObject doInBackground(String... args) {
            JSONParser jParser = new JSONParser();
            // Get JSON from URL
            JSONObject json = jParser.getJSONFromUrl(JSONUrl);
            if (json == null)
                return null;

            try {
                // Get video array
                JSONArray videos = json.getJSONArray(TAG_VIDEOS);

                // Loop all videos
                for (int i=0; i<videos.length(); i++) {
                    JSONObject video = videos.getJSONObject(i);
                    Log.e("JSON:", "cat: "+video.getString(TAG_CAT)+",title: "+video.getString(TAG_TITLE)+", url: "+video.getString(TAG_URL));
                    // Check if video belongs to "categoryName"
                    if (video.getString(TAG_CAT).equals(categoryName)) {
                        addVideo(video);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return json;
        }

        private void addVideo(JSONObject video) {
            try {
                // Add title and URL to their respective arrays
                videoTitles.add(video.getString(TAG_TITLE));
                videoURLs.add(video.getString(TAG_URL));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        protected void onPostExecute(JSONObject json) {
            // Close the "loading" dialog
            pDialog.dismiss();
            if (json == null) {
                // Do something when there's no internet connection
                // Or there are no videos to be displayed
            }
            else // Let the adapter notify ListView that it has new items
                adapter.notifyDataSetChanged();
        }
    }
}
// Example JSON video item
// {"videoid":"59","video_type":"Youtube","language":"english","date":"08 Jul 2014",
// "video_title":"What does","video_category":"Category2","video_url":"P84FGn49b4A"},

YoutubeActivity:

package com.example.youtubecatplayer;

import android.app.ActionBar;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;
/**
 * Created by Simon on 2014 Jul 08.
 */
public class YoutubeActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{
    public static final String DEVELOPER_KEY = "DEV_KEY";
    private String videoId;
    private ActionBar actionabar;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        setContentView(R.layout.youtube_activity);

        Bundle bundle = getIntent().getExtras();
        videoId = bundle.getString("videoUrl");
        this.setTitle(bundle.getString("videoTitle"));
        //((TextView) findViewById(R.id.videoTitle)).setText(bundle.getString("videoTitle"));

        YouTubePlayerView playerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);
        playerView.initialize(DEVELOPER_KEY, this);

        actionabar = getActionBar();
        //actionabar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionabar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
                                        boolean wasRestored) {
        if (!wasRestored) {
            try {
                if (videoId != null) {
                    // 2GPfZYwYZoQ
                    player.cueVideo(videoId);
                } else {
                    Toast.makeText(getApplicationContext(),
                            "Not a Valid Youtube Video", Toast.LENGTH_SHORT)
                            .show();
                }
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(),
                        "Not a Valid Youtube Video", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider,
                                        YouTubeInitializationResult youTubeInitializationResult) {
    }
}

JSONParser:

package com.example.youtubecatplayer;
/**
 * Created by Simon on 2014 Jul 08.
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
    public JSONParser() {
    }

    public JSONObject getJSONFromUrl(String url) {
        InputStream input = null;
        String jsonStr = null;
        JSONObject jsonObj = null;
        try {
            // Make the request
            input = new URL(url).openStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(input,
                    Charset.forName("UTF-8")));
            StringBuilder strBuilder = new StringBuilder();
            int ch;
            while ((ch = reader.read()) != -1)
                strBuilder.append((char) ch);

            input.close();
            jsonStr = strBuilder.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        // try parse the string to a JSON object
        try {
            jsonObj = new JSONObject(jsonStr);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        // return JSON String
        return jsonObj;
    }
}

请注意:你需要上网的权限在AndroidManifest.xml中:

NOTE: you need internet permission in your AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />

和你需要实现YoutubeAPI到项目中。

And you need to implement the YoutubeAPI into your project.


的build.gradle 编译文件(库/ YouTubeAndroidPlayerApi.jar')

build.gradle compile files('libs/YouTubeAndroidPlayerApi.jar')

编辑:

我只是试图重新建立这个项目,它完美地工作。这里是我的布局和清单文件,也许他们给你造成麻烦。

I just tried to re-create this project and it worked perfectly. Here's my layout and manifest files, maybe they're causing you trouble.

Android清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.simas.myapplication" >
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".ListActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".YoutubeActivity"
            android:label="@string/app_name" >
        </activity>

    </application>

</manifest>

youtube_activity.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <com.google.android.youtube.player.YouTubePlayerView
        android:id="@+id/youtubeplayerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

list1.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".ListActivity">

    <ListView
        android:id="@+id/listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

不要忘记改变开发者密钥,否则你会看到一个球员初始化的问题。

这篇关于为了实现这些ListActivity显示在视频网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 13:46