0

实际上我是从 json 获取 url,现在在我的列表视图中,它显示了 json 解析中存在的所有 youtube url 列表。单击 url 后,它将转到 youtube 页面并且正在播放该视频,我不想去其他网站从我的应用程序中,视频必须显示在我的应用程序中,因为我将如何使用嵌入到我的应用程序中的 youtube。

如何在我的列表视图中显示 youtube 视频列表,现在它显示 url,我希望它必须显示列表视图的小视频,如果我们单击视频,它将使用嵌入在我的应用程序中播放 youtube 视频

我的活动.java

public class PoojaVideos extends Activity implements FetchDataListener1  {

    private ProgressDialog dialog;

    ListView lv2;
    private List<Application1> items;


    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_item1);  

        lv2 =(ListView)findViewById(R.id.listV_main);   
        lv2.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
                Object o = lv2.getItemAtPosition(position);
                Application1 obj_itemDetails = (Application1)o;
                final Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(((Application1) o).getUrlWiki()));
                  startActivity(i);


            } 
        });

        //praycount.setOnClickListener(this);
        initView();
    }

    private void initView(){
        // show progress dialog
        dialog = ProgressDialog.show(this, "", "Loading...");
        String url = "http://www.ginfy.com/api/v1/videos.json";
        FetchDataTask1 task = new FetchDataTask1(this);
        task.execute(url);
    }

    @Override
    public void onFetchComplete(List<Application1> data) {
        this.items = data;
        // dismiss the progress dialog
        if ( dialog != null )
            dialog.dismiss();
        // create new adapter
        ApplicationAdapter1 adapter = new ApplicationAdapter1(this, data);
        // set the adapter to list
        lv2.setAdapter(adapter);

    }

    @Override
    public void onFetchFailure(String msg) {
        if ( dialog != null )
            dialog.dismiss();
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show();

    } This is the activity for showing listview and after clicking the item in list it is going to youtube page.

我的布局.xml

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

     <ListView 
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:id="@+id/listV_main"/>
</LinearLayout>

我不想进入 youtube 网站,视频必须显示在我的应用程序本身中,因为如何使用嵌入

应用程序适配器.java

public class ApplicationAdapter1 extends ArrayAdapter<Application1> {

    private List<Application1> items;
    private LayoutInflater inflator;
    private PoojaVideos activity;
    private ProgressDialog dialog;

    public ApplicationAdapter1(PoojaVideos context, List<Application1> items){
        super(context, R.layout.activity_row1, items);
        this.items = items;
        inflator = LayoutInflater.from(getContext());
        activity=context;
    }

    @Override
    public int getCount(){
        return items.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        ViewHolder holder = null;
        //View v = convertView;
        if ( convertView == null ){ 
            convertView = inflator.inflate(R.layout.activity_row1, null);
            holder = new ViewHolder();
            holder.prayersLinkWiki = (TextView) convertView.findViewById(R.id.prayersLinkWiki);


            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }





        Application1 app1 = items.get(position);

        holder.prayersLinkWiki.setText(Html.fromHtml(app1.getUrlWiki()));



        return convertView;
    }
    class ViewHolder {

        public TextView prayersLinkWiki;

    }
    //return convertView;



}

Fetchdatatask1.java

public class FetchDataTask1 extends AsyncTask<String, Void, String>
{
    private final FetchDataListener1 listener;
    private  OnClickListener onClickListener;
    private String msg;

    public FetchDataTask1(FetchDataListener1 listener)
    {
        this.listener = listener;
    }



    @Override
    protected String doInBackground(String... params)
    {
        if ( params == null )
            return null;
        // get url from params
        String url = params[0];
        try
        {
            // create http connection
            HttpClient client = new DefaultHttpClient();
            HttpGet httpget = new HttpGet(url);
            // connect
            HttpResponse response = client.execute(httpget);
            // get response
            HttpEntity entity = response.getEntity();
            if ( entity == null )
            {
                msg = "No response from server";
                return null;
            }
            // get response content and convert it to json string
            InputStream is = entity.getContent();
            return streamToString(is);
        }
        catch ( IOException e )
        {
            msg = "No Network Connection";

        }
        return null;
    }

    @Override
    protected void onPostExecute(String sJson)
    {
        if ( sJson == null )
        {
            if ( listener != null )
                listener.onFetchFailure(msg);
            return;
        }
        try
        {
            // convert json string to json object
            JSONObject jsonObject = new JSONObject(sJson);
            JSONArray aJson = jsonObject.getJSONArray("youtube_url");
            // create apps list
            List<Application1> apps = new ArrayList<Application1>();
            for ( int i = 0; i < aJson.length(); i++ )
            {
                JSONObject json = aJson.getJSONObject(i);
                Application1 app1 = new Application1();
                 app1.setUrlWiki("https://www.youtube.com/watch?v="+json.getString("youtube_url"));



                // add the app to apps list
                apps.add(app1);



            }
            //notify the activity that fetch data has been complete
            if ( listener != null )
                listener.onFetchComplete(apps);
        }
        catch ( JSONException e )
        {
            e.printStackTrace();
            msg = "Invalid response";
            if ( listener != null )
                listener.onFetchFailure(msg);
            return;
        }
    }

    /**
     * This function will convert response stream into json string
     * 
     * @param is
     *            respons string
     * @return json string
     * @throws IOException
     */
    public String streamToString(final InputStream is) throws IOException
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try
        {
            while ( (line = reader.readLine()) != null )
            {
                sb.append(line + "\n");
            }
        }
        catch ( IOException e )
        {
            throw e;
        }
        finally
        {
            try
            {
                is.close();
            }
            catch ( IOException e )
            {
                throw e;
            }
        }
        return sb.toString();
    }
}

这是用于显示布局的行 url

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

     <TextView
            android:id="@+id/prayersLinkWiki"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:autoLink="web"
            android:textSize="15sp" />

</RelativeLayout>

在我的列表视图中,它会显示 url,单击 url 后它会转到 youtube 网站观看视频,对于持有者 url 中的列表视图,我们可以使用嵌入此行中的 youtube 视频列表holder.prayersLinkWiki.setText(Html.fromHtml(app1.getUrlWiki()));

4

1 回答 1

0

删除代码final Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(((Application1) o).getUrlWiki())); startActivity(i);添加代码以打开带有 webview 的对话框。在 webview 中加载 youtube URL

于 2013-06-27T10:40:38.617 回答