获取值并将其传递给xml中的另一个活动

时间:2013-08-06 06:28:40

标签: android

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

中的CustomizedList View代码

我通过添加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);


        }
    });     
}   
}

LazyAdapter

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

PlayVideo.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);
       }
   }

6 个答案:

答案 0 :(得分:1)

你可以试试这个:

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

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

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

现在您有一个查询如何从列表中获取ID。然后我建议给你的自定义适配器类的视图一个标签,这是你的视频ID,这样当你点击列表它会给你点击视图,你可以在哪里获得视图标签,所以最终你会得到视频的ID 。这样您就可以将该视频标记传递给另一个活动。就像这样:

vi.setTag(song.get(CustomizedListView.KEY_ID));
在getView()方法中的lazy loader类中的

。然后在列表的点击事件中获取标记,如下所示:

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

希望它能帮助!!

答案 1 :(得分:0)

将以下代码添加到onItemClick方法

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

答案 2 :(得分:0)

从XML获取视频ID的好方法是在您的项目中添加标记属性(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。

答案 3 :(得分: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");

答案 4 :(得分: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");

答案 5 :(得分: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();