0

我正在使用本教程中的 CustomizedList View 代码http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

我通过添加 YouTube 视频 ID 对其进行了修改,但是当我点击列表项并且我想开始新的活动来播放视频时出现问题,我不知道如何将视频 ID 传递给下一个活动!

我的问题是如何在 CustomizedListView Activity 中获取 KEY_VID_ID 的值!!!

CuztomizedListView.java 代码

public class CustomizedListView extends Activity {
// All static variables
static final String URL = "http://api.androidhive.info/music/music.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
static final String KEY_VID_ID = "vid_id";


ListView list;
LazyAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML from URL
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_SONG);
    // looping through all song nodes <song>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
        map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
        map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
        map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
        map.put(KEY_VID_ID, parser.getValue(e, KEY_VID_ID));

        // adding HashList to ArrayList
        songsList.add(map);
    }


    list=(ListView)findViewById(R.id.list);

    // Getting adapter by passing xml data ArrayList
    adapter=new LazyAdapter(this, songsList);        
    list.setAdapter(adapter);


    // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Intent intent = new Intent(CustomizedListView.this, PlayVideo.class);

        //  intent.setData(Uri.parse());
            startActivity(intent);


        }
    });     
}   
}

懒惰适配器

public class LazyAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView title = (TextView)vi.findViewById(R.id.title); // title
    TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
    TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

    HashMap<String, String> song = new HashMap<String, String>();
    song = data.get(position);

    // Setting all values in listview
    title.setText(song.get(CustomizedListView.KEY_TITLE));
    artist.setText(song.get(CustomizedListView.KEY_ARTIST));
    duration.setText(song.get(CustomizedListView.KEY_DURATION));
    imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
    return vi;
}
}

播放视频.java

public class PlayVideo extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener {

static private final String DEVELOPER_KEY = "AIzaSyBOQlVNfn6TY_TpyWYKMbwNPk09AhBRWXw";
 static private final String VIDEO = "oZbeL1ciR4E";
   @Override
   protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.playvideo);
          YouTubePlayerView youTubeView = (YouTubePlayerView)
findViewById(R.id.youtube_view);
       youTubeView.initialize(DEVELOPER_KEY, this);
     Bundle extras = getIntent().getExtras();
       if (extras != null) {
      String VIDEOs = extras.getString("videourl");
      TextView text = (TextView) findViewById(R.id.youtube_text);
      text.setText(VIDEOs);
       }

      }
     @Override
     public void onInitializationFailure(Provider provider,
YouTubeInitializationResult error) {
            Toast.makeText(this, "Oh no! "+error.toString(),
Toast.LENGTH_LONG).show();
   }
   @Override
   public void onInitializationSuccess(Provider provider, YouTubePlayer player,
boolean wasRestored) {
     /*  Bundle extras = getIntent().getExtras();
       if (extras != null) {
      String VIDEOs = extras.getString("videourl");
      TextView text = (TextView) findViewById(R.id.youtube_text);
      text.setText(VIDEOs);*/

          player.loadVideo(VIDEO);
       }
   }
4

6 回答 6

1

你可以试试这个:

Intent intent = new Intent(CustomizedListView.this, PlayVideo.class);
intent.putExtra("ID",KEY_ID);
startActivity(intent);

并像这样在 PlayVideo.java 中获取它

String Key_id = getIntent().getExtras().getString("ID");

现在您有一个关于如何从列表中获取 ID 的查询。然后我建议给你在自定义适配器类中的视图一个标签,这是你的视频 ID . 这样您就可以将该视频标签传递给另一个活动。像这样:

vi.setTag(song.get(CustomizedListView.KEY_ID));

在 getView() 方法的惰性加载器类中。然后在列表的点击事件中获取标签,如下所示:

String tag = view.getTag().toString();

希望能帮助到你!!

于 2013-08-06T06:32:28.293 回答
0

将此代码放在您的 CustomizedListView 类中

Intent intent = new Intent(CustomizedListView.this, PlayVideo.class);
                        truckIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        truckIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        truckIntent.putExtra(KEY_VID_ID, ID);

                        startActivity(intent);

将以下代码放在您的 playvideo.class

String Key_id = getIntent().getExtras().getString("ID");
于 2013-08-06T06:37:21.833 回答
0

将以下代码添加到您的 onItemClick 方法中

Intent intent = new Intent(CustomizedListView.this, PlayVideo.class);        
intent.putExtra("videoId", id);
startActivity(intent );
于 2013-08-06T06:31:19.480 回答
0

从 XML 获取视频 ID 的一个好方法是将 tag 属性添加到您的项目 ( android:tag="v=ID") 中。因此,将 ItemClickListener 修改为:

 // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            Intent intent = new Intent(CustomizedListView.this, PlayVideo.class);
        //Verify if we have the ID and then send to another activity.
        if(view.getTag() != null)
                 intent.putExtra("id", "" + view.getTag());
        //  intent.setData(Uri.parse());
            startActivity(intent);


        }
    });  

然后,在 PlayVideo.class 中,在 onCreate 方法中,您可以让 Id 执行以下操作:

String id = null;
Bundle extras = intent.getExtras()
if(extras != null && extras.containsKey("id"))
    id = extras.getString("id");

现在字符串 id 将包含您想要的 id。

于 2013-08-06T06:35:53.837 回答
0

在第一个活动中

Intent i = new Intent(FirstActivity.this);   
i.putExtra("STRING_I_NEED", strName);
i.startActivity();

在第二个活动

Intent n = getIntent();
String newString= extras.getString("STRING_I_NEED");
于 2013-08-06T06:36:47.767 回答
0

我想我知道你在问什么。我也有同样的问题。您可以像这样在字符串中创建值,例如: String url = getItem(pos).getLink(); 我收到了位置和链接,我做了一个字符串。

这是我的代码示例:

viewHolder.btnDownload = (Button) row.findViewById(R.id.btnDownload);
viewHolder.btnDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //getItemId(pos);
                String url = getItem(position).getLink();
于 2016-03-14T14:11:59.480 回答