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

所属分类: 软件编程 / Android 阅读数: 1508
收藏 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应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多