无法使用Java获取列表项正确的代码

时间:2015-10-14 17:16:15

标签: java list android-listview

我正在尝试用Java创建一个列表视图,允许每个列表项打开一个web url地址,但我似乎无法正确获取代码。请有人告诉我IO出错的地方。

package com.sasquatchapps.hydraquip10.bestofmonsterquest;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;

import android.os.Bundle;

import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class Season1Activity extends Activity{

    private String episodes[] = {"America's Loch Ness Monster","Sasquatch Attack",
            "Giant Squid Found","Birdzilla","Bigfoot","“Mutant K9","Lions in the Backyard","Gigantic Killer Fish","Swamp Beast","Russia's Killer Apemen","Unidentified Flying Creatures","The Real Hobbit",
            "Giganto: The Real King Kong","American Werewolf"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        ArrayAdapter<String> adapter = new
                ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1, episodes);
        setListAdapter(adapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Toast.makeText(this, "Item clicked;" + episodes[position], Toast.LENGTH_SHORT).show();
        if (position == 0) {
            Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/watch?v=o7-RdxrCFAg"));
            startActivity(intent);
        }
    }

 }

1 个答案:

答案 0 :(得分:0)

您有一些错误 - 您的listview在哪里?修改你的代码

1)创建listview -

ListView myList;

2)在你的onCreate中分配这个列表:

myList = (ListView) findViewById(R.id.my_list);

3)将适配器设置为列表:

 myList.setAdapter(adapter);