Android开发之利用Intent实现数据传递的方法

所属分类: 软件编程 / Android 阅读数: 1472
收藏 0 赞 0 分享

本文实例讲述了Android利用Intent实现数据传递的方法。分享给大家供大家参考,具体如下:

在Android开发过程中,很多人都熟悉Intent,这是个用于在多个View之间共享数据的类。本节主要讲述通过点选ListView中的文本,把文本中的URL加载到一个新的页面上,并且打印出来。为了方便,我先把前面一篇《Android开发之利用jsoup解析HTML页面的方法》的代码重新贴一下,因为在上一节后,代码做了少许修改:

try {
    doc = Jsoup.parse(new URL("http://www.51yam.com"), 5000);
    } catch (MalformedURLException e1) {
      e1.printStackTrace();
    } catch (IOException e1) {
      e1.printStackTrace();
    }
    final List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    Elements es = doc.getElementsByClass("subnav");
    for (int i=0;i<es.size();i++) {
      Element e = es.get(i);
      int count = e.getElementsByTag("a").size();
      for(int j=0;j<count;j++)
      {
        Map<String, String> map = new HashMap<String, String>();
        Element ex = e.getElementsByTag("a").get(j);
        map.put("title", ex.text());
        map.put("href", "http://www.51yam.com/"+ex.attr("href"));
        list.add(map);
      }
    }
    ListView listView = (ListView) findViewById(R.id.listView1);
    listView.setAdapter(new SimpleAdapter(this, list, android.R.layout.simple_list_item_2,
        new String[] { "title","href" }, new int[] {
        android.R.id.text1,android.R.id.text2
}));

实现的效果如下:

然后我们需要做的就是当点击ListView中的项目的时候,程序会将每个话题下面的URL链接发送到新的页面显示:

下面是当点击ListView项目的时候,利用Intent传递数据的方法:

listView.setOnItemClickListener(new OnItemClickListener(){
  @Override
  public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
    //Toast.makeText(getApplicationContext(), (TextView), duration)
    System.out.println("position:"+position);
    System.out.println("id:"+id);
    //Toast.makeText(_GetWebResoureActivity.this, list.get(position).get("href"), Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(v.getContext(),topicdetails.class);
    intent.putExtra("src", list.get(position).get("href"));
    startActivityForResult(intent,0);
  }
});

在子页面“topicdetails.java”中,我们可以通过如下的方式来接收传递过来的值:

package com.android.web;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import java.lang.Object;
public class topicdetails extends Activity {
  private EditText editText;
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.topiccontent);
    editText = (EditText)this.findViewById(R.id.editText);
    String srcUrl = getIntent().getStringExtra("src");
    editText.setText(srcUrl);
  }
}

当然,一定不要忘记了在AndroidManifest.xml文件中添加Activity映射(黄色背景部分):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.android.web"
  android:versionCode="1"
  android:versionName="1.0" >
  <uses-sdk android:minSdkVersion="7" />
  <!-- 加入访问网络的权限 -->
  <uses-permission android:name="android.permission.INTERNET" />
  <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
      android:label="@string/app_name"
      android:name="._GetWebResoureActivity" >
      <intent-filter >
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name=".topicdetails"></activity>
  </application>
</manifest>

这样当一切工作准备完毕后,运行程序,点击ListView 的Item,我们成功地跳转到了子页面:

以下是所有的源码:

主页面源码:

package com.android.web;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.lang.Object;
import org.apache.http.util.ByteArrayBuffer;
import org.apache.http.util.EncodingUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class _GetWebResoureActivity extends Activity {
  Document doc;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        load();
      }
    });
  }
  protected void load() {
    try {
      doc = Jsoup.parse(new URL("http://www.51yam.com"), 5000);
    } catch (MalformedURLException e1) {
      e1.printStackTrace();
    } catch (IOException e1) {
      e1.printStackTrace();
    }
    final List<Map<String, String>> list = new ArrayList<Map<String, String>>();
    Elements es = doc.getElementsByClass("subnav");
    for (int i=0;i<es.size();i++) {
      Element e = es.get(i);
      int count = e.getElementsByTag("a").size();
      for(int j=0;j<count;j++)
      {
        Map<String, String> map = new HashMap<String, String>();
        Element ex = e.getElementsByTag("a").get(j);
        map.put("title", ex.text());
        map.put("href", "http://www.51yam.com/"+ex.attr("href"));
        list.add(map);
      }
    }
    ListView listView = (ListView) findViewById(R.id.listView1);
    listView.setAdapter(new SimpleAdapter(this, list, android.R.layout.simple_list_item_2,
        new String[] { "title","href" }, new int[] {
        android.R.id.text1,android.R.id.text2
    }));
    listView.setOnItemClickListener(new OnItemClickListener(){
      @Override
      public void onItemClick(AdapterView<?> parent, View v, int position,long id) {
        //Toast.makeText(getApplicationContext(), (TextView), duration)
        System.out.println("position:"+position);
        System.out.println("id:"+id);
        //Toast.makeText(_GetWebResoureActivity.this, list.get(position).get("href"), Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(v.getContext(),topicdetails.class);
        intent.putExtra("src", list.get(position).get("href"));
        startActivityForResult(intent,0);
      }
    });
  }
  /**
   * @param urlString
   * @return
   */
  public String getHtmlString(String urlString) {
    try {
      URL url = null;
      url = new URL(urlString);
      URLConnection ucon = null;
      ucon = url.openConnection();
      InputStream instr = null;
      instr = ucon.getInputStream();
      BufferedInputStream bis = new BufferedInputStream(instr);
      ByteArrayBuffer baf = new ByteArrayBuffer(500);
      int current = 0;
      while ((current = bis.read()) != -1) {
        baf.append((byte) current);
      }
      return EncodingUtils.getString(baf.toByteArray(), "gbk");
    } catch (Exception e) {
      return "";
    }
  }
}

子页面源码:

package com.android.web;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import java.lang.Object;
public class topicdetails extends Activity {
  private EditText editText;
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.topiccontent);
    editText = (EditText)this.findViewById(R.id.editText);
    String srcUrl = getIntent().getStringExtra("src");
    editText.setText(srcUrl);
  }
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android调试技巧与常见问题解决方法汇总》、《Android开发入门与进阶教程》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结

希望本文所述对大家Android程序设计有所帮助。

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多